Adding Records into Access
Hello!
I am new to VB.NET.
I am trying to add records into Microsoft access from VB. using this code!
Quote:
Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click
Me.CustomerBindingSource.EndEdit()
Me.CustomerTableAdapter.Update(IKEADataSet.Customer)
Dim pos = Me.CustomerBindingSource.Position
Me.CustomerTableAdapter.Fill(Me.IKEADataSet.Customer)
Me.CustomerBindingSource.Position = pos
MsgBox("New Record added to the Database correctly.")
Me.CustomerBindingSource.EndEdit()
End Sub
It says the record has added but when i look into the data file...the records in to there!Please help!
Thanks!
Re: Adding Records into Access
As there appears to be no condition to be met, the success message box will always show irrespective of the result. I have to say that that code looks very unlikely to succeed to me but I confess to not being particularly au fait with design time bindings so I could be wrong.
Re: Adding Records into Access
thanks for replying... i tried a new coding
but its says "data.mdb" couldnt be found when i have stored my access file inside the bin folder of the program
Quote:
Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
Dim cmd As New OleDb.OleDbCommand
If Not cnn.State = ConnectionState.Open Then
cnn.Open()
End If
cmd.Connection = cnn
cmd.CommandText = "INSERT INTO student(stdid, stdname, gender, phone, address)" & _
"VALUES (" & Me.txtstdID.Text & " , '" & Me.txtStdName.Text & "', '" & _
Me.cboGender.Text & "' , '" & Me.txtPhone.Text & "' , '" & _
Me.txtAddress.Text & "')"
cmd.ExecuteNonQuery()
RefreshData()
cnn.Close()
End Sub
Quote:
Private Sub Test1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
'TODO: This line of code loads data into the 'Student1DataSet.student' table. You can move, or remove it, as needed.
cnn = New OleDb.OleDbConnection
cnn.ConnectionString = "Provider=Microsoft.Jet.Oledb.4.0; Data Source=..\data.mdb"
End Sub
Re: Adding Records into Access
That connection string doesn't look like something you should ever be using. Generally speaking, if you're using an Access database then you should add the data file to the project in the Solution Explore and, if prompted, agree to having it copied to your project folder. You should then configure the file to be copied to the output folder only when it is newer than the existing copy. You then use |DataDirectory| in your connection string to refer to the program folder that contains the data file:
Code:
cnn.ConnectionString = "Provider=Microsoft.Jet.Oledb.4.0; Data Source=|DataDirectory|\data.mdb"
For more information on managing local data files, follow the first link in my signature. Also, you should store your connection string in the application config file, not in code.
Re: Adding Records into Access
Also, don't insert values into SQL code using string concatenation. Always use parameters. To learn why and how, follow the Blog link in my signature and check out my post on Parameters In ADO.NET.
Re: Adding Records into Access