Click to See Complete Forum and Search --> : Invalid attempt to read when no data is present


Eulibrius7
05-16-2004, 01:22 AM
Server Error in '/' Application.
--------------------------------------------------------------------------------

Invalid attempt to read when no data is present.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.InvalidOperationException: Invalid attempt to read when no data is present.

Source Error:


Line 322: dr.Read()
Line 323:
Line 324: Response.Write(dr("Quote"))
Line 325: Response.Write(dr("Name"))
Line 326:



Sub Random_Quote()

Dim conDB As SqlConnection = New SqlConnection("Server=DOX;Database=integration;User ID=sa;Password=x")
Dim dr As SqlDataReader
Dim RecNo, MaxRecNo, MinRecNo as Integer

conDB.Open()

Dim cmdSelectRndmQuote As SqlCommand = New SqlCommand( "Select Max(ID) as MaxID, Min(ID) as MinID FROM dbo.tblQuotes", conDB)
dr = cmdSelectRndmQuote.ExecuteReader()
dr.Read()
MaxRecNo = Int(dr("MaxID"))
MinRecNo = Int(dr("MinID"))
RecNo = getRandomNumber(MinRecNo,MaxRecNo)
dr.Close()

cmdSelectRndmQuote = New SqlCommand( "SELECT dbo.tblQuotes.ID, dbo.tblQuotes.Quote, dbo.tblTextSource.Name, dbo.tblTextSource.Nlink FROM dbo.tblTextSource RIGHT JOIN dbo.tblQuotes ON dbo.tblTextSource.ID = dbo.tblQuotes.Source WHERE dbo.tblQuotes.ID = '" & RecNo & "'", conDB)

dr = cmdSelectRndmQuote.ExecuteReader()
dr.Read()

Response.Write(dr("Quote"))
Response.Write(dr("Name"))

dr.Close()
conDB.Close()
conDB.Dispose()

End Sub



This Error is INCONSISTENT. If the page is refreshed say 10 times, it may give the error 4 out of 9 times.

Eulibrius7
05-16-2004, 02:27 AM
If the value RecNo is >= 100 Then it spits out this error saying that there wasn't any IDs beyond 99 which isn't true since there are 380+ records... What could be causing this?

Eulibrius7
05-16-2004, 03:53 AM
the error message would clue one in on the fact that the Query isn't returning any rows for the DataReader to display. That was the problem since the IDs in the DB had gaps. Problem FIXED.