Error message: Syntax error in UPDATE statement [closed] - vb.net

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
I don't know why when ever I click on the update button, I get an error
Syntax error in UPDATE statement
I have no idea what's going wrong in my code
This is my code:
Public Class Form1
Private Function vld(ByVal ParamArray ctl() As Object) As Boolean
For i As Integer = 0 To UBound(ctl)
If ctl(i).text = "" Then
ErrorProvider1.SetError(ctl(i), ctl(i).tag)
Return False
Exit For
End If
Next
Return True
End Function
Dim cn As New OleDbConnection
Dim cm As New OleDbCommand
Dim da As OleDbDataAdapter
Dim dt As New DataTable
Private Sub Form1_FormClosing(sender As Object, e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
cn.Close()
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
TxtExamtime.Format = DateTimePickerFormat.Custom
TxtExamtime.CustomFormat = "hh:MM tt"
cn.ConnectionString = "provider=microsoft.jet.oledb.4.0; Data Source=C:\psave\New folder\save.xls;Extended Properties=Excel 8.0;"
cn.Open()
FillDataGridView("select ID, Family Name, Given Name, Gender, DOB, Exam Date, Exam Time, Street Name, House Nr, PLZ, City from [edit$]")
End Sub
Private Sub FillDataGridView(ByVal Query As String)
da = New OleDbDataAdapter(Query, cn)
dt.Clear()
da.Fill(dt)
With DataGridView1
.DataSource = dt
.Columns(0).HeaderText = "ID"
.Columns(1).HeaderText = "Family Name"
.Columns(2).HeaderText = "Given Name"
.Columns(3).HeaderText = "Gender"
.Columns(4).HeaderText = "DOB"
.Columns(5).HeaderText = "Exam Date"
.Columns(6).HeaderText = "Exam Time"
.Columns(7).HeaderText = "Street Name"
.Columns(8).HeaderText = "House Nr"
.Columns(9).HeaderText = "PLZ"
.Columns(10).HeaderText = "City"
.Columns(10).AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill
End With
End Sub
Private Sub BtnSearch_Click(sender As Object, e As EventArgs) Handles BtnSearch.Click
Try
FillDataGridView("select * from [edit$] where ID='" & TxtId.Text & "'")
TxtFamilyname.Text = dt.Rows(0).Item(1)
TxtGivenname.Text = dt.Rows(0).Item(2)
TxtGender.Text = dt.Rows(0).Item(3)
TxtDob.Text = dt.Rows(0).Item(4)
TxtExamdate.Text = dt.Rows(0).Item(5)
TxtExamtime.Text = dt.Rows(0).Item(6)
TxtStreet.Text = dt.Rows(0).Item(7)
TxtHouse.Text = dt.Rows(0).Item(8)
TxtPlz.Text = dt.Rows(0).Item(9)
TxtCity.Text = dt.Rows(0).Item(10)
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.Critical, Text)
End Try
End Sub
Private Sub BtnSave_Click(sender As Object, e As EventArgs) Handles BtnSave.Click
If vld(TxtId, TxtFamilyname, TxtGivenname, TxtGender, TxtDob, TxtExamdate, TxtExamtime, TxtStreet, TxtHouse, TxtPlz, TxtCity) = False Then
Exit Sub
Else
End If
Try
With cm
.Connection = cn
.CommandText = "insert into [edit$]values('" & TxtId.Text & "','" & TxtFamilyname.Text & "','" & TxtGivenname.Text & "','" & TxtGender.Text & "','" & TxtDob.Text & "','" & TxtExamdate.Text & "','" & TxtExamtime.Text & "','" & TxtStreet.Text & "','" & TxtHouse.Text & "','" & TxtPlz.Text & "','" & TxtCity.Text & "' )"
.ExecuteNonQuery()
End With
FillDataGridView("select * from [edit$]")
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.Critical, Text)
Return
End Try
MsgBox("succefully Saved!", MsgBoxStyle.Information, Text)
End Sub
Private Sub BtnUpdate_Click(sender As Object, e As EventArgs) Handles Btnupdate.Click
Try
With cm
.Connection = cn
.CommandText = "Update from [edit$] set [Family Name] = '" & TxtFamilyname.Text & "' where ID ='" & TxtId.Text & "' and Given Name = '" & TxtGivenname.Text & "' and Gender = '" & TxtGender.Text & "'and DOB = '" & TxtDob.Text & "'and Exam Date'" & TxtExamdate.Text & "'and Exam Time = '" & TxtExamtime.Text & "'and Street Name = '" & TxtStreet.Text & "'and House Nr = '" & TxtHouse.Text & "'and PLZ = '" & TxtPlz.Text & "'and CITY = '" & TxtCity.Text & "'"
.ExecuteNonQuery()
End With
FillDataGridView("select * from [edit$]")
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.Information, Text)
Return
End Try
MsgBox("Succesfully updated!", MsgBoxStyle.Information, Text)
End Sub
Private Sub BtnClose_Click(sender As Object, e As EventArgs) Handles BtnClose.Click
Close()
End Sub
Private Sub BtnClear_Click(sender As Object, e As EventArgs) Handles BtnClear.Click
TxtId.Clear()
TxtFamilyname.Clear()
TxtGivenname.Clear()
TxtStreet.Clear()
TxtHouse.Clear()
TxtPlz.Clear()
TxtCity.Clear()
'To see all the data in DataGridView
FillDataGridView("select * from[edit$]")
End Sub
Private Sub BtnDelete_Click(sender As Object, e As EventArgs) Handles BtnDelete.Click
Try
With cm
.Connection = cn
.CommandText = "Delete from [edit$] where [Family Name] = '" & TxtFamilyname.Text & "' and ID ='" & TxtId.Text & "' and [Given Name] = '" & TxtGivenname.Text & "'and Gender = '" & TxtGender.Text & "'and DOB = '" & TxtDob.Text & "'and [Exam Date]'" & TxtExamdate.Text & "'and [Exam Time] = '" & TxtExamtime.Text & "'and [Street Name] = '" & TxtStreet.Text & "'and [House Nr] = '" & TxtHouse.Text & "'and PLZ = '" & TxtPlz.Text & "'and CITY = '" & TxtCity.Text & "'"
.ExecuteNonQuery()
End With
MsgBox("Succesfully Deleted!", MsgBoxStyle.Information, Text)
FillDataGridView("select * from [edit$]")
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.Information, Text)
End Try
End Sub
End Class

