Hi, I seem to be having a problem when attempting to insert a new record into my database through vb.net. From what I can gather, there seems to be an error in the code that the command builder (cBuilder) is creating for me.
Here's a copy of the code for the connection:
Private Sub btnLoadDB_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLoadDB.Click
Provider = "PROVIDER=MICROSOFT.ACE.OLEDB.12.0;"
Source = "Data Source = C:\Documents and Settings\Hp\My Documents\COMP4\COMP4 CW\WND.accdb"
connection.ConnectionString = Provider & Source
connection.Open()
sqlCode = "SELECT * FROM Customers"
dAdapter = New OleDb.OleDbDataAdapter(sqlCode, connection)
dAdapter.Fill(dSet, "Customers")
MsgBox("Database Loaded.", MsgBoxStyle.Information)
connection.Close()
maxRows = dSet.Tables("Customers").Rows.Count
incr = -1
End Sub
Here is the code for adding a new record:
If incr <> -1 Or maxRows = 0 Then
Dim cBuilder As New OleDb.OleDbCommandBuilder(dAdapter)
Dim dSetNewRow As DataRow
dSetNewRow = dSet.Tables("Customers").NewRow
dSetNewRow.Item(0)= txtCustomerID.text
dSetNewRow.Item(1) = txtTitle.Text
dSetNewRow.Item(2) = txtFirstName.Text
dSetNewRow.Item(3) = txtLastName.Text
dSetNewRow.Item(4) = txtDOB.Text
dSetNewRow.Item(5) = txtAddress.Text
dSetNewRow.Item(6)= txtTown.text
dSetNewRow.Item(7) = txtPostcode.Text
dSetNewRow.Item(8) = txtCounty.Text
dSetNewRow.Item(9) = txtTelNumber.Text
dSetNewRow.Item(10) = txtPostcode.Text
dSet.Tables("Customers").Rows.Add(dSetNewRow)
dAdapter.Update(dSet, "Customers")
MsgBox("A new record has successfully been added to the database" _
, MsgBoxStyle.Information)
end if
end sub
I would use my own SQL statements, but I'm not too sure about how to put them into my program. Any assistance would be greatly appreciated.
Thanks :)
Collin