问题
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