An Update statement does not have a "From" in it, so it should start with...
Update [Edit$]
There's a couple of other things wrong too. If the ID is a number then it probably doesn't need bounding single quotes, through they won't stop it either...
Where ID = " & txtId.text & "
Columns names that have embedded white space need bounding brackets...
and [Given Name] = '" & txtGivenName.text & "'
Finally, this statement is wide open to SQL Injection, where someone could do serious damage to your table by entering SQL into one of your text boxes. Please consider using parameters instead.
You should also consider using Microsoft.ACE.OLEDB.12.0 as the one you are using is quite old now.
You should probably have other parameters in your extended properties if you want to use Excel as a database, in particular you will need HDR=Yes...
Extended Properties=""Excel 8.0;HDR=Yes"""
This tells OLEDB that the first line of your sheet contains the column names, otherwise it will use F1...Fn (I think but it may be C1...Cn)

Beside some other issues with your code (e.g. you should almost always use parameterized queries), Update from [edit$] set... is wrong.
Just use Update [edit$] set....

Your Syntax for Update statment is clearly not correct :
please find below code :
Private Sub BtnUpdate_Click(sender As Object, e As EventArgs) Handles Btnupdate.Click
Try
With cm
.Connection = cn
.CommandText = "Update [edit$] set [Family Name] = '" & TxtFamilyname.Text & "' where ID ='" & TxtId.Text & "' and [Given Name] = '" & TxtGivenname.Text & "' and Gender = '" & TxtGender.Text & "'and DOB = '" & TxtDob.Text & "'and [Exam Date]='" & TxtExamdate.Text & "'and [Exam Time]= '" & TxtExamtime.Text & "'and [Street Name] = '" & TxtStreet.Text & "'and [House Nr]= '" & TxtHouse.Text & "'and PLZ = '" & TxtPlz.Text & "'and CITY = '" & TxtCity.Text & "'"
.ExecuteNonQuery()
End With
FillDataGridView("select * from [edit$]")
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.Information, Text)
Return
End Try
MsgBox("Succesfully updated!", MsgBoxStyle.Information, Text)
End Sub
Place all column names inside [ ] other wise the query will be interpreted wrongly because it might ignore the rest of the query as soon as it encounters a white space
You have done it correctly for family name but certainly ignored other column names which have a space in between them

Related

INSERT / UPDATE error updating access database through VB.net

I am trying to update / save a new record in an access database file through VB.
When I run the application and press the save or update button I am receiving the 2 errors:
Additional information: Syntax error in INSERT INTO statement.
Additional information: Syntax error in UPDATE statement.
Can anyone see the problem with my syntax?
I will attach the code and a screenshot of GUI
Imports System.Data.OleDb
Public Class Form1
Dim dbconn As New OleDbConnection
Dim adt As New OleDbDataAdapter
Dim ds As New DataSet
Dim datatable As New DataTable
Dim cmd As New OleDbCommand
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
dbconn.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0; data source = CUBSDatabase.accdb"
showData()
End Sub
Private Sub btnSave_Click(sender As Object, e As EventArgs) Handles btnSave.Click
adt = New OleDbDataAdapter("insert into Student (FName, SName, Attendance, CA1, CA2, FinalExam) values ( '" & txtFName.Text & "','" & txtSName.Text & "', '" & txtAttendance.Text & "', '" & txtCA1.Text & "', '" & txtCA2.Text & "', '" & txtFinalExam.Text & "', )", dbconn)
adt.Fill(ds)
ds = New DataSet
showData()
MsgBox("Saved")
End Sub
Private Sub showData()
Dim dbcommand As String
dbcommand = "SELECT * FROM Student"
adt = New OleDbDataAdapter(dbcommand, dbconn)
datatable = New DataTable
adt.Fill(datatable)
DataGridView1.DataSource = datatable
End Sub
Private Sub btnFind_Click(sender As Object, e As EventArgs) Handles btnFind.Click
Dim sql = "select * from Student where ID =" & txtID.Text & " "
adt = New OleDbDataAdapter(sql, dbconn)
cmd = New OleDbCommand(sql)
adt.Fill(ds, "Student")
txtFName.Text = ds.Tables("Student").Rows(0)(1).ToString
txtSName.Text = ds.Tables("Student").Rows(0)(2).ToString
txtAttendance.Text = ds.Tables("Student").Rows(0)(3).ToString
txtCA1.Text = ds.Tables("Student").Rows(0)(4).ToString
txtCA2.Text = ds.Tables("Student").Rows(0)(5).ToString
txtFinalExam.Text = ds.Tables("Student").Rows(0)(6).ToString
ds = New DataSet
End Sub
Private Sub TabPage1_Click(sender As Object, e As EventArgs) Handles TabPage1.Click
End Sub
Private Sub btnUpdate_Click(sender As Object, e As EventArgs) Handles btnUpdate.Click
adt = New OleDbDataAdapter("update Student set FName='" & txtFName.Text & "', SName='" & txtSName.Text & "', Attendance='" & txtAttendance.Text & "', CA1'" & txtCA1.Text & "', CA2'" & txtCA2.Text & "', FinalExam'" & txtFinalExam.Text & "'where ID=" & txtID.Text & "", dbconn)
adt.Fill(ds)
ds = New DataSet
showData() ' refresh data in datagridview
MsgBox("Updated")
End Sub
End Class
This is my GUI
James was almost there, but I think this should do it for you...
adt = New OleDbDataAdapter("update Student set FName='" & txtFName.Text _
& "', SName='" & txtSName.Text _
& "', Attendance='" & txtAttendance.Text _
& "', CA1='" & txtCA1.Text _
& "', CA2='" & txtCA2.Text _
& "', FinalExam='" & txtFinalExam.Text _
& "' where ID='" & txtID.Text & "'", dbconn)
I have blocked it for easier reading, the full string is below too if you prefer that (Both do same thing).
adt = New OleDbDataAdapter("update Student set FName='" & txtFName.Text & "', SName='" & txtSName.Text & "', Attendance='" & txtAttendance.Text & "', CA1='" & txtCA1.Text & "', CA2='" & txtCA2.Text & "', FinalExam='" & txtFinalExam.Text & "' where ID='" & txtID.Text & "'", dbconn)
Side note - Be aware of the texts you are passing too - for example if txtSName contains a ' within the string, it is going to throw errors.
Have a check of This Stackoverflow Article or Microsoft SQL Parameters They both can help you with this type of coding.
As far as i can tell, you are missing your closing ' in the following line
adt = New OleDbDataAdapter("update Student set FName='" & txtFName.Text & "', SName='" & txtSName.Text & "', Attendance='" & txtAttendance.Text & "', CA1'" & txtCA1.Text & "', CA2'" & txtCA2.Text & "', FinalExam'" & txtFinalExam.Text & "'where ID=" & txtID.Text & "", dbconn)
should be
adt = New OleDbDataAdapter("update Student set FName='" & txtFName.Text & "', SName='" & txtSName.Text & "', Attendance='" & txtAttendance.Text & "', CA1'" & txtCA1.Text & "', CA2'" & txtCA2.Text & "', FinalExam'" & txtFinalExam.Text & "'where ID=" & txtID.Text & "'", dbconn)

Error: Syntax error (missing operators) in query expression?

When ever I click on the UPDATE button it showing me the error as "Syntax error (missing operators) in query expression" but the save and refresh functions are working fine. I don't know what is the syntax error in the update button.
Here is my code:
Private Sub FillDataGridView(ByVal Query As String)
da = New OleDbDataAdapter(Query, cn)
dt.Clear()
da.Fill(dt)
With DataGridView1
.DataSource = dt
.Columns(0).HeaderText = "ID"
.Columns(1).HeaderText = "Name"
.Columns(2).HeaderText = "Age"
.Columns(1).AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill
End With
End Sub
Private Sub BtnSearch_Click(sender As Object, e As EventArgs) Handles BtnSearch.Click
Try
FillDataGridView("select * from [edit$] where ID='" & TxtId.Text & "'")
TxtFamilyname.Text = dt.Rows(0).Item(1)
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.Critical, Text)
End Try
End Sub
Private Sub BtnSave_Click(sender As Object, e As EventArgs) Handles BtnSave.Click
Try
With cm
.Connection = cn
.CommandText = "insert into [edit$]values('" & TxtId.Text & "','" & TxtFamilyname.Text & "', '" & TxtAge.Text & "')"
.ExecuteNonQuery()
End With
FillDataGridView("select * from [edit$]")
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.Critical, Text)
Return
End Try
MsgBox("successfully Saved!", MsgBoxStyle.Information, Text)
End Sub
Private Sub BtnUpdate_Click(sender As Object, e As EventArgs) Handles BtnUpdate.Click
Try
With cm
.Connection = cn
.CommandText = "Update [edit$] set [Family Name] = '" & TxtFamilyname.Text & "' where id ='" & TxtId.Text & "' where Age= '" & TxtAge.Text & "'"
.ExecuteNonQuery()
End With
FillDataGridView("select * from [edit$]")
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.Information, Text)
Return
End Try
MsgBox("Successfully updated!", MsgBoxStyle.Information, Text)
End Sub
Use AND with WHERE clause between two or more conditions
where id ='" & TxtId.Text & "' AND Age= '" & TxtAge.Text & "'"
You probably shouldn't have single quotes around your id and age fields (assuming these are numeric data types) Also - you have two where clauses which is not allowed.
So it should look more like this:
.CommandText = "Update [edit$] set [Family Name] = '" & TxtFamilyname.Text & "' where id =" & TxtId.Text & " AND Age= " & TxtAge.Text
But having said that you shouldn't write sql queries like this anyway. Search for Little Bobby Tables, then search for parameterised queries - you don't need to worry about single quotes when using parameterised queries
You cant have two where clauses...
Use "AND"/"OR" to extend your query.
SQL AND & OR Operators
There are two WHERE statements in your Update CommanText, try the following;
.CommandText = "Update [edit$] set [Family Name] = '" & TxtFamilyname.Text & "' where id ='" & TxtId.Text & "' and Age= '" & TxtAge.Text & "'"
You can't have two where clauses in the update. Use and between the conditions:
.CommandText = "Update [edit$] set [Family Name] = '" & TxtFamilyname.Text & "' where id ='" & TxtId.Text & "' and Age= '" & TxtAge.Text & "'"
That said, your code is wide open for SQL injection attacks. You should rather use parameterised queries than to concatenate the values into the queries. Example:
With cm
.Connection = cn
.CommandText = "Update [edit$] set [Family Name] = #name where id = #id and Age = #age"
.Parameters.AddWithValue("#name", TxtFamilyname.Text)
.Parameters.AddWithValue("#id", TxtId.Text)
.Parameters.AddWithValue("#age", TxtAge.Text)
.ExecuteNonQuery()
End With

How to delete the data of a row in the excel sheet DB from datagridview through a Delete button "BtnDelete"

I don't know how to delete the data of a row in the excel sheet DB from DataGridView. its not showing any error but the selected row in DataGridView aslo not getting delete. No idea what's going wrong in my code
Here is my code:
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
TxtExamtime.Format = DateTimePickerFormat.Custom
TxtExamtime.CustomFormat = "hh:MM tt"
cn.ConnectionString = "provider=microsoft.jet.oledb.4.0; Data Source=C:\psave\New folder\save.xls;Extended Properties=Excel 8.0;"
cn.Open()
FillDataGridView("select ID, Family Name, Given Name, Gender, DOB, Exam Date, Exam Time, Street Name, House Nr, PLZ, City from [edit$]")
End Sub
Private Sub FillDataGridView(ByVal Query As String)
da = New OleDbDataAdapter(Query, cn)
dt.Clear()
da.Fill(dt)
With DataGridView1
.DataSource = dt
.Columns(0).HeaderText = "ID"
.Columns(1).HeaderText = "Family Name"
.Columns(2).HeaderText = "Given Name"
.Columns(3).HeaderText = "Gender"
.Columns(4).HeaderText = "DOB"
.Columns(5).HeaderText = "Exam Date"
.Columns(6).HeaderText = "Exam Time"
.Columns(7).HeaderText = "Street Name"
.Columns(8).HeaderText = "House Nr"
.Columns(9).HeaderText = "PLZ"
.Columns(10).HeaderText = "City"
.Columns(10).AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill
End With
End Sub
Private Sub BtnSave_Click(sender As Object, e As EventArgs) Handles BtnSave.Click
Try
With cm
.Connection = cn
.CommandText = "insert into [edit$]values('" & TxtId.Text & "','" & TxtFamilyname.Text & "','" & TxtGivenname.Text & "','" & TxtGender.Text & "','" & TxtDob.Text & "','" & TxtExamdate.Text & "','" & TxtExamtime.Text & "','" & TxtStreet.Text & "','" & TxtHouse.Text & "','" & TxtPlz.Text & "','" & TxtCity.Text & "' )"
.ExecuteNonQueryAsync()
End With
FillDataGridView("select * from [edit$]")
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.Critical, Text)
Return
End Try
MsgBox("succefully Saved!", MsgBoxStyle.Information, Text)
End Sub
Private Sub BtnUpdate_Click(sender As Object, e As EventArgs) Handles Btnupdate.Click
Try
With cm
.Connection = cn
.CommandText = "Update [edit$] set [Family Name] = '" & TxtFamilyname.Text & "' where id ='" & TxtId.Text & "' and Given Name = '" & TxtGivenname.Text & "'and Gender = '" & TxtGender.Text & "'and DOB = '" & TxtDob.Text & "'and Exam Date'" & TxtExamdate.Text & "'and Exam Time = '" & TxtExamtime.Text & "'and Street Name = '" & TxtStreet.Text & "'and House Nr = '" & TxtHouse.Text & "'and PLZ = '" & TxtPlz.Text & "'and CITY = '" & TxtCity.Text & "'"
.ExecuteNonQueryAsync()
End With
FillDataGridView("select * from [edit$]")
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.Information, Text)
Return
End Try
MsgBox("Succesfully updated!", MsgBoxStyle.Information, Text)
End Sub
Private Sub BtnDelete_Click(sender As Object, e As EventArgs) Handles BtnDelete.Click
Try
With cm
.Connection = cn
.CommandText = "Delete [edit$] set [Family Name] = '" & TxtFamilyname.Text & "' where id ='" & TxtId.Text & "' and Given Name = '" & TxtGivenname.Text & "'and Gender = '" & TxtGender.Text & "'and DOB = '" & TxtDob.Text & "'and Exam Date'" & TxtExamdate.Text & "'and Exam Time = '" & TxtExamtime.Text & "'and Street Name = '" & TxtStreet.Text & "'and House Nr = '" & TxtHouse.Text & "'and PLZ = '" & TxtPlz.Text & "'and CITY = '" & TxtCity.Text & "'"
.ExecuteNonQueryAsync()
End With
FillDataGridView("select * from [edit$]")
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.Information, Text)
Return
End Try
MsgBox("Succesfully Deleted!", MsgBoxStyle.Information, Text)
End Sub
Your DELETE statement is wrong. It should be throw an exception (a MsgBox showing the error as per your code) when trying to delete the record.
The correct syntax is
DELETE FROM table_name
WHERE some_column=some_value;
E.g:
DELETE FROM Customers
WHERE CustomerName='Alfreds Futterkiste' AND ContactName='Maria Anders';
Supposing that you want to delete the record that matches with ALL the textboxes (as per your code), your CommandText should be something like this
.CommandText = "Delete from [edit$] where [Family Name] = '" & TxtFamilyname.Text & "' and id ='" & TxtId.Text & "' and [Given Name] = '" & TxtGivenname.Text & "'and Gender = '" & TxtGender.Text & "'and DOB = '" & TxtDob.Text & "'and [Exam Date]'" & TxtExamdate.Text & "'and [Exam Time] = '" & TxtExamtime.Text & "'and [Street Name] = '" & TxtStreet.Text & "'and [House Nr] = '" & TxtHouse.Text & "'and PLZ = '" & TxtPlz.Text & "'and CITY = '" & TxtCity.Text & "'"
Edit
Also your message Succesfully Deleted! is outside of the Catch. It will show that message even when a exception has been thrown
Try this
Private Sub BtnDelete_Click(sender As Object, e As EventArgs) Handles BtnDelete.Click
Try
With cm
.Connection = cn
.CommandText = "Delete from [edit$] where [Family Name] = '" & TxtFamilyname.Text & "' and id ='" & TxtId.Text & "' and [Given Name] = '" & TxtGivenname.Text & "'and Gender = '" & TxtGender.Text & "'and DOB = '" & TxtDob.Text & "'and [Exam Date]'" & TxtExamdate.Text & "'and [Exam Time] = '" & TxtExamtime.Text & "'and [Street Name] = '" & TxtStreet.Text & "'and [House Nr] = '" & TxtHouse.Text & "'and PLZ = '" & TxtPlz.Text & "'and CITY = '" & TxtCity.Text & "'"
.ExecuteNonQuery()
End With
MsgBox("Succesfully Deleted!", MsgBoxStyle.Information, Text)
FillDataGridView("select * from [edit$]")
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.Information, Text)
End Try
End Sub

"Error: Operator '&' is not defined for types 'String' and 'System.Windows.Forms.TextBox'."

Im getting an error as in both SAVE and UPDATE button in .CommandText
"Error: Operator '&' is not defined for types 'String' and 'System.Windows.Forms.TextBox'."
I don't know what is the error in both SAVE and UPDATE buttons.
Here is my code:
Private Sub BtnSave_Click(sender As Object, e As EventArgs) Handles BtnSave.Click
Try
With cm
.Connection = cn
.CommandText = "insert into [edit$]values('" & TxtId.Text & "','" & TxtFamilyname.Text & "','" & TxtGivenname.Text & "','" & TxtGender.Text & "','" & TxtDob.Text & "','" & TxtExamdate.Text & "','" & TxtExamtime.Text & "','" & TxtStreet.Text & "','" & TxtHouse.Text & "','" & TxtPlz.Text & "','" & TxtCity & "' )"
.ExecuteNonQueryAsync()
End With
FillDataGridView("select * from [edit$]")
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.Critical, Text)
Return
End Try
MsgBox("succefully Saved!", MsgBoxStyle.Information, Text)
End Sub
Private Sub BtnUpdate_Click(sender As Object, e As EventArgs) Handles Btnupdate.Click
Try
With cm
.Connection = cn
.CommandText = "Update [edit$] set [Family Name] = '" & TxtFamilyname.Text & "' where id ='" & TxtId.Text & "' and Given Name = '" & TxtGivenname.Text & "'and Gender = '" & TxtGender.Text & "'and DOB = '" & TxtDob.Text & "'and Exam Date'" & TxtExamdate.Text & "'and Exam Time = '" & TxtExamtime.Text & "'and Street Name = '" & TxtStreet.Text & "'and House Nr = '" & TxtHouse.Text & "'and PLZ = '" & TxtPlz.Text & "'and CITY = '" & TxtCity & "'"
.ExecuteNonQuery()
End With
FillDataGridView("select * from [edit$]")
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.Information, Text)
Return
End Try
MsgBox("Succesfully updated!", MsgBoxStyle.Information, Text)
End Sub
The answer is probably very easy:
You didn't call the Text-Property every time you used a TextBox value.
Look at the TxtCity variable:
"'and CITY = '" & TxtCity & "'"
should be
"'and CITY = '" & TxtCity.Text & "'"

Error message in UPDATE statement: Syntax error (missing operator) in query expression

I don't know why when ever I click on the update button, I get an error
Syntax error in UPDATE statement
I have no idea what's going wrong in my code
This is my code:
Public Class Form1
Private Function vld(ByVal ParamArray ctl() As Object) As Boolean
For i As Integer = 0 To UBound(ctl)
If ctl(i).text = "" Then
ErrorProvider1.SetError(ctl(i), ctl(i).tag)
Return False
Exit For
End If
Next
Return True
End Function
Dim cn As New OleDbConnection
Dim cm As New OleDbCommand
Dim da As OleDbDataAdapter
Dim dt As New DataTable
Private Sub Form1_FormClosing(sender As Object, e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
cn.Close()
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
TxtExamtime.Format = DateTimePickerFormat.Custom
TxtExamtime.CustomFormat = "hh:MM tt"
cn.ConnectionString = "provider=microsoft.jet.oledb.4.0; Data Source=C:\psave\New folder\save.xls;Extended Properties=Excel 8.0;"
cn.Open()
FillDataGridView("select ID, Family Name, Given Name, Gender, DOB, Exam Date, Exam Time, Street Name, House Nr, PLZ, City from [edit$]")
End Sub
Private Sub FillDataGridView(ByVal Query As String)
da = New OleDbDataAdapter(Query, cn)
dt.Clear()
da.Fill(dt)
With DataGridView1
.DataSource = dt
.Columns(0).HeaderText = "ID"
.Columns(1).HeaderText = "Family Name"
.Columns(2).HeaderText = "Given Name"
.Columns(3).HeaderText = "Gender"
.Columns(4).HeaderText = "DOB"
.Columns(5).HeaderText = "Exam Date"
.Columns(6).HeaderText = "Exam Time"
.Columns(7).HeaderText = "Street Name"
.Columns(8).HeaderText = "House Nr"
.Columns(9).HeaderText = "PLZ"
.Columns(10).HeaderText = "City"
.Columns(10).AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill
End With
End Sub
Private Sub BtnSearch_Click(sender As Object, e As EventArgs) Handles BtnSearch.Click
Try
FillDataGridView("select * from [edit$] where ID='" & TxtId.Text & "'")
TxtFamilyname.Text = dt.Rows(0).Item(1)
TxtGivenname.Text = dt.Rows(0).Item(2)
TxtGender.Text = dt.Rows(0).Item(3)
TxtDob.Text = dt.Rows(0).Item(4)
TxtExamdate.Text = dt.Rows(0).Item(5)
TxtExamtime.Text = dt.Rows(0).Item(6)
TxtStreet.Text = dt.Rows(0).Item(7)
TxtHouse.Text = dt.Rows(0).Item(8)
TxtPlz.Text = dt.Rows(0).Item(9)
TxtCity.Text = dt.Rows(0).Item(10)
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.Critical, Text)
End Try
End Sub
Private Sub BtnSave_Click(sender As Object, e As EventArgs) Handles BtnSave.Click
If vld(TxtId, TxtFamilyname, TxtGivenname, TxtGender, TxtDob, TxtExamdate, TxtExamtime, TxtStreet, TxtHouse, TxtPlz, TxtCity) = False Then
Exit Sub
Else
End If
Try
With cm
.Connection = cn
.CommandText = "insert into [edit$]values('" & TxtId.Text & "','" & TxtFamilyname.Text & "','" & TxtGivenname.Text & "','" & TxtGender.Text & "','" & TxtDob.Text & "','" & TxtExamdate.Text & "','" & TxtExamtime.Text & "','" & TxtStreet.Text & "','" & TxtHouse.Text & "','" & TxtPlz.Text & "','" & TxtCity.Text & "' )"
.ExecuteNonQuery()
End With
FillDataGridView("select * from [edit$]")
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.Critical, Text)
Return
End Try
MsgBox("succefully Saved!", MsgBoxStyle.Information, Text)
End Sub
Private Sub BtnUpdate_Click(sender As Object, e As EventArgs) Handles Btnupdate.Click
Try
With cm
.Connection = cn
.CommandText = "Update [edit$] set [Family Name] = '" & TxtFamilyname.Text & "' where [ID] ='" & TxtId.Text & "' and [Given Name] = '" & TxtGivenname.Text & "' and [Gender] = '" & TxtGender.Text & "'and [DOB] = '" & TxtDob.Text & "'and [Exam Date]'" & TxtExamdate.Text & "'and [Exam Time] = '" & TxtExamtime.Text & "'and [Street Name] = '" & TxtStreet.Text & "'and [House Nr] = '" & TxtHouse.Text & "'and [PLZ] = '" & TxtPlz.Text & "'and [CITY] = '" & TxtCity.Text & "'"
.ExecuteNonQuery()
End With
FillDataGridView("select * from [edit$]")
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.Information, Text)
Return
End Try
MsgBox("Succesfully updated!", MsgBoxStyle.Information, Text)
End Sub
Private Sub BtnClose_Click(sender As Object, e As EventArgs) Handles BtnClose.Click
Close()
End Sub
Private Sub BtnClear_Click(sender As Object, e As EventArgs) Handles BtnClear.Click
TxtId.Clear()
TxtFamilyname.Clear()
TxtGivenname.Clear()
TxtStreet.Clear()
TxtHouse.Clear()
TxtPlz.Clear()
TxtCity.Clear()
'To see all the data in DataGridView
FillDataGridView("select * from[edit$]")
End Sub
Private Sub BtnDelete_Click(sender As Object, e As EventArgs) Handles BtnDelete.Click
Try
With cm
.Connection = cn
.CommandText = "Delete from [edit$] where [Family Name] = '" & TxtFamilyname.Text & "' and [ID] ='" & TxtId.Text & "' and [Given Name] = '" & TxtGivenname.Text & "'and [Gender] = '" & TxtGender.Text & "'and [DOB] = '" & TxtDob.Text & "'and [Exam Date]'" & TxtExamdate.Text & "'and [Exam Time] = '" & TxtExamtime.Text & "'and [Street Name] = '" & TxtStreet.Text & "'and [House Nr] = '" & TxtHouse.Text & "'and [PLZ] = '" & TxtPlz.Text & "'and [CITY] = '" & TxtCity.Text & "'"
.ExecuteNonQuery()
End With
MsgBox("Succesfully Deleted!", MsgBoxStyle.Information, Text)
FillDataGridView("select * from [edit$]")
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.Information, Text)
End Try
End Sub
End Class
See if this helps you:
.CommandText = "Update [edit$] set [Family Name] = '" & TxtFamilyname.Text & "' where ID = '" & TxtId.Text & "' and [Given Name] = '" & TxtGivenname.Text & "' and Gender = '" & TxtGender.Text & "' and DOB = '" & TxtDob.Text & "' and [Exam Date] = '" & TxtExamdate.Text & "' and [Exam Time] = '" & TxtExamtime.Text & "' and [Street Name] = '" & TxtStreet.Text & "' and [House Nr] = '" & TxtHouse.Text & "' and PLZ = '" & TxtPlz.Text & "' and CITY = '" & TxtCity.Text & "'"
Changes I made:
Added spaces in between values and the AND keyword (for example, TxtExamdate.Text & "'and [Exam Time] needs a space before "and")
Added missing equal signs between field names and values (for example, and [Exam Date]'" & TxtExamdate.Text should look more like and [Exam Date] = '" & TxtExamdate.Text)
Got rid of extraneous brackets
If you still have problems, consider that you are inserting strings (which could potentially contain any printable character (and non-printable characters!) from textboxes, directly into your statement. If these strings contain illegal characters (for example, single or double quotes) they will change the meaning of your UPDATE statement, and possibly invalidate the syntax. It has been suggested before (in the other question) that you should use parametrized queries. Finally, you may get a Data Type Mismatch error if any of your table columns are not strings (like "ID"), because you're sending them all as strings (enclosed in double quotes).
And please, only post a question once, from a single account.