I got the error message There is an open data reader associated with this command which needs to be closed first by using the following code:
myCommand = New SqlCommand("SELECT BookCode FROM tblBook",myConnection)
myReader = myCommand.ExceuteReader
While myReader.Read
If myReader(0).ToString <> txtBookCode.Text Then
myCommand = New SqlCommand("INSERT INTO tblBook VALUES(@BookCode, @BookTitle)",myConnection)
myCommand.Parameters.AddWithValue("@BookCode", txtBookCode.Text)
myCommand.Parameters.AddWithValue("@BookTitle", txtBookTitle.Text)
myCommand.ExecuteNonQuery()
Else
MsgBox("There is already a book name '"& txtTitle.Text "'. Please try another code.",vbOkOnly,"BookCode Exists")
End If
End While
Pleas help.
Don’t reuse myCommand variable. Create new one.
myCommand should be disposed in the end too (as well as reader).
Real reason of exception is more likely that you’re trying to run two command on one connection at the same time.
First read all data you need from reader and THEN do all inserts. Not both at once (i assume you don’t want to create two connections. That would suck)