Unhandled exception of type..

Posted on 2006-05-14
Last Modified: 2010-04-23
Hi Experts,

Iv'e been following an ADO tutorial in a SAMS teach yourself VB.NET book. The code matches mine, however I am trying to apply it to my database rather than the example db.

I am getting this error:

An unhandled exception of type 'System.Data.OleDb.OleDbException' occurred in

from this peice of code:


This is the code that I have so far...

Public Class txt
    'declare modules
    Inherits System.Windows.Forms.Form
    Private m_cnADONetConnection As New OleDb.OleDbConnection
    Private m_daDataAdapter As New OleDb.OleDbDataAdapter
    Private m_cbCommandBuilder As OleDb.OleDbCommandBuilder
    Private m_dtCustomers As New DataTable
    Private m_rowPosition As Integer = 0

#Region " Windows Form Designer generated code "

    Public Sub New()

        'This call is required by the Windows Form Designer.

        'Add any initialization after the InitializeComponent() call

    End Sub

    'Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
            End If
        End If
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    Friend WithEvents txtContactName As System.Windows.Forms.TextBox
    Friend WithEvents txtAddress1 As System.Windows.Forms.TextBox
    Friend WithEvents txtAddress2 As System.Windows.Forms.TextBox
    Friend WithEvents txtPostcode As System.Windows.Forms.TextBox
    Friend WithEvents txtPhoneNo As System.Windows.Forms.TextBox
    Friend WithEvents txtID As System.Windows.Forms.TextBox
    Friend WithEvents Label1 As System.Windows.Forms.Label
    Friend WithEvents Label2 As System.Windows.Forms.Label
    Friend WithEvents Label3 As System.Windows.Forms.Label
    Friend WithEvents Label4 As System.Windows.Forms.Label
    Friend WithEvents Label5 As System.Windows.Forms.Label
    Friend WithEvents Label6 As System.Windows.Forms.Label
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.txtContactName = New System.Windows.Forms.TextBox
        Me.txtAddress1 = New System.Windows.Forms.TextBox
        Me.txtAddress2 = New System.Windows.Forms.TextBox
        Me.txtPostcode = New System.Windows.Forms.TextBox
        Me.txtPhoneNo = New System.Windows.Forms.TextBox
        Me.txtID = New System.Windows.Forms.TextBox
        Me.Label1 = New System.Windows.Forms.Label
        Me.Label2 = New System.Windows.Forms.Label
        Me.Label3 = New System.Windows.Forms.Label
        Me.Label4 = New System.Windows.Forms.Label
        Me.Label5 = New System.Windows.Forms.Label
        Me.Label6 = New System.Windows.Forms.Label
        Me.txtContactName.Location = New System.Drawing.Point(88, 32)
        Me.txtContactName.Name = "txtContactName"
        Me.txtContactName.Size = New System.Drawing.Size(152, 20)
        Me.txtContactName.TabIndex = 0
        Me.txtContactName.Text = ""
        Me.txtAddress1.Location = New System.Drawing.Point(88, 64)
        Me.txtAddress1.Name = "txtAddress1"
        Me.txtAddress1.Size = New System.Drawing.Size(152, 20)
        Me.txtAddress1.TabIndex = 1
        Me.txtAddress1.Text = ""
        Me.txtAddress2.Location = New System.Drawing.Point(88, 96)
        Me.txtAddress2.Name = "txtAddress2"
        Me.txtAddress2.Size = New System.Drawing.Size(152, 20)
        Me.txtAddress2.TabIndex = 2
        Me.txtAddress2.Text = ""
        Me.txtPostcode.Location = New System.Drawing.Point(88, 128)
        Me.txtPostcode.Name = "txtPostcode"
        Me.txtPostcode.Size = New System.Drawing.Size(152, 20)
        Me.txtPostcode.TabIndex = 3
        Me.txtPostcode.Text = ""
        Me.txtPhoneNo.Location = New System.Drawing.Point(88, 160)
        Me.txtPhoneNo.Name = "txtPhoneNo"
        Me.txtPhoneNo.Size = New System.Drawing.Size(152, 20)
        Me.txtPhoneNo.TabIndex = 4
        Me.txtPhoneNo.Text = ""
        Me.txtID.Location = New System.Drawing.Point(88, 192)
        Me.txtID.Name = "txtID"
        Me.txtID.Size = New System.Drawing.Size(152, 20)
        Me.txtID.TabIndex = 5
        Me.txtID.Text = ""
        Me.Label1.Location = New System.Drawing.Point(24, 32)
        Me.Label1.Name = "Label1"
        Me.Label1.Size = New System.Drawing.Size(40, 32)
        Me.Label1.TabIndex = 6
        Me.Label1.Text = "Name"
        Me.Label2.Location = New System.Drawing.Point(24, 64)
        Me.Label2.Name = "Label2"
        Me.Label2.Size = New System.Drawing.Size(56, 32)
        Me.Label2.TabIndex = 7
        Me.Label2.Text = "Address1"
        Me.Label3.Location = New System.Drawing.Point(24, 96)
        Me.Label3.Name = "Label3"
        Me.Label3.Size = New System.Drawing.Size(56, 32)
        Me.Label3.TabIndex = 8
        Me.Label3.Text = "Address2"
        Me.Label4.Location = New System.Drawing.Point(24, 128)
        Me.Label4.Name = "Label4"
        Me.Label4.Size = New System.Drawing.Size(64, 32)
        Me.Label4.TabIndex = 9
        Me.Label4.Text = "Postcode"
        Me.Label5.Location = New System.Drawing.Point(24, 160)
        Me.Label5.Name = "Label5"
        Me.Label5.Size = New System.Drawing.Size(64, 32)
        Me.Label5.TabIndex = 10
        Me.Label5.Text = "Phone No"
        Me.Label6.Location = New System.Drawing.Point(24, 192)
        Me.Label6.Name = "Label6"
        Me.Label6.Size = New System.Drawing.Size(40, 32)
        Me.Label6.TabIndex = 11
        Me.Label6.Text = "ID"
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(376, 366)
        Me.Name = "txt"
        Me.Text = "Cust"

    End Sub

