0

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.

0

1 Answer 1

2

You are not progressing the datareader after the:

Do While (datareader("subjectcode") = "F23")

Therefore it is continuously reading the same record :)

You need to put a:

datareader.Read()

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

    strSQL = "exec sp_CoursesStartingSoon"
    objConn = New SqlConnection(strConnection)
    objConn.Open()
    objCmd = New SqlCommand(strSQL, objConn)
    datareader = objCmd.ExecuteReader(0)

    While datareader.Read()

        If (datareader("subjectcode") = "F23") Then

            html += "<h1>Access to HE</h1>"
            html += "<p>" & datareader("name") & "</p>"
            html += "<p>" & datareader("level") & "</p>"

        ElseIf (datareader("subjectcode") = "F06") Then

            html += "<h1>Art and Design</h1>"
            html += "<p>" & datareader("name") & "</p>"
            html += "<p>" & datareader("level") & "</p>"

        End If

    End While

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Have I used the wrong logic in the setup of what I'm attempting? Its not obvious to me where I'd need to put another datareader in, in order to progress to the next record.
It would deserve an upvote if you try to write the corrected code

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.