Hi all,

Im trying to connect my Emp.mdb database to my VB.net application.

with this code.

Imports System.Data.OleDb
Public Class Form1 Inherits System.Windows.Forms.Form
Dim cn As OleDbConnection
Dim cmd As OleDbCommand
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e as _
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As_
System.EventArgs) Handles Button1.Click
Try
cn = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;_
Data Source=C:\emp.mdb;")
'provider to be used when working with access database
cn.Open()
cmd = New OleDbCommand("select * from table1", cn)
TextBox1.Text = dr(0)
TextBox2.Text = dr(1)
TextBox3.Text = dr(2)
End While
Catch
End Try
dr.Close()
cn.Close()
End Sub
End Class

I had sorted all of the 30 errors!! but now when i try and execute the project and simply retrieve 3 database values to the three textboxes there's an error with the dr.Close() stating that the nullreferenceexception was unhandled???

Cheers,

The Irishman!

2
Contributors
1
4
Views
9 Years
Discussion Span
Last Post by Chris147

Hi,

Try using dr = Nothing instead of dr.Close()

You're getting the error because you have cmd set to the "New" keyword and not dr (so cmd is an Object whereas dr is a Variable).

HTH,

Chris.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.