0

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.

5
  • Error message? You know that VS usually COPIES the Access file from project folder to the bind folder, which means you work on a fresh copy on each run? Behavior can be changed in properties windows for the file -> copy to output directory: NEVER Commented Nov 28, 2012 at 8:48
  • @igrimpe there is no error.The row added should be reflected in Access.But it is not.What should i do? Commented Nov 28, 2012 at 8:58
  • 1
    And you check the correct file? As I already stated: BY DEFAULT the (usually empty) Access db file is COPIED from the project directory into the sub directory where the executable is placed (/bin/debug for example). So on each run this OVERWRITES all changes made and you any changes from the last run are lost. Commented Nov 28, 2012 at 9:09
  • @igrimpe nope.It din't worked.I tried all options. Commented Nov 28, 2012 at 11:41
  • @igrimpe hey thanks...it kinda worked..but still having problem wid incompatible data types Commented Nov 29, 2012 at 5:21

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

but i think it should work fine using this code.what's the problem here?
hey thankx:) It worked,when i considered your and the comments by igrimpe thanks.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.