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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

datareader problem:Invalid attempt to read data when reader is closed.

Status
Not open for further replies.

MackAA

Programmer
Mar 21, 2006
13
CA
I get a mysterious error when I try to read a datareader:
I have an sql statement and when I execute it it sqlserver management studio I get one row. And when I try with the datareader I get the: Invalid attempt to read data when reader is closed.


SqlConn = New SqlConnection(ConfigurationSettings.AppSettings.Item("ConnectionString"))
SqlConn.Open()

strSQL = "SELECT * FROM MyTable"
dr = New SqlCommand(strSQL, SqlConn).ExecuteReader

If dr.Read() Then
intPeriod = dr("ID")
dCurrentPeriod = dr("StartDate")
end if
dr.Close()

Is anyone could help me with this?
 
Try using a While Loop rather than an End If statement. You could also check the HasRows method to see if it has been populated correctly.


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
hmm,

the poster's code looks ok. before the HasRows method came about this was the way in which we were checking if the reader has rows.


7520:

which line does the error point to? the If Stmt???

Known is handfull, Unknown is worldfull
 
Hi everybody and thanks for your interest to my question.

So, the while loop gets me the same error and dr.hasRows is true.
The dr.read is false but I know that my datareader has data beacuse the same sql statement in sql server management studio returns one row:

Id:69342
StartDate:2006-08-06 00:00:00
PeriodNo: 40

any other suggestions?



 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top