0

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

2
  • Which code executes Test_Load? Did you run a trace? Commented Nov 28, 2013 at 7:48
  • Why do you have single quotes around Me.TextBox2.Text, but not Me.TextBox1.Text? Also this approach leaves you vulnerable to SQL Injection, you should look into using parameters with stored procedures as I describe here: stackoverflow.com/questions/17172796/… Commented Nov 28, 2013 at 22:45

2 Answers 2

4

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.

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

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.