problem when adding into database

shabir

New member
Joined
Oct 8, 2010
Messages
1
Programming Experience
Beginner
hi guys... i am having problem when adding stuff in the database.i included the three functions that am using to add the data to the database. The connection is working fine so don't worry about it. its the probably the syntax of some code that is not working correctly. The line in red is where the compiler is complaining about.
here is the error that i get:

incorrect syntax near ')'. at
system.sql.data.sqlconnection.on error(.....)
at system.data.sqlclient.internalconnection.onerror(...)
at system.data.sqlclient.tsdParser.throwexceptionandWarning()
at system.data.tsqlclient.sdparser.run(...)
at system.data.sqlclient.sqlcommand.internalExecutionnonQueryTsd(...)




Protected Function UpdateDataSource(ByVal currentCommand As SqlCommand) As Boolean
Dim success As Boolean = False
Try
'open the connection
cnMain.Open()

currentCommand.CommandType = CommandType.Text
currentCommand.ExecuteNonQuery() ' am having the problem here

'close the connection
cnMain.Close()
success = True
Catch errObj As Exception
MessageBox.Show(errObj.Message & " " & errObj.StackTrace)
Finally

End Try
Return success
End Function



Public Sub DatabaseAdd(ByVal TempGuest As Guest)
Dim strSQL As String = ""

strSQL = "INSERT into Runner(GuestID, GuestName, GuestSurname, GuestDOB, GuestGender, GuestAddress, GuestPostalcode, GuestPhone, GuestEmail)" & _
"VALUES ('" & GetValueString(TempGuest) & ")"

'Create & execute the insert command
UpdateDataSource(New SqlCommand(strSQL, cnMain))

End Sub



Private Function GetValueString(ByVal TempGuest As Guest) As String
Dim aStr As String = ""

aStr = TempGuest.GuestID & "' , '" & TempGuest.GuestName & "' ," & _
"' " & TempGuest.GuestSurname & " ' ," & _
"'" & TempGuest.DOB & "' , " & _
"'" & TempGuest.Gender & " ' ," & _
"'" & TempGuest.Address & " ' ," & _
"'" & TempGuest.PostalCode & " ' ," & _
"'" & TempGuest.PhoneNumber & " ' ," & _
"' " & TempGuest.Email & "' , "


Return aStr
End Function
 
This is a classic case of not actually looking at the data you're using. I suggest that you actually look at the value of strSQL and I think you'll find that the issue is obvious.

After that, follow the Blog link in my signature and check out my post on ADO.NET parameters to find the proper way to insert variables into SQL code so you can avoid such issues in the future.
 
Back
Top