Just wondering if someone can check my do while statement that is nested within an if/elseif statement to see where I’m potentially going wrong.
strSQL = "exec sp_CoursesStartingSoon"
objConn = New SqlConnection(strConnection)
objConn.Open()
objCmd = New SqlCommand(strSQL, objConn)
datareader = objCmd.ExecuteReader(0)
datareader.Read()
If (datareader("subjectcode") = "F23") Then
Do While (datareader("subjectcode") = "F23")
html += "<h1>Access to HE</h1>"
html += "<p>" & datareader("name") & "</p>"
html += "<p>" & datareader("level") & "</p>"
Loop
ElseIf (datareader("subjectcode") = "F06") Then
Do While (datareader("subjectcode") = "F06")
html += "<h1>Art and Design</h1>"
html += "<p>" & datareader("name") & "</p>"
html += "<p>" & datareader("level") & "</p>"
Loop
End If
At the moment, the page isn’t loading and seems stuck in a loop but wanted to check if its my logic (which I’m assuming it is).
Thanks.
You are not progressing the datareader after the:
Therefore it is continuously reading the same record 🙂
You need to put a:
in the loop somewhere.
If you match any of the F23 or F06 checks, you will stay in the do while loop. Trust me.
EDIT: here you go
Just to explain:
To read the next record, you always need to do datareader.Read(). If there are no more records, the API will return false, and exit the loop correctly. You can then check the datareader cursor to see if the current record contains the strings you are looking for. Then when the code loops round again, the datareader.Read() will get the next record for you.