Ahmad Asjad
Ahmad Asjad

Reputation: 823

vb.net data not inserting into ms access database

This is my form snapshot

enter image description here

This is my code to insert data into data base

Imports System.Data.OleDb

Public Class Test
    Dim cnn As New OleDb.OleDbConnection
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.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 Test(ID, Test) " & _
                        " VALUES(" & Me.TextBox1.Text & ",'" & Me.TextBox2.Text & "')"
        cmd.ExecuteNonQuery()
        cnn.Close()
    End Sub

    Private Sub Test_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        cnn = New OleDb.OleDbConnection
        cnn.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\OfficeAutomationSystem.accdb; Persist Security Info=False"
    End Sub
End Class

My database name is : OfficeAutomationSystem.accdb , Table Name is: Test , Table Structure is as follows:

FieldName        DataType
ID               Number
Test             Text

Code is running successfully, and giving no error. When I see in database, there was no record found in that

What's the error? I'm unable to find it. So Please, Help me. Thanks in advance

Upvotes: 0

Views: 7616

Answers (2)

Malek
Malek

Reputation: 25

Dim cnn = New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & Application.StartupPath & "\OfficeAutomationSystem.accdb"

Upvotes: 1

ɐsɹǝʌ ǝɔıʌ
ɐsɹǝʌ ǝɔıʌ

Reputation: 4512

Sometimes Data Source=|DataDirectory|\... is problematic when debugging. Please bear on mind that you'll have another database in \bin\debug at your project folder when you are debugging your code. Probably you're updating the records in this database instead the original one.

Try to set an absolute path and check if the records are being updated.

Upvotes: 4

Related Questions