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)
Related
I am getting error in INSERT INTO command.
Code:
Imports System.Data.OleDb
Imports System.IO
Public Class newemployee
Dim read As String
Dim datafile As String
Dim connstring As String
Dim cmd As New OleDbCommand
Public da As New OleDbDataAdapter
Dim str As String
Public ds As New DataSet
Public ds1 As New DataSet
Public ds2 As New DataSet
Dim myconnection As OleDbConnection = New OleDbConnection
Dim er, pho As Integer
Dim Photos1() As Byte
Private Sub newemployee_Load(sender As Object, e As EventArgs) Handles MyBase.Load
read = "provider=microsoft.ace.oledb.12.0;data source="
datafile = "C:\Users\DELL\source\repos\HRIS SYSTEM\loginformdatabase\BLUESTREAM.accdb"
connstring = read & datafile
myconnection.ConnectionString = connstring
ds.Clear()
pho = 0
DateTimePicker1.Value = DateTime.Now
If myconnection.State = ConnectionState.Open Then
myconnection.Close()
End If
myconnection.Open()
er = 0
'cn.Open()
str = "select * from bsemployee"
cmd = New OleDbCommand(str, myconnection)
da.SelectCommand = cmd
da.Fill(ds, "bsemployee")
End Sub
'INSERT
Private Sub Save_Click(sender As Object, e As EventArgs) Handles Button3.Click
ds.Clear()
str = "select * from bsemployee"
cmd = New OleDbCommand(str, myconnection)
da.SelectCommand = cmd
da.Fill(ds, "bsemployee")
Dim SLNO As Integer
Dim ph As String
SLNO = ds.Tables("bsemployee").Rows.Count + 1
If SLNO >= 0 Then
ph = SLNO
Else
ph = 0
End If
If er = 0 Then
Try
cmd.Connection = myconnection
cmd.CommandText = "INSERT INTO bsemployee(SL NO,EMP SSN,PID,BSPS EMP ID,SIIM ID,FIRST NAME,LAST NAME,NAME,FATHER/HUSBAND NAME,DEPARTMENT,GENDER,DOB,AGE,CURRENT ADDRESS,PERMANENT ADDRESS,EMAIL,COUNTRY,STATE,PHONE NUMBER,PAN,ADHAR NUMER,TE DOJ,
BSPS DOJ,DIVISION,DESIGNATION,TE EMAIL,BSPS EMAIL,BSPS DESIGNATION,COST CENTER,SALARY GRADE,SHIFT,BANK ACCOUNT NO,ACCOUNT CODE,BSPS PF NO,1ST LEVEL SUPERVISOR,2ND LEVEL SUPERVISOR,MANGER,OFFER LETTER ISSUE DATE,NOTICE PERIOD,BOND SIGN,NO OF YEARS,REPORTING,
BG VERIFICATION STATUS,DATE OF RESIGNATION,BASIC SALARY,HOUSE RENT ALLOWANCE,PROVIDENT FUND(Employer distribution),LEAVE TRAVEL ALLOWANCE,MEDICAL ALLOWANCE,SPECIAL ALLOWANCE,STAT BONUS,ESI,MONTHLY GAURENTED CASH,ANNUAL BASIC SALARY,ANNUAL HOUSE RENT ALLOWANCE,
ANNUAL PROVIDENT FUND,ANNUAL LEAVE TRAVEL ALLOWANCE,ANNUAL MEDICAL ALLOWANCE,ANNUAL SPECIAL ALLOWANCE,ANNUAL STAT ALLOWANCE,ANNUAL ESI,ANNUAL GARUNTEED CASH,FOOD COUPON PER ANNUM,GRATIUTY PER ANNUM,INSURANCE PREMIUM PER ANNUM,ANNUAL BONUS,TOTAL COST BENIFIT,GROSS COMPENSATION)
VALUES ('" & TextBox1.Text & "', '" & TextBox2.Text & "','" & TextBox3.Text & "','" & TextBox4.Text & "','" & TextBox5.Text & "','" & TextBox6.Text & "','" & TextBox7.Text & "''" & TextBox8.Text & "','" & TextBox9.Text & "','" & TextBox10.Text & "','" & ComboBox1.Text & "','" & DateTimePicker1.Text & "',
'" & TextBox11.Text & "','" & TextBox12.Text & "','" & TextBox13.Text & "','" & TextBox14.Text & "','" & ComboBox2.Text & "','" & TextBox15.Text & "','" & TextBox16.Text & "','" & TextBox17.Text & "','" & TextBox18.Text & "','" & DateTimePicker2.Text & "','" & DateTimePicker3.Text & "','" & TextBox20.Text & "',
'" & TextBox21.Text & "','" & TextBox22.Text & "','" & TextBox23.Text & "','" & TextBox24.Text & "','" & ComboBox3.Text & "','" & TextBox26.Text & "','" & TextBox26.Text & "','" & ComboBox4.Text & "','" & TextBox25.Text & "','" & TextBox27.Text & "','" & ComboBox13.Text & "','" & ComboBox5.Text & "','" & ComboBox6.Text & "','" & ComboBox7.Text & "',
'" & DateTimePicker4.Text & "','" & ComboBox8.Text & "','" & ComboBox9.Text & "','" & ComboBox10.Text & "','" & ComboBox11.Text & "','" & ComboBox12.Text & "','" & DateTimePicker5.Text & "','" & TextBox28.Text & "','" & TextBox29.Text & "','" & TextBox30.Text & "','" & TextBox31.Text & "','" & TextBox32.Text & "','" & TextBox33.Text & "',
'" & TextBox34.Text & "','" & TextBox35.Text & "','" & TextBox36.Text & "','" & TextBox37.Text & "','" & TextBox38.Text & "','" & TextBox39.Text & "','" & TextBox40.Text & "','" & TextBox41.Text & "','" & TextBox42.Text & "','" & TextBox43.Text & "','" & TextBox44.Text & "','" & TextBox45.Text & "','" & TextBox46.Text & "','" & TextBox47.Text & "',
'" & TextBox48.Text & "','" & TextBox49.Text & "','" & TextBox50.Text & "','" & TextBox51.Text & "')"
cmd.ExecuteNonQuery() 'if command is executed'
If pho = 1 Then
'Photo Saving
cmd = New OleDbCommand
cmd.CommandText = " INSERT INTO Photos VALUE (#PhID, #EID, #EName, #EMPImage) "
cmd.Parameters.AddWithValue("#PhID", ph)
cmd.Parameters.AddWithValue("#EID", TextBox1.Text)
cmd.Parameters.AddWithValue("#EName", TextBox8.Text)
Photos1 = File.ReadAllBytes(OpenFileDialog1.FileName)
cmd.Parameters.AddWithValue("#EMPImage", Photos1)
cmd.Connection = myconnection
cmd.ExecuteNonQuery()
End If
'MsgBox("New Class Added.")
Dim result As Integer = MessageBox.Show("New Employee Added. Want To Add Another One.", "Added", MessageBoxButtons.YesNo)
If result = DialogResult.No Then
Me.Close()
ElseIf result = DialogResult.Yes Then
ds.Clear()
TextBox1.Clear()
TextBox2.Clear()
TextBox3.Clear()
TextBox4.Clear()
TextBox5.Clear()
TextBox6.Clear()
TextBox7.Clear()
TextBox8.Clear()
TextBox9.Clear()
TextBox10.Clear()
TextBox11.Clear()
TextBox12.Clear()
TextBox13.Clear()
TextBox14.Clear()
TextBox15.Clear()
TextBox16.Clear()
TextBox17.Clear()
TextBox18.Clear()
TextBox20.Clear()
TextBox21.Clear()
TextBox22.Clear()
TextBox23.Clear()
TextBox24.Clear()
TextBox25.Clear()
TextBox26.Clear()
TextBox27.Clear()
TextBox28.Clear()
TextBox29.Clear()
TextBox30.Clear()
TextBox31.Clear()
TextBox32.Clear()
TextBox33.Clear()
TextBox34.Clear()
TextBox35.Clear()
TextBox36.Clear()
TextBox37.Clear()
TextBox38.Clear()
TextBox39.Clear()
TextBox40.Clear()
TextBox41.Clear()
TextBox42.Clear()
TextBox43.Clear()
TextBox44.Clear()
TextBox45.Clear()
TextBox46.Clear()
TextBox47.Clear()
TextBox48.Clear()
TextBox49.Clear()
TextBox50.Clear()
TextBox51.Clear()
ComboBox1.ResetText()
ComboBox2.ResetText()
ComboBox3.ResetText()
ComboBox4.ResetText()
ComboBox5.ResetText()
ComboBox6.ResetText()
ComboBox7.ResetText()
ComboBox8.ResetText()
ComboBox9.ResetText()
ComboBox9.ResetText()
ComboBox10.ResetText()
ComboBox11.ResetText()
ComboBox12.ResetText()
ComboBox13.ResetText()
DateTimePicker1.ResetText()
DateTimePicker2.ResetText()
DateTimePicker3.ResetText()
DateTimePicker4.ResetText()
DateTimePicker5.ResetText()
str = "select * from bsemployee"
cmd = New OleDbCommand(str, myconnection)
da.SelectCommand = cmd
da.Fill(ds, "bsemployee")
SLNO = ds.Tables("bsemployee").Rows.Count + 1
If SLNO >= 0 Then
TextBox1.Text = SLNO
Else
TextBox1.Text = 0
End If
End If
Catch ex As Exception
MsgBox(ex.Message)
End Try
'insert close
End If
'myconnection close
End Sub
The first place I'd look would be for any text with an apostrophe in it - e.g. a surname like O'Rourke - that'll mess up your commandtext. Also, are the date formats compatible with the database date formats - i.e. UK format dates like 25/02/2019 aren't well like by US-centric databases. Similar situation with any numeric formats - might there be currency symbols in the text box?
I'm using Visual Basic (VB 2010).
How do I insert multiple records to the database using MS Access 2007? My code is not working:
Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
If DataGridView1.Rows(0).Cells(0).Value = "" Then
MsgBox("Belum ada transaksi", MsgBoxStyle.Exclamation, "Informasi")
Exit Sub
End If
If TextBox6.Text = "" Then
MsgBox("Jumlah bayar belum diinput!", MsgBoxStyle.Exclamation, "Informasi")
Exit Sub
End If
On Error Resume Next
If RadioButton1.Checked Then
For baris As Integer = 0 To DataGridView1.Rows.Count - 2
Dim simpan As String = "Insert into TBL_JUALTUNAI (NomorFaktur,TglTransaksi,WaktuTransaksi,KodeBarang,NamaBarang,HargaSatuan,JumlahBeli,Total) values " & _
"('" & TextBox10.Text & "','" & TextBox11.Text & "','" & TextBox12.Text & "','" & DataGridView1.Rows(0).Cells(0).Value & "','" & DataGridView1.Rows(0).Cells(1).Value & "','" & DataGridView1.Rows(0).Cells(2).Value & "','" & DataGridView1.Rows(0).Cells(3).Value & "','" & DataGridView1.Rows(0).Cells(4).Value & "')"
CMD = New OleDbCommand(simpan, CONN)
CMD.ExecuteNonQuery()
Next baris
End If
If RadioButton2.Checked Then
Dim simpan1 As String = "Insert into TBL_PELANGGAN (NomorFaktur,TglTransaksi,WaktuTransaksi,NamaPelanggan,AlamatPelanggan,TelpPelanggan,KodeBarang,NamaBarang,HargaSatuan,JumlahBeli,Total) values " & _
"('" & TextBox10.Text & "','" & TextBox11.Text & "','" & TextBox12.Text & "','" & DataGridView1.Rows(0).Cells(7).Value & "','" & DataGridView1.Rows(0).Cells(8).Value & "','" & DataGridView1.Rows(0).Cells(9).Value & "','" & DataGridView1.Rows(0).Cells(0).Value & "','" & DataGridView1.Rows(0).Cells(1).Value & "','" & DataGridView1.Rows(0).Cells(2).Value & "','" & DataGridView1.Rows(0).Cells(3).Value & "','" & DataGridView1.Rows(0).Cells(4).Value & "')"
CMD = New OleDbCommand(simpan1, CONN)
CMD.ExecuteNonQuery()
End If
CMD = New OleDbCommand("select * from TBL_BARANG where KodeBarang='" & DataGridView1.Rows(0).Cells(0).Value & "'", CONN)
RD = CMD.ExecuteReader
RD.Read()
If RD.HasRows Then
Dim kurangistok As String = "update TBL_BARANG set StockBarang= '" & RD.Item(4) - DataGridView1.Rows(0).Cells(3).Value & "' where KodeBarang='" & DataGridView1.Rows(0).Cells(0).Value & "'"
CMD = New OleDbCommand(kurangistok, CONN)
CMD.ExecuteNonQuery()
End If
End Sub
End Class
With that code, you aren't going to insert multiple records at once. Each INSERT command will only insert one row, so you have to use multiple INSERT commands for multiple rows.
If I'm understanding you correctly, you have a DataGridView where there multiple rows, and you want to insert the selected rows, yes?
Correct me if I'm wrong, but what you should be doing is enumerating through the DataGridView's rows and insert a row to the database for each row in that DataGridView?
In that case, use a For Each loop to retrieve the rows (DataGridView1.Rows) and then execute your INSERT command in that loop.
Edit: In reply to your comment, I don't know the full context of your code, but I can get you started.
For Each dgvRow As DataGridViewRow In dgv.Rows
Dim myCommand As String = "INSERT INTO " 'From here, insert your parameters for that row. You can call on dgvRow.Cells.
CMD = New OleDbCommand(myCommand, CONN)
CMD.ExecuteNonQuery()
Next
I'm using Microsoft Visual Studio 2010 Express and I'm trying to make a enrollment form using VB.NET. This is my code so far:
Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
Dim cardStr As String
Dim formStr As String
Dim birthStr As String
Dim goodmoralStr As String
If cbcard.Checked Then
cardStr = "OK"
Else
cardStr = ""
End If
If cbform.Checked Then
formStr = "OK"
Else
formStr = ""
End If
If cbbirth.Checked Then
birthStr = "OK"
Else
birthStr = ""
End If
If cbgoodmoral.Checked Then
goodmoralStr = "OK"
Else
goodmoralStr = ""
End If
Dim cmd As New OleDbCommand
Dim conn As New OleDbConnection(conStr)
conn.Open()
cmd.Connection = conn
cmd.CommandType = CommandType.Text
cmd.CommandText = "insert into tblEnroll(StudID,StudLevel,StudFName,StudMName,StudLName,StudGender,StudBirthday,StudNationality,StudContact,StudPOB,StudCitizenship,StudReligion,MomName,MomContact,MomOccupation,DadName,DadContact,DadOccupation,PrevSchool,Card,F137,BirthCertificate) values ('" & txtID.Text & "','" & lbGrade.Text & "','" & txtFName.Text & "','" & txtMName.Text & "','" & txtLName.Text & "','" & lbGender.Text & "','" & dtpBirthDate.Text & "','" & txtNationality.Text & "','" & txtStudContact.Text & "','" & txtPOB.Text & "','" & txtCitizen.Text & "','" & txtReligion.Text & "','" & txtMom.Text & "','" & txtMomContact.Text & "','" & txtMomOccupation.Text & "','" & txtDad.Text & "','" & txtDadContact.Text & "','" & txtDadOccupation.Text & "','" & txtPrevSchool.Text & "','" & cardStr & "','" & formStr & "','" & birthStr & "','" & goodmoralStr & "')"
cmd.ExecuteNonQuery()
conn.Close()
MessageBox.Show("Student Successfully Enrolled!")`
What could be the solution here?
You have one too many columns in your INSERT statement values. The value goodmoralStr does not have a corresponding column to insert into.
As a slight aside, you really should use parameterised SQL in your code to avoid issues with SQL injection.
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 & "'"
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.