Null Exception on OleDbDataAdapter.Fill

G

Guest

I am getting Null Exception error when I try to fill a dataset. I am
providing a value for my parameter so I'm not sure, what's going on. Please
see below.....

Thanks

Dim cmd1 As OleDbCommand = New OleDbCommand
cmd1.Parameters.Add("@adate", OleDbType.DBDate)
cmd1.Connection = mycnn
cmd1.CommandType = CommandType.Text
cmd1.CommandText = "SELECT * FROM Mytable WHERE adate = @adate"
MsgBox(cmd1.CommandText.ToString)

Dim ds As DataSet = New DataSet
Dim da As Data.OleDb.OleDbDataAdapter = New
Data.OleDb.OleDbDataAdapter
da.SelectCommand = cmd1
cmd1.Parameters("@adate").Value = #1/3/2005#
Try
da.Fill(ds, "Mytable")
Catch ex As OleDbException
MsgBox(ex.Message)
End Try
 
G

Guest

Hello Everyone,

The error I am actually getting is missing operand not a Null Exception.

Thanks,
 
L

Larry Woods

Chris,

Here you go.... Use the "?" for OleDB.

HTH,

Larry Woods

Dim cmd1 As OleDbCommand = New OleDbCommand
cmd1.Parameters.Add("@adate", OleDbType.DBDate)
cmd1.Connection = mycnn
cmd1.CommandType = CommandType.Text
'cmd1.CommandText = "SELECT * FROM Mytable WHERE adate = @adate"
cmd1.CommandText = "SELECT * FROM Mytable WHERE adate = ?"
MsgBox(cmd1.CommandText.ToString)
Dim ds As DataSet = New DataSet
Dim da As Data.OleDb.OleDbDataAdapter = New Data.OleDb.OleDbDataAdapter
da.SelectCommand = cmd1
'cmd1.Parameters("@adate").Value = #1/3/2005#
cmd1.Parameters(0).Value = #1/3/2005#
Try
da.Fill(ds, "Mytable")
Catch ex As OleDbException
MsgBox("ERROR: " & ex.Message)
End Try
For i As Integer = 0 To ds.Tables(0).Rows.Count - 1
MessageBox.Show(CType(ds.Tables(0).Rows(0)("aDate"),
DateTime).ToShortDateString)
Next
 
G

Guest

Larry,

Thanks for the help, as it turns out, I had to enclose the parameter1.value
with curly braces. So the command text ended up looking like


cmd1.CommandText = "SELECT * FROM Mytable WHERE adate = " & "{" &
@adate.value & "}"

This seems to be the only way I can get this to work, but I'm not sure why I
Had to wrap the parameter value with the curly braces.

THanks
 

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