VB.NET 2003 Database Access Issues

M

Materialised

Hello everyone,

Please bear with me. I have not programmed in Visual Basic since 1994. And
now my employer has given me the tedious task of writing a database driven
program.
I have a access database with 5 tables, the perticular one I am interested
in is named CUSTOMERS.

Here is a section of my code:

Inherits System.Windows.Forms.Form
Private Sub Form3_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Dim dbpath As String
Dim dbConn As OleDbConnection
Dim dbCommand As OleDbCommand
OpenFileDialog1.ShowDialog()
dbPath = OpenFileDialog1.FileName
Debug.Write(dbPath)
dbConn = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;" _
& "DATA SOURCE=" _
& dbpath)
dbCommand.CommandText = "SELECT * FROM CUSTOMERS"
dbCommand.Connection = dbConn
dbConn.Open()
Dim dbDR As OleDb.OleDbDataReader = dbCommand.ExecuteReader
While dbDR.Read
txtCustCode = dbDR("CUSCODE".ToString)
End While
End Sub

However when I run this program, I get the following error message:
An unhandled exception of type 'System.NullReferenceException' occurred in
WindowsApplication1.exe

Additional information: Object reference not set to an instance of an
object.

Could someone please be so kind as to point out where I am going wrong?

Thanks
 
P

pvdg42

Materialised said:
Hello everyone,

Please bear with me. I have not programmed in Visual Basic since 1994. And
now my employer has given me the tedious task of writing a database driven
program.
I have a access database with 5 tables, the perticular one I am interested
in is named CUSTOMERS.

Here is a section of my code:

Inherits System.Windows.Forms.Form
Private Sub Form3_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Dim dbpath As String
Dim dbConn As OleDbConnection
Dim dbCommand As OleDbCommand
OpenFileDialog1.ShowDialog()
dbPath = OpenFileDialog1.FileName
Debug.Write(dbPath)
dbConn = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;" _
& "DATA SOURCE=" _
& dbpath)
dbCommand.CommandText = "SELECT * FROM CUSTOMERS"
dbCommand.Connection = dbConn
dbConn.Open()
Dim dbDR As OleDb.OleDbDataReader = dbCommand.ExecuteReader
While dbDR.Read
txtCustCode = dbDR("CUSCODE".ToString)
End While
End Sub

However when I run this program, I get the following error message:
An unhandled exception of type 'System.NullReferenceException' occurred in
WindowsApplication1.exe

Additional information: Object reference not set to an instance of an
object.

Could someone please be so kind as to point out where I am going wrong?

Thanks
I'll make a wild guess, as you don't mention which line throws the
exception.
Is the exception thrown in the first line where you reference dbCommand,
perhaps this one?

dbCommand.CommandText = "SELECT * FROM CUSTOMERS"

Where you do create an OleDbConnection object with:

dbConn = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;" _
& "DATA SOURCE=" _
& dbpath)

You never create an OleDbCommand object.
 
K

KitWest

Here's a version that would show you the line # in the stack trace:

Private Sub Form1_Load(ByVal sender As Object, _
ByVal e As EventArgs) Handles MyBase.Load
Try
Dim dbpath As String = "c:\t\db1.mdb"
Dim dbConn As New OleDbConnection( _
"Provider=Microsoft.Jet.OLEDB.4.0;DATA SOURCE=" & dbpath)
dbConn.Open()
Dim dbCommand As New OleDbCommand( _
"SELECT * FROM Customers", dbConn)
Dim dbDR As OleDb.OleDbDataReader = dbCommand.ExecuteReader
Do While dbDR.Read
ComboBox1.Items.Add(dbDR("CUSCODE"))
Loop
Catch ex As Exception
MessageBox.Show(ex.Message & vbCrLf & ex.StackTrace, "Error")
End Try
End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top