Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

I'm getting errors when I try to do what (I think) is a pretty simple procedure: namely, retrieving records from an Access database with the OleDbDataReader object.

 

Here's my code:

 

Dim sPath As String = Server.MapPath("MyDb.mdb")
Dim sConn As String = "Provider=Microsoft.Jet.OLEDB.4.0; DATA Source=" & sPath & ";"
Dim sSql As String = "SELECT * FROM MyTable"
Dim oConn As OleDbConnection
Dim oCmd As OleDbCommand
Dim oDr As OleDbDataReader

oConn = New OleDbConnection(sConn)
oConn.Open()
oCmd = New OleDbCommand(sSql, oConn)
oDr = oCmd.ExecuteReader(CommandBehavior.CloseConnection)

While oDr.Read
  ... etc. ...
End While

oDr.Close()
oConn.Close()

 

The error I get is:

 

Exception Details: System.Data.OleDb.OleDbException: No value given for one or more required parameters.

 

It's complaining about the line that reads:

 

oDr = oCmd.ExecuteReader(CommandBehavior.CloseConnection)

 

Does anyone see any problems with the code I've written (it looks letter-perfect to me, but then I've been staring at my screen all day ;))?

 

Advice is appreciated...

Posted

Thanks for the quick response. Maybe I didn't follow you, but I get the same error even when I execute:

 

oConn = New OleDbConnection(sConn)
oConn.Open()
oCmd = New OleDbCommand(sSql, oConn)
oDr = oCmd.ExecuteReader()

While oDr.Read()
  ... etc ...

 

Is there some explicit syntax I'm missing?

Posted

If found this code sample -->

 

Dim myConnection As New OleDbConnection(myConnectionString)

Dim myCommand As New OleDbCommand(mySelectQuery, myConnection)

myConnection.Open()

Dim myReader As OleDbDataReader = myCommand.ExecuteReader(CommandBehavior.CloseConnection)

While myReader.Read()

Console.WriteLine(myReader.GetString(0))

End While

myReader.Close()

 

The only difference I see is that the Open() is after dimensioning the Command.

 

Also have you tried removing CommandBehavior.CloseConnection ?

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...