Click here to Skip to main content
14,238,591 members
Rate this:
Please Sign up or sign in to vote.
See more:
I am going add some data to ms access 2010 by vb.net and I am getting following error message at run time.

"A first chance exception of type 'System.Data.OleDb.OleDbException' occurred in System.Data.dll"

What I have tried:

My Code is;

Imports System.Data.OleDb
Public Class Form1

Private Sub cmdcancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdcancel.Click
        Close()
    End Sub

    Private Sub cmdadd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdadd.Click
        Try
            Dim sqlconn As New OleDb.OleDbConnection
            Dim sqlquery As New OleDb.OleDbCommand
            Dim connString As String
            connString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\Tubewell\Tubewell\Tubewell.accdb"
            sqlconn.ConnectionString = connString
            sqlquery.Connection = sqlconn
            sqlconn.Open()
            sqlquery.CommandText = "INSERT INTO Employee_Details(EmpNo,Employee Name,Status) VALUES ('" & txtempno.Text & "','" & txtempname.Text & "','" & ComboBox1.Text & "')"
            sqlquery.ExecuteNonQuery()
            sqlconn.Close()
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub
End Class
Posted
Updated 18-Aug-17 13:08pm
v2
Comments
Richard Deeming 18-Aug-17 14:49pm
   
Your code is vulnerable to SQL Injection[^]. NEVER use string concatenation to build a SQL query. ALWAYS use a parameterized query.

Everything you wanted to know about SQL injection (but were afraid to ask) | Troy Hunt[^]
How can I explain SQL injection without technical jargon? | Information Security Stack Exchange[^]
Query Parameterization Cheat Sheet | OWASP[^]

Fix that, and it will probably fix the OleDbException as well.
Michael_Davies 18-Aug-17 15:10pm
   
Is your project 64 or 32 bit?

Member 13363527 19-Aug-17 13:09pm
   
64bit
Michael_Davies 19-Aug-17 13:14pm
   
Set it to 32 bit and see if it fixes, the first chance faults occur when resources load (in this case OLEDB), Microsoft.ACE.OLEDB.12.0 is 32 bit. Lots of articles on the subject here's one using the same for Excel; https://blogs.msdn.microsoft.com/farukcelik/2010/06/04/accessing-excel-files-on-a-x64-machine/
Rate this:
Please Sign up or sign in to vote.

Solution 1

Start by fixing the SQL Injection[^] vulnerability.

You'll also need to wrap the Employee Name field in square brackets. You should generally avoid creating columns or tables with spaces or other special characters in their name.

You should wrap the connection and command objects in Using blocks, to ensure that they're always cleaned up properly.
Private Sub cmdadd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdadd.Click
    Try
        Using sqlconn As New OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\Tubewell\Tubewell\Tubewell.accdb")
            Using sqlquery As New OleDb.OleDbCommand("INSERT INTO Employee_Details(EmpNo, [Employee Name], Status) VALUES (?, ?, ?)", sqlconn)
                sqlquery.Parameters.AddWithValue("@EmpNo", txtempno.Text)
                sqlquery.Parameters.AddWithValue("@Name", txtempname.Text)
                sqlquery.Parameters.AddWithValue("@Status", ComboBox1.Text)
                 
                sqlconn.Open()
                sqlquery.ExecuteNonQuery()
            End Using
        End Using
    
    Catch ex As Exception
        MessageBox.Show(ex.Message)
    End Try
End Sub

If you're still getting the exception, set a breakpoint on the MessageBox.Show line, and examine the full details of the exception.
   
Comments
Member 13363527 19-Aug-17 13:31pm
   
Done. It works. Thanks a lot.
Karthik_Mahalingam 21-Aug-17 7:11am
   
if works, please mark it as answer by clicking the accept button.
Rate this:
Please Sign up or sign in to vote.

Solution 2

Remove the Try/Catch, it will allow you to see exactly where is the problem and with the debugger, you will be able to inspect variables.
-----
Not a solution to your question, but another problem you have.
Never build an SQL query by concatenating strings. Sooner or later, you will do it with user inputs, and this opens door to a vulnerability named "SQL injection", it is dangerous for your database and error prone.
A single quote in a name and your program crash. If a user input a name like "Brian O'Conner" can crash your app, it is an SQL injection vulnerability, and the crash is the least of the problems, a malicious user input and it is promoted to SQL commands with all credentials.
SQL injection - Wikipedia[^]
SQL Injection[^]
SQL Injection Attacks by Example[^]
SQL Injection Prevention Cheat Sheet - OWASP[^]
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100