Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

I'm new at using vb.net to talk to databases and have been trying to follow examples that i have seen from other members. I'm trying to keep it simple by connecting to an access database but for some reason i always get this error when trying to run this code.

 

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

 

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
       Dim MyConnection As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=\\ares\mis\data.mdb")
       MyConnection.Open()
       Dim MyCommand As New OleDbCommand("SELECT * FROM MyTable WHERE ID = 1", MyConnection)
       Dim MyReader As OleDbDataReader = MyCommand.ExecuteReader()
       While MyReader.Read
           Lastname.Text = MyReader("MyFName")
           FirstName.Text = MyReader("MyLName")
           Address.Text = MyReader("MyAddress")
       End While
       MyConnection.Close()
       MyReader.Close()
       MyCommand.Dispose()
   End Sub

 

It errors out on this line

Dim MyReader As OleDbDataReader = MyCommand.ExecuteReader()

 

I've looked everywhere but haven't found a solution.

Help please

Posted

Do this, and see what it tells you.

 



Dim MyConnection As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=\\ares\mis\data.mdb")
MyConnection.Open()
Dim MyCommand As New OleDbCommand("SELECT * FROM MyTable WHERE ID = 1", MyConnection)

Try
Dim MyReader As OleDbDataReader = MyCommand.ExecuteReader()
Catch ex1 As System.Data.OleDb.OleDbException
MessageBox.Show(ex1.Message)
Catch ex2 As Exception
MessageBox.Show(ex2.Message)
End Try

While MyReader.Read
Lastname.Text = MyReader("MyFName")
FirstName.Text = MyReader("MyLName")
Address.Text = MyReader("MyAddress")
End While
MyConnection.Close()
MyReader.Close()
MyCommand.Dispose()

[/Code]

Posted

Woops, shouldn't Dim in the Try block....

 



Dim MyConnection As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=\\ares\mis\data.mdb")
MyConnection.Open()
Dim MyCommand As New OleDbCommand("SELECT * FROM MyTable WHERE ID = 1", MyConnection)

Dim MyReader As OleDbDataReader

Try
MyReader = MyCommand.ExecuteReader()
Catch ex1 As System.Data.OleDb.OleDbException
MessageBox.Show(ex1.Message)
Catch ex2 As Exception
MessageBox.Show(ex2.Message)
End Try

While MyReader.Read
Lastname.Text = MyReader("MyFName")
FirstName.Text = MyReader("MyLName")
Address.Text = MyReader("MyAddress")
End While
MyConnection.Close()
MyReader.Close()
MyCommand.Dispose()


[/Code]

 

Sorry....

Posted

MSGBOX

No Value Given for one or more required parameters.

 

MDE Error

An unhandled exception of type 'System.NullReferenceException' occurred in AccessDB.exe

 

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

 

 

This is a pain in the behind...

 

Thanks for your help

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...