www.webdeveloper.com
Results 1 to 3 of 3

Thread: Invalid attempt to read when no data is present

  1. #1
    Join Date
    Apr 2004
    Posts
    27

    Invalid attempt to read when no data is present

    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:

    Code:
    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.
    Last edited by Eulibrius7; 05-16-2004 at 01:25 AM.

  2. #2
    Join Date
    Apr 2004
    Posts
    27

    Addendum

    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?

  3. #3
    Join Date
    Apr 2004
    Posts
    27

    Talking Understanding...

    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.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles