Reputation: 3566
I am trying to insert into Microsoft Access database from VB.net. I can read from the Data Base(can see in Grid View) but cannot insert into Database.I don't know where the problem is. This is what i am doing:
Imports System.Data
Imports System.Data.OleDb
Public Class Form1
Public ctr As Integer
Dim bm As BindingManagerBase
Dim dr As DataRow, dt As DataTable
Dim flag As Integer, tid As Integer, tname As String
Dim totalrow As Integer, currentrow As Integer
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'TODO: This line of code loads data into the 'ShowroomDataSet.customer' table. You can move, or remove it, as needed.
bm = Me.BindingContext(ShowroomDataSet, "customer")
bm.Position = 0
Me.CustomerTableAdapter.Fill(Me.ShowroomDataSet.customer)
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim x As Integer
Dim y As String
bm.Position = bm.Count - 1
x = ShowroomDataSet.customer(bm.Position).cid
y = ShowroomDataSet.customer(bm.Position).cname
TextBox1.Text = x
TextBox2.Text = y
End Sub
Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
Close()
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
'save
'enable all buttons except save
If flag = 1 Then
dt = ShowroomDataSet.Tables("customer")
dr = dt.NewRow()
dr!cid = Val(TextBox1.Text)
dr!cname = TextBox2.Text
dt.Rows.Add(dr)
End If
If flag = 2 Then
dr.Delete()
End If
If flag = 3 Then
dt = ShowroomDataSet.Tables("customer")
dr = dt.Rows.Find(tid)
dr.BeginEdit()
dr!cid = Val(TextBox1.Text)
dr!name = TextBox2.Text
dr.EndEdit()
End If
'Me.CustomerTableAdapter.Update(Me.ShowroomDataSet.customer)
'Me.CustomerTableAdapter.Fill(Me.ShowroomDataSet.customer)
flag = 0
Button1.Enabled = True
Button2.Enabled = False
Button3.Enabled = True
Button4.Enabled = True
End Sub
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
'add
'disable all buttons except save
Dim len As Integer
TextBox1.Text = " "
TextBox2.Text = " "
flag = 1
TextBox1.Focus()
Button1.Enabled = False
Button2.Enabled = True
Button3.Enabled = False
Button4.Enabled = False
dt = ShowroomDataSet.Tables("customer")
len = dt.Rows.Count - 1
dr = dt.Rows(len)
End Sub
End Class
No errors or faults. Please help.
Upvotes: 0
Views: 784
Reputation: 59
If you are just looking to insert data into an access database use the database table adapter insert method, followed by the fill method of the table adapter to fill the a table in the dataset. See following example
me.tableadapter.insert(me.1stFieldTextbox.text, me.2ndFieldTextBox.Text....) me.tableadapter.fill(me.Dataset.Table)
If you get an error, it could be because your data types do not match, you try to insert data into an auto increament field or one which has a calculated data type.
Upvotes: 1