问题
the problem falls under this button and i get the error here:
objcommand.ExecuteNonQuery()
Once i press this button, it should update all the records in my mysql database. Here is my code (the exact code works fine for another table):
Private Sub btnModify1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnModify1.Click
Using objconnection = New MySqlConnection("Server=localhost;database=ba-solutions;user id=root;password=")
objconnection.Open()
If Len(cbxCompanyName.Text) < 1 Then
MsgBox("Please select a company name")
Return
End If
If Len(txtPosition.Text) < 1 Then
MsgBox("Please enter the postion of the shareholder in the company")
Return
End If
If Len(txtNI.Text) <> 8 Then
MsgBox("The National Insurance Number must be 8 characters")
Return
End If
If Len(txtAddressLine.Text) < 1 Then
MsgBox("Enter a First Line of Address")
Return
End If
If Len(txtCity.Text) < 1 Then
MsgBox("Enter a City Name")
Return
End If
If Len(txtPostcode.Text) < 1 Then
MsgBox("Enter a Postcode")
Return
End If
If Len(txtEmail.Text) < 1 Then
MsgBox("Enter an Email Address")
Return
End If
If Len(txtPhoneNumber.Text) <> 11 Then
MsgBox("The Phone Number must be 11 numbers ")
Return
End If
'all validations for relevant textboxes
Dim companyname As String
Dim position As String
Dim ninumber As String
Dim dob As String
Dim forename As String
Dim surname As String
Dim addressline1 As String
Dim city As String
Dim postcode As String
Dim phonenumber As String
Dim email As String
postcode = txtPostcode.Text
companyname = cbxCompanyName.Text
position = txtPosition.Text
ninumber = txtNI.Text
dob = DTP1.Value
forename = txtForename.Text
surname = txtSurname.Text
addressline1 = txtAddressLine.Text
city = txtCity.Text
phonenumber = txtPhoneNumber.Text
email = txtEmail.Text
sqlstring = "UPDATE shareholder_details SET position=@position, ni=@ninumber, dob=@dob, forename=@forename, surname=@surname, addressline1=@addressline, city=@city, postcode=@postcode, phonenumber=@phone, email=@email where companyname= @company "
objcommand = New MySqlCommand(sqlstring, objconnection)
objcommand.Parameters.AddWithValue("@company", companyname)
objcommand.Parameters.AddWithValue("@postion", position)
objcommand.Parameters.AddWithValue("@ni", ninumber)
objcommand.Parameters.AddWithValue("@dob", dob)
objcommand.Parameters.AddWithValue("@forename", forename)
objcommand.Parameters.AddWithValue("@surname", surname)
objcommand.Parameters.AddWithValue("@addressline", addressline1)
objcommand.Parameters.AddWithValue("@city", city)
objcommand.Parameters.AddWithValue("@postcode", postcode)
objcommand.Parameters.AddWithValue("@phone", phonenumber)
objcommand.Parameters.AddWithValue("@email", email)
objcommand.ExecuteNonQuery()
btnRefresh.PerformClick()
Dim check As Integer = objcommand.ExecuteReader.RecordsAffected
If check < 1 Then
MessageBox.Show("Record was not updated, please try again", "Update unsucessfull",
MessageBoxButtons.OK, MessageBoxIcon.Error)
Else
MessageBox.Show("Record updated sucessfully", "Update sucessfull",
MessageBoxButtons.OK, MessageBoxIcon.Information)
objconnection.Close()
End If
End Using
End Sub
回答1:
you write
objcommand.Parameters.AddWithValue("@postion", position)
but this probably should be
objcommand.Parameters.AddWithValue("@position", position)
Other issues could be,
that you have ni=@ninumber
, in your SQL statement,
but you use objcommand.Parameters.AddWithValue("@ni", ninumber)
in your host variables
and in your SQL statement this looks different companyname= @company
compared to your usage of other host variables
and could you try to use the same order of objcommand.Parameters.AddWithValue("@host_variable", variable)
as you use in your SQL statement
回答2:
Solution that i used and it really works. This error is mostly caused by a MISSING or Incorrectly Spelled Parameter declaration. eg. @FirstName mistakenly spelled for @FirtName.
Make sure that all the parameters that are declared in the sql query are all declared in the AddwithValue Parameter declaration. (It helps to count the query versus the Addwithvalues).
The best solution is for visual studio to provide information about the missing Parameter. Use a Try-Catch block. In the catch block use Messagebox.show(ex.Innerexception.Message) instead of Messagebox.show(ex.message). This will show the exact Parameter that is missing. eg. below
Try
conCommand.Parameters.Addwithvalue("@FirstName", txtFirstName.text)
conCommand.Parameters.Addwithvalue("@MiddleName", txtMiddleName.text)
conCommand.Parameters.Addwithvalue("@LastName", txtLastName.text)
conCommand.Parameters.Addwithvalue("@PhoneNo", txtPhoneno.text)
catch ex as exception
Messagebox.show(ex.innerexception.Message)
End Try
Hope this helps. Its really great that we share our ideas in the world of programming. Thanks for reading.
if you need more assistance. Please contact me on flashrescueagency@gmail.com. I am not a pro but i have a lot of experience in Visual Studion (VB.NET), SQL Client and MySQL Client Programming.
来源:https://stackoverflow.com/questions/22501881/mysql-exception-was-unhandled-fatal-error-encountered-during-command-execution