databases and VB

sakaveli

New member
Joined
Oct 16, 2009
Messages
2
Programming Experience
5-10
Hi guys, i have some code that i am trying to learn how to read and write to an access database from but whenever i save the file the program stops working and says the dr.close() command is not valid. is this a problem with my work network settings or with the code?

VB.NET:
Imports System.Data.OleDb
Public Class Form1
    Inherits System.Windows.Forms.Form
    Dim cn As OleDbConnection
    Dim cmd As OleDbCommand
    Dim dr As OleDbDataReader
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As  _
    System.EventArgs) Handles MyBase.Load
    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)
            dr = cmd.ExecuteReader
            While dr.Read()
                TextBox1.Text = dr(0)
                TextBox2.Text = dr(1)
                TextBox3.Text = dr(2)
                ' loading data into TextBoxes by column index
            End While
        Catch
        End Try
        dr.Close()
        cn.Close()
    End Sub
End Class

please help!
 
Thanks for the reply, but I still get the same error:

"Object reference not set to an instance of an object."

Im not sure why it should work when not saved and then give this error only after its been saved!
 
Back
Top