#End Region

    Private Sub Cust_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'run connection string
        m_cnADONetConnection.ConnectionString = _
        "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Documents and Settings\Dave\My Documents\DVDHire.mdb"

        'open connection

        m_daDataAdapter = _
            New OleDb.OleDbDataAdapter("Select * From Contacts", m_cnADONetConnection)

        'commandbuilder to handle updates adding and deleting
        m_cbCommandBuilder = New OleDb.OleDbCommandBuilder(m_daDataAdapter)

        'fill the dataset


    End Sub

    Private Sub Cust_Closed(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Closed
        'Close the connection
    End Sub

    Private Sub ShowCurrentRecord()
        If m_dtCustomers.Rows.Count = 0 Then
            txtContactName.Text = ""
            txtAddress1.Text = ""
            txtAddress2.Text = ""
            txtPostcode.Text = ""
            txtPhoneNo.Text = ""
            txtID.Text = ""
            Exit Sub
        End If

        txtContactName.Text = _
        txtAddress1.Text = _
        txtAddress2.Text = _
        txtPostcode.Text = _
        txtPhoneNo.Text = _
            m_dtCustomers.Rows(m_rowPosition)("Telephone No").ToString()
        txtID.Text = _
    End Sub

End Class

Can you see where I have gone wrong in trying to convert it to my database?
Question by:foster30
    LVL 5

    Expert Comment

    I just pasted your code in one of my apps pointing to one of my databases and it work fine.  Does the exception give you any more information?

    Author Comment

    The following line of code is highlighted in green...


    the following error appears in a msg box entitled Microsoft Development Environment

    An unhandled exception of type 'System.Data.OleDb.OleDbException' occurred in

    I am given the option to continue or break, when I continue, the debug box at the bottom appears with the following..

    at DVD.Stock.Main() in C:\Documents and Settings\Dave\My Documents\DVD\Stock.vb:line 1The program '[2916] DVD.exe' has exited with code 0 (0x0).

    I'll try building this on a different PC today, if that doesn't work I'll try changing the database (which I can access manually fine)

    I've just realised that the database and the program have the same name, could that be an issue?

    LVL 6

    Accepted Solution

    Check the SQL Written while creating the Adapter

    (Table Name is wrong or some other error related with the SQL)


    In your code you might have written the wrong SQL.
    (Probably the table name is wrong or the Table does not belong to the database
    or some other error of same kind)

    Author Comment

    okay thanks, I will comment back soon.

    Author Comment

    manch was right, my sql command was wrong, had got my tables mixed up on my forms.

    thanks a lot guys.

    Featured Post

    6 Surprising Benefits of Threat Intelligence

    All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

    Join & Write a Comment

    I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
    Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
    Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
    This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor ( If you're looking for how to monitor bandwidth using netflow or packet s…

    732 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    22 Experts available now in Live!

    Get 1:1 Help Now