I am doing a college assignment and have been trying to figure it out for hours, but I cant seem to get my new customer to save to the database! Please, I would really, REALLY apreciate it if you could have a look at my code, make any suggestions, or let me know more efficient ways of doing this. Bellow I provide a sample of my code for this.
To begin with, on form load I determine the new customer ID to be put into the database:
Private Sub frmRegister_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'Me.CustomerTableAdapter.Fill(Me.DatabasePizzaPalaceDataSet.Customer)
Dim conn As New System.Data.OleDb.OleDbConnection()
conn.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=H:\Year 2\Unit 17 Project Planning\Workto do\PizzaPalce\Program\DatabasePizzaPalace.accdb"
conn.Open()
Dim Rows As Integer
Dim sql As String = "SELECT * FROM Customer"
Dim da As OleDb.OleDbDataAdapter
Dim ds As New DataSet
da = New OleDb.OleDbDataAdapter(sql, conn)
da.Fill(ds, "Customer")
Rows = ds.Tables("Customer").Rows.Count
NewCustomerID.Text = Rows + 1
Customer_IDTextBox.Text = NewCustomerID.Text
conn.Close()
End Sub
Now that being said, here is the piece of code I run when clicking on my save button for the recrod to be added through a new data row.
Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
Dim conn As New System.Data.OleDb.OleDbConnection()
conn.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=H:\Year 2\Unit 17 Project Planning\Workto do\PizzaPalce\Program\DatabasePizzaPalace.accdb"
conn.Open()
Dim inc As Integer
Dim sql As String = "SELECT * FROM Customer"
Dim da As OleDb.OleDbDataAdapter
Dim ds As New DataSet
da = New OleDb.OleDbDataAdapter(sql, conn)
da.Fill(ds, "Customer")
inc = Customer_IDTextBox.Text
If inc <> -1 Then
Dim cb As New OleDb.OleDbCommandBuilder(da)
Dim dsNewRow As DataRow
dsNewRow = ds.Tables("Customer").NewRow()
dsNewRow.Item("Customer_ID") = Customer_IDTextBox.Text
dsNewRow.Item("Username_Email") = Username_EmailTextBox.Text
dsNewRow.Item("Password") = PasswordTextBox.Text
dsNewRow.Item("First_Name") = First_NameTextBox.Text
dsNewRow.Item("Surname") = SurnameTextBox.Text
dsNewRow.Item("Mobile") = MobileTextBox.Text
dsNewRow.Item("House") = HouseTextBox.Text
ds.Tables("Customer").Rows.Add(dsNewRow)
da.Update(ds, "Customer")
MsgBox("New Record added to the Database")
conn.Close()
frmLogin.Show()
End If
'Dim cb As New OleDb.OleDbCommandBuilder(da)
'Me.CustomerTableAdapter.Insert(Customer_IDTextBox.Text, Username_EmailTextBox.Text, PasswordTextBox.Text, First_NameTextBox.Text, SurnameTextBox.Text, MobileTextBox.Text, HouseTextBox.Text)
'Me.CustomerTableAdapter.Fill(Me.DatabasePizzaPalaceDataSet.Customer)
'Me.Validate()
'Me.CustomerBindingSource.EndEdit()
'Me.CustomerTableAdapter.Fill(DatabasePizzaPalaceDataSet.Customer)
'Me.TableAdapterManager.UpdateAll(Me.DatabasePizzaPalaceDataSet)
'da.Update(ds, "Customer")
'MsgBox("You have been succesfully registerd with us. Thanks!")
'conn.Close()
'frmLogin.Show()
End Sub
In comments you can also see a code provided by my teacher, which we are supposed to improve, I just wish to find a way of making this work!
Thanks a lot, all help and suggestions are very appreciated.
"Customer"is the name of the table inside your dataset? I tried your code and it worked for me. When you are debugging, is the code actually getting inside yourIF Then Elsestatement?