VB 2008 sintax error

Microsoft Visual studio 2008 professiona...
January 15, 2011 at 12:33:09
Specs: Windows XP Professional, 2 GB
I made a code to change a date field on an Access Table. The new date is input in a MaskedEditBox in the form. The change is made through the click event in this form.
On running the program Im having the following error message: "Data recording error. Sintax error
(missing operator) in the consulting expression ' & csdate'
What should by the error and how do I correct it?
The complete code is:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim cs As String = My.Settings.SociosAepetNConnectionString
Dim conect As New OleDb.OleDbConnection
conect.ConnectionString = cs
Try
conect.Open()
Dim selcom As New OleDb.OleDbCommand
Dim csDate As Date
If IsDate(MaskedTextBox1.Text) Then
csDate = CDate(MaskedTextBox1.Text)
End If
csdate = MaskedTextBox1.Text
selcom.Connection = conect
selcom.CommandText = "UPDATE SÓCIOS Set Validade= & csdate"
selcom.ExecuteNonQuery()
conect.Close()
Catch ex As Exception
MessageBox.Show("Erro de gravação de dados!" & vbCrLf & ex.Message, "ERRO", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Sub

Thanks for any help


See More: VB 2008 sintax error

Report •


#1
January 15, 2011 at 12:42:45
You should be using parameterized queries when updating databases.

selcom.CommandText = "UPDATE SÓCIOS Set Validade= & csdate"
That said, be glad your database choked; every Validade field in SÓCIOS would have had its value changed to whatever csdate happened to be.

How To Ask Questions The Smart Way


Report •

#2
January 16, 2011 at 05:22:02
Tahnks Razor 2.3 for your answer. Neverthless you didnt give me the correction to the code. Im a biginner in VB 2008.
What should be the changes I had to make?
Thanks again.

Report •

#3
January 16, 2011 at 05:44:46
Fine, I don't have all the information, and I don't have access to a box with VS at the moment, but this should get you started.
selcom.CommandText = "UPDATE SÓCIOS Set Validade=? WHERE <someField>=?"
selcom.Parameters.Add("Validade", csdate)
selcom.Parameters.Add("<someField>", <someVar>)

How To Ask Questions The Smart Way


Report •

Related Solutions

#4
January 19, 2011 at 09:18:12
Razor 2.3
Thanks! Your hint did the trick! There was only one change: instead of : 'selcom.Parameters.Add' we should use : selcom.Parameters.AddWithValue
The problem is solved I think.
Thanks again.

Report •


Ask Question