'Failed to convert parameter value from a String to a Int32/

五迷三道 提交于 2020-05-09 17:11:59

问题


I have already post this question but now in details. I have trying to insert Date from Vb.net to SQL Database But when i click insert Button then show me above error message.I need your help Thanks.

I have been trying for a long time to insert the data in many ways, but have been consistently failing.

'Sql Database table data type is below.
';;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
Userid  int Checked
EmpSalary   int Checked
EmpName varchar(50) Checked
FatherName  varchar(50) Checked
EmailAddress    varchar(50) Checked
BankAccount int Checked
HomeAddress varchar(50) Checked
PersonalMobile  int Checked
DateofBirth date    Checked
EmpImage    image   Checked
';;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
'My Vb.net insert Query Code is below.

    Public Sub ExecuteMyQuery(cmd As SqlCommand, MyMessage As String)
        con.Open()
        If cmd.ExecuteNonQuery = 1 Then
            MessageBox.Show(MyMessage)
        Else
            MessageBox.Show("Query Not Execute")

        End If
        con.Close()
    End Sub
 Dim insertquery As String = "Insert into AddNew(Userid,EmpSalary,EmpName,FatherName,EmailAddress,BankAccount,HomeAddress,PersonalMobile,DateofBirth,EmpImage) values(@EmpID,@EmpSalary,@EmpName,@FatherName,@EmailAddress,@BankAccount,@HomeAddress,@PersonalMobile,@DtpDOB,@img)"

   Dim cmd As New SqlCommand(insertquery, con)

        cmd.Parameters.Add("@EmpID", SqlDbType.Int).Value = EmpID.Text
        cmd.Parameters.Add("@EmpSalary", SqlDbType.Int).Value = EmpSalary.Text
        cmd.Parameters.Add("@EmpName", SqlDbType.VarChar).Value = EmpName.Text
        cmd.Parameters.Add("@FatherName", SqlDbType.VarChar).Value = FatherName.Text
        cmd.Parameters.Add("@EmailAddress", SqlDbType.VarChar).Value = EmailAddress.Text
        cmd.Parameters.Add("@BankAccount", SqlDbType.Int).Value = BankAccount.Text
        cmd.Parameters.Add("@HomeAddress", SqlDbType.VarChar).Value = HomeAddress.Text
        cmd.Parameters.Add("@PersonalMobile", SqlDbType.Int).Value = PersonalMobile.Text
        cmd.Parameters.Add("@DtpDOB", SqlDbType.Date).Value = DtpDOB.Value
        cmd.Parameters.Add("@img", SqlDbType.Image).Value = img

        ExecuteMyQuery(cmd, "Saved Your Data Successful")

    End Sub

回答1:


Give this a go:

Dim r as New Regex("[^0-9]")

cmd.Parameters.Add("@EmpID", SqlDbType.Int).Value = r.Replace(EmpID.Text, "")
cmd.Parameters.Add("@EmpSalary", SqlDbType.Int).Value = r.Replace(EmpSalary.Text, "")
cmd.Parameters.Add("@EmpName", SqlDbType.VarChar).Value = EmpName.Text
cmd.Parameters.Add("@FatherName", SqlDbType.VarChar).Value = FatherName.Text
cmd.Parameters.Add("@EmailAddress", SqlDbType.VarChar).Value = EmailAddress.Text
cmd.Parameters.Add("@BankAccount", SqlDbType.Int).Value = r.Replace(BankAccount.Text, "")
cmd.Parameters.Add("@HomeAddress", SqlDbType.VarChar).Value = HomeAddress.Text
cmd.Parameters.Add("@PersonalMobile", SqlDbType.Int).Value = r.Replace(PersonalMobile.Text, "")
cmd.Parameters.Add("@DtpDOB", SqlDbType.Date).Value = DtpDOB.Value
cmd.Parameters.Add("@img", SqlDbType.Image).Value = img

It strips out non numbers present in text fields, meaning the conversion should succeed..

It would, however be better to a) Use a MaskedTextBox, a NumericUpDown or a textbox that rejects non-numbers and b) convert the text to int yourself so you can handle if it fails, using a nice error message:

Try
    cmd.Parameters.Add("@EmpID", SqlDbType.Int).Value = Convert.ToInt32(EmpID.Text)
    cmd.Parameters.Add("@EmpSalary", SqlDbType.Int).Value = Convert.ToInt32(EmpSalary.Text)
    ...
Catch ex as Exception
    'the last added parameter is the one that hit the problem
    MessageBox.Show($"Error adding a value for {cmd.Parameters.Last().ParameterName}")
End Try


来源:https://stackoverflow.com/questions/61108665/failed-to-convert-parameter-value-from-a-string-to-a-int32

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!