Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

DataReader question

Status
Not open for further replies.

777axa

Technical User
Jul 30, 2002
48
US
I'm trying to pass a Date value from DB field to a Calendar control using DataReader.

It gives me following error:
++++++++++++++++++++++++++++++++++++++++++++++++++++++
Exception Details: System.InvalidOperationException: No data exists for the row/column.

Source Error:

Line 83: .DataBind()
Line 84: End With
Line 85: calBirthDate.SelectedDate = CDate(dr("BirthDate"))
Line 86: 'Response.Write(CDate(dr("BirthDate")))
Line 87: End Sub
+++++++++++++++++++++++++++++++++++++++++++++++++++++

Here is the code.
DataGrid - gr displays data just fine
but I can't display the value of the field using response. write (dr.item("BirthDate")) - i get the same error


++++++++++++++++++++++++++++++++++++++++++++++++++++++

Private Sub rblEmpl_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rblEmpl.SelectedIndexChanged
Dim strSQL As String
Dim strConn As String
Dim dr As OleDb.OleDbDataReader

strConn = Session("ConnectString")
strSQL = "Select EmployeeID, BirthDate FROM Employees where EmployeeID = " & rblEmpl.Items(rblEmpl.SelectedIndex).Value

dr = DataHandler.GetDataReader(strSQL, strConn)

With gr
.DataSource = dr
.DataBind()
End With
calBirthDate.SelectedDate = CDate(dr("BirthDate"))

End Sub
 
Hello -
The datareader returns what is called a firehose cursor (forward only, readonly). When you call the databind method, it moves through the result set past the last record. So you need to read it again. I think this will do the trick.

Private Sub rblEmpl_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rblEmpl.SelectedIndexChanged
Dim strSQL As String
Dim strConn As String
Dim dr As OleDb.OleDbDataReader

strConn = Session("ConnectString")
strSQL = "Select EmployeeID, BirthDate FROM Employees where EmployeeID = " & rblEmpl.Items(rblEmpl.SelectedIndex).Value

dr = DataHandler.GetDataReader(strSQL, strConn)

With gr
.DataSource = dr
.DataBind()
End With
dr = DataHandler.GetDataReader(strSQL, strConn)
if dr.read then
calBirthDate.SelectedDate = CDate(dr("BirthDate"))
end if

dr.close
End Sub

Note that you should close the datareader.

Hope this works
Mark
 
Thanks Mark,

I'll try dr.read .
I was thinking that having those two lines would be enough:

dr = DataHandler.GetDataReader(strSQL, strConn)
calBirthDate.SelectedDate = CDate(dr("BirthDate"))
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top