Monday, March 12, 2012

why this error?Invalid attempt to read when no data is present

System.InvalidOperationException: Invalid attempt to read when no data is present. at System.Data.SqlClient.SqlDataReader.PrepareRecord(Int32 i) at System.Data.SqlClient.SqlDataReader.GetValue(Int32 i) at System.Data.SqlClient.SqlDataReader.get_Item(String name) at e_auction.temp.Page_Load(Object sender, EventArgs e)

I have check the query in QUERY ANALYZER, it worked pretty fine, but when i copy pasted, it is generating exception, I checked it, the part which is inbold is genearting exception and goes to catch part. i don't know y, help me plz


Dim bool_bidUpdate As Boolean = True
Dim lastBidder As Integer = 1
Dim inc As Long = 5000
Dim lastBid As Long = 175000

Dim tempMax As Long = 0
Dim tempBidder As Integer
Dim tempBidDate As Date
Dim tempCurrentBid As Long

Dim myConnection As SqlConnection = New SqlConnection(ConfigurationSettings.AppSettings("ConnectionString"))
Dim MydataReader As SqlDataReader
Dim MyCommand As New SqlCommand()

MyCommand = New SqlCommand("select * from temp1 where auctionId = 7 and maxBid > (select max(currBid) from temp1 where auctionId=7) order by maxBid DESC", myConnection)
myConnection.Open()
Try
MydataReader = MyCommand.ExecuteReader()
If CInt(MydataReader.Item("uId")) = lastBidder Then
bool_bidUpdate = False
Else
Do While MydataReader.Read()

If tempMax < MydataReader("maxBid") Then
If (MydataReader("maxBid") / (lastBid + inc)) >= 1 Then
tempMax = MydataReader("maxBid")
tempBidder = MydataReader("uId")
tempBidDate = MydataReader("bidDate")
tempCurrentBid = (lastBid + inc)
End If
ElseIf tempMax = MydataReader("maxBid") Then
If tempBidDate > MydataReader("bidDate") Then
tempBidDate = MydataReader("bidDate")
tempMax = MydataReader("maxBid")
tempBidder = MydataReader("uId")
End If
Else
Exit Do
End If

Loop
End If
myConnection.Close()

Catch b As Exception
Response.Write(b.ToString)
End Try
If bool_bidUpdate = False Then
Response.Write("no need to update")
Else
Response.Write("paleeeeez update" & " with uID: " & tempBidder & " with tempCurrentBid : " & tempCurrentBid)
End If

You need to call reader's Read method at least once before trying to access its data.
then wat should i do, if i start it from the first if condition my whole logic ruins, coz reader only goes in forward direction, so wat shoud i do, one way ard could be i check if this loop is executing second time the first if condition shouldn't be executing but this is just hmm not a good approach?
wat do u suggest.

'Read the first row
If reader.Read() Then
'Do the one-time check here

'Continue normally in the loop
Do
'processing goes here
'...
Loop While reader.Read()
Else
'No records
'...
End If
reader.Close()

Here is a way to check if there is a row/make a one time check and then continue normally.
it works fine as long as i don't use debugger, but whenever i inser any breakpoint, it gives me same error and goes to catch part. wat can be the prob
Where do you put breakpoints? What's the code you use now?
it generates error when it reaches to myDataReader part. i have put break point in various position sto check the prob, it still exists, wither its in some expression or either its some pace where i m using myDataReader. but when i run it without breakpoints it runs quite smooth.
i have changed the code to previous version, sorry for dat :)


Try
MydataReader = MyCommand.ExecuteReader()
Do While MydataReader.Read
-->breakPoint iLoopcheck = iLoopcheck + 1
If MydataReader.Item("uId") = lastBidder And iLoopcheck = 1 Then
tempLastMaxBid = MydataReader.Item("maxBid")
tempLastMaxBidder = MydataReader.Item("uId")
bool_lastCheck = False
Else
newCurrentBid = lastBid + inc
-->exception Point If tempMax < MydataReader("maxBid") Then
If (MydataReader("maxBid") / newCurrentBid) >= 1 Then

0 comments:

Post a Comment