开发者

VS2010 MySqlException was unhandled

I am new to VB programming and have come across an problem:( after a couple of days trying to resolve this need some help!

I am trying to pass some information from a VB form to my MySQL database. i have named all the textbox's the same as the field in the database and checked all my database fields and textbox names which are all correct.

When i try to enter information into a form I sometimes get an error at the .executeNonQuery section of the code.

To test, I outputted the SQLStatement string to a textbox ( which pulled through all the fields from the textboxes correctly) then manually inputted the completed SQL query into the database and it worked. But when I try to do this in one go it seems to fail if there is too much text ( if i enter 'a' into each field it works). Are they limits to the size of the SQL query that can be passed from VB?? all the MySql database fields are set to text with no size limits.

Thanks in advance!!!

    Public Sub SaveQuote(ByRef SQLStatement As String)
    Dim cmd As MySqlCommand = New MySqlCommand

    With cmd
        .CommandText = SQLStatement
        .CommandType = CommandType.Text
 开发者_如何学Go       .Connection = SQLConnection
        .ExecuteNonQuery()
    End With
    SQLConnection.Close()
    MsgBox("successfully Added!")
    SQLConnection.Dispose()
End Sub

Private Sub CmdSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CmdSave.Click

    Dim SQLstatement As String = "INSERT INTO laptopstock(forename,surname,emailaddress,contactnumber,quotedate,manufacturer,model,os,battery,drive,defects) VALUES('" & forename.Text & "','" & surname.Text & "','" & emailaddress.Text & "','" & contactnumber.Text & "', CURDATE(),'" & manufacturer.Text & "','" & modelnumber.Text & "','" & os.Text & "','" & batterycondition.Text & "','" & drivetype.Text & "','" & defects.Text & "')"
    SaveQuote(SQLstatement)
End Sub

'Test SQL query

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click


    Dim testSQLstatement As String = "INSERT INTO laptopstock(forename,surname,emailaddress,contactnumber,quotedate,manufacturer,model,os,battery,drive,defects) VALUES('" & forename.Text & "','" & surname.Text & "','" & emailaddress.Text & "','" & contactnumber.Text & "', CURDATE(),'" & manufacturer.Text & "','" & modelnumber.Text & "','" & os.Text & "','" & batterycondition.Text & "','" & drivetype.Text & "','" & defects.Text & "')"


    testbox.Text = testSQLstatement

End Sub

here is the output from testSQLstatement = testbox.text

    INSERT INTO laptopstock(forename,surname,emailaddress,contactnumber,quotedate,manufacturer,model,os,battery,drive,defects) VALUES('Joe','Bloggs','J.bloggs@jbloggs.com','07777777777', CURDATE(),'Sony','Vaio','Windows 7 Pro','Poor','DVD-Rom','Faulty Screen')

from what i can see it is correctly formatted and when i enter this directly into a query on the MySql server a record is created


Does your application SQL login have insert rights on this table? Try executing a grant statment in SQL console directly: "grant insert on dbo.laptopstock to (add you application login here)" Also, I why are you passing the SQLStatement byref? You are not modifying it, so use byval. This shouldn't impact the code, but is a good practice.

I see you state that sometimes you get an error, so we can asume it is working code. That leads me to believe it is in your input data. Do the fields accept nulls? Are they the right format? Also, one of these things is not like the others, the date. You are passing the curdate() function in the insert. If the fields are all strings like you mention, then you are performing an implicit conversion from date to string. You could as easily built the insert string using the Vb.Net equivalent. (Date.Now.ToString).

Lastly, it is hard to debug this without more detailed information about the error. As you have not posted code for your SQLConnection and MySLCommand objects (sure you don't mean MySQLCommand as New SQLCommand) I have to assume they work.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