• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 282
  • Last Modified:

Unhandled exception of type..

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 system.data.dll

from this peice of code:

m_daDataAdapter.Fill(m_dtCustomers)

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()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        '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
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    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.SuspendLayout()
        '
        'txtContactName
        '
        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 = ""
        '
        'txtAddress1
        '
        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 = ""
        '
        'txtAddress2
        '
        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 = ""
        '
        'txtPostcode
        '
        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 = ""
        '
        'txtPhoneNo
        '
        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 = ""
        '
        'txtID
        '
        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 = ""
        '
        'Label1
        '
        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"
        '
        'Label2
        '
        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"
        '
        'Label3
        '
        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"
        '
        'Label4
        '
        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"
        '
        'Label5
        '
        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"
        '
        'Label6
        '
        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"
        '
        'txt
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(376, 366)
        Me.Controls.Add(Me.Label6)
        Me.Controls.Add(Me.Label5)
        Me.Controls.Add(Me.Label4)
        Me.Controls.Add(Me.Label3)
        Me.Controls.Add(Me.Label2)
        Me.Controls.Add(Me.Label1)
        Me.Controls.Add(Me.txtID)
        Me.Controls.Add(Me.txtPhoneNo)
        Me.Controls.Add(Me.txtPostcode)
        Me.Controls.Add(Me.txtAddress2)
        Me.Controls.Add(Me.txtAddress1)
        Me.Controls.Add(Me.txtContactName)
        Me.Name = "txt"
        Me.Text = "Cust"
        Me.ResumeLayout(False)

    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_cnADONetConnection.Open()

        'dataadapter
        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
        m_daDataAdapter.Fill(m_dtCustomers)

        Me.ShowCurrentRecord()

    End Sub

    Private Sub Cust_Closed(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Closed
        'Close the connection
        m_cnADONetConnection.Close()
    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 = _
            m_dtCustomers.Rows(m_rowPosition)("Name").ToString()
        txtAddress1.Text = _
            m_dtCustomers.Rows(m_rowPosition)("Address1").ToString()
        txtAddress2.Text = _
            m_dtCustomers.Rows(m_rowPosition)("Address2").ToString()
        txtPostcode.Text = _
            m_dtCustomers.Rows(m_rowPosition)("Postcode").ToString()
        txtPhoneNo.Text = _
            m_dtCustomers.Rows(m_rowPosition)("Telephone No").ToString()
        txtID.Text = _
            m_dtCustomers.Rows(m_rowPosition)("ID").ToString()
    End Sub




End Class

Can you see where I have gone wrong in trying to convert it to my database?
0
foster30
Asked:
foster30
  • 3
1 Solution
 
maralansCommented:
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?
0
 
foster30Author Commented:
The following line of code is highlighted in green...

m_daDataAdapter.Fill(m_dtStock)

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

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

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?

0
 
manchCommented:
Check the SQL Written while creating the Adapter

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

m_daDataAdapter.Fill(m_dtCustomers)

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)
0
 
foster30Author Commented:
okay thanks, I will comment back soon.
0
 
foster30Author Commented:
manch was right, my sql command was wrong, had got my tables mixed up on my forms.

thanks a lot guys.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now