转换数据类型错误 [英] conversion datatype error

查看:93
本文介绍了转换数据类型错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



i我是初学者和编程初学者

i我正在使用Visual Studio终极版和sql server快递版

i写一个代码来搜索手机数据库中的数字

但是我收到的错误是int 32的值太大或太小或从字符串转换为整数无效

in my数据库的列类型是nvarchar(11),包括0总数是11

i也试过convert.toint32和convert.toint64但是错误仍然存​​在。

我的代码是

  Dim  cmd  As   SqlClient.SqlCommand 
Dim DaLogin As < span class =code-keyword>新 SqlClient.SqlDataAdapter
Dim ds As DataSe t
cmd.CommandText = SELECT PhoneNo来自[Users] WHERE PhoneNo ='& ; Convert.ToInt64(txtloginuser.Text.Trim)& '
Con.Open()
cmd.Connection = Con
DaLogin.SelectCommand = cmd
DaLogin.Fill(ds, 0
Dim count = ds.Tables( 0 )。Rows.Count
< span class =code-comment>' 检查数据库中是否存在数据
如果计数> 0 然后
MsgBox( 现在找到用户打开 有效用户,vbOKOnly + vbInformation)
btnconnect.Enabled = True
Else
MsgBox( 用户未在数据库中注册。请重试或创建新帐户 无效的电话号码,vbOKOnly + vbCritical)
txtloginuser.Text = Focused( )

结束 如果





plz检查并告诉我删除此错误的解决方案

解决方案

  Dim 查询作为 字符串 =  从[Users]中选择PhoneNo WHERE PhoneNo = @LoginUser 
使用 conn 作为 系统。 Data.SqlClient.SqlConnection( Connection String
Dim cmd As System.Data.SqlClient.SqlCommand(query,conn )
cmd.Parameters.AddWithValue( @ LoginUser,SqlDbType.NVarChar).Value = txtloginuser.Text
conn.Open()
cmd.ExecuteNonQuery()


我看不出你为何会这样做的原因转换txtL如果基础数据类型是nvarchar,则oginId进入int32。



这应该也可以。



 cmd.CommandText =  从[Users]选择PhoneNo WHERE PhoneNo =' txtloginuser.Text.Trim&  ' 


thanks提供你的经验

i尝试了这个syed asif iqbal但同样的错误arrises

i我正在尝试chimcham解决方案

谢谢


hi
i am new and beginner in programming
i am using Visual Studio ultimate and sql server express edition
i write a code to search phone number from the database
but i am getting error of " too large or too small value for int 32" or "conversion from string to integer is not valid"
in my database the column type is nvarchar(11) and including 0 total digits are 11
i also tried convert.toint32 and convert.toint64 but error still there.
my code is

Dim cmd As New SqlClient.SqlCommand
            Dim DaLogin As New SqlClient.SqlDataAdapter
            Dim ds As New DataSet
            cmd.CommandText = "SELECT PhoneNo from [Users] WHERE PhoneNo = '" & Convert.ToInt64(txtloginuser.Text.Trim) & "'"
            Con.Open()
            cmd.Connection = Con
            DaLogin.SelectCommand = cmd
            DaLogin.Fill(ds, "0")
            Dim count = ds.Tables(0).Rows.Count
            'check weither data exist in database or not 
            If count > 0 Then
                MsgBox("User found now open the conenction", "Valid User", vbOKOnly + vbInformation)
                btnconnect.Enabled = True
            Else
                MsgBox("User not registered in database. try again or create a new account", "Invalid Phone No", vbOKOnly + vbCritical)
                txtloginuser.Text = Focused()

            End If



plz check and tell me the solution for removing this error

解决方案

Dim query As String = "SELECT PhoneNo from [Users] WHERE PhoneNo = @LoginUser"
   Using conn As New System.Data.SqlClient.SqlConnection("Connection String")
     Dim cmd As New System.Data.SqlClient.SqlCommand(query, conn)
     cmd.Parameters.AddWithValue("@LoginUser", SqlDbType.NVarChar).Value = txtloginuser.Text
     conn.Open()
     cmd.ExecuteNonQuery()


I don't see a reason why you are converting txtLoginId into int32 if the underlying datatype is nvarchar.

this should work as well.

cmd.CommandText = "SELECT PhoneNo from [Users] WHERE PhoneNo = '" txtloginuser.Text.Trim & "'"


thanks for providing ur experience
i have tried this syed asif iqbal but same error arrises
i am trying the chimcham solution
thanks


这篇关于转换数据类型错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