Invalid attempt to Read when reader is closed

L

Len

Help. I am new to VB.NET 2003. I have written an application for SQL
CE (also new at) and I am very frustrated. This is the code:

strSQL = "INSERT INTO ROLL(RNO, SPCD, UPDATES, CONTIME, STCD,
PSCD, LOCD) " & _
"VALUES('" & strPrevRoll & "', '" & strSpeed & "', 0, '" &
datCurrent & "', '" & strStatus & "', '" & strPosition & "', " &
intLocation & ")"
cmd.CommandText = strSQL
cmd.Connection = Conn
cmd.ExecuteNonQuery()

strSQL = "SELECT * from ROLL WHERE RNO = '" & strRoll
& "'"
cmd.CommandText = strSQL
cmd.Connection = Conn
reader = cmd.ExecuteReader
While reader.Read
cmbPosition.Text = reader.Item("PSCD")
cmbStatus.Text = reader.Item("STCD")
cmbSpeed.Text = reader.Item("SPCD")
If cmbStatus.Text = "S1 " Then
cmbPosition.Visible = False
lblPosition.Visible = False
lblPOS.Visible = False
End If
End While
'Conn.Close()
'Conn.Open()
'reader.Close()
'txtScan.Focus()

Everything works find until the "While reader.Read" line is executed
for the second time. It does not find more than one record, and the
very next line I get the error :

An unhandled exception of type 'System.InvalidOperationException'
occurred in System.Data.SqlServerCe.dll

Additional information: Invalid attempt to Read when reader is closed.

I have tried several things as you can see in the commented out lines.
Regardless of what line after the "End While", I get the error. HELP
Please. Thanks in advance.

Len
 
R

Raymond Lewallen

Len,

Off the top of my head I don't see an obvious problem. Instead of While...
End While try using Do While... Loop. Just a quick suggestion, and I'll
look into it a bit deeper in the meantime.

HTH,

Raymond Lewallen
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top