locked
While loop in vb.net RRS feed

  • Question

  • User-1578974752 posted

    In the below code ,if the detail have 7 values then only 6 is counting. After 6 the debugger is exiting the while loop. If 5 values in detail table then 4 is counting in the While loop.Always 1 less than the actual number.How to fix this issue in While loop. Appreciate the help

    Try

    cmd.CommandText = "select * FROM Detail where TestID = '" + id.Text + "'"

    drd = cmd.ExecuteReader

     

    If drd.HasRows = True Then

    drd.Read()

    While drd.Read

    Selans.Text = drd.Item("SelectedAnswer").ToString

    Corrans.Text = drd.Item("CorrectAnswer").ToString

    If (Selans.Text = Corrans.Text) Then

    TextBox1.Text = 20

    markobt += 20

    Else

    TextBox1.Text = 0

    End If

    End While

    End If

    Wednesday, September 12, 2018 7:15 AM

Answers

  • User1724605321 posted

    Hi shsu ,

    Please refer to document :

    https://msdn.microsoft.com/en-us/library/aa326245(v=vs.71).aspx 

    [Visual Basic] 
    Public Sub CreateMySqlDataReader(mySelectQuery As String, myConnection As SqlConnection)
        Dim myCommand As New SqlCommand(mySelectQuery, myConnection)
        myConnection.Open()
        Dim myReader As SqlDataReader
        myReader = myCommand.ExecuteReader()
        Try
        While myReader.Read()
            Console.WriteLine(myReader.GetString(0))
        End While
    Finally
        myReader.Close
        myConnection.Close
        End Try
    End Sub 'CreateMySqlCommand

    And I would recommend to you use the parametrized query to avoid SQL Injection ,  like this code sample .

    Best Regards,

    Nan Yu

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, September 12, 2018 7:32 AM
  • User1120430333 posted

    Why are you doing a drd.Read() before you do While drd.Read(s) in the loop?  What is the purpose of the single drd.Read() that reads one record and move the reader cursor to the next record? The reader is doing a read forward,  meaning one record is read, then the reader courser moves forward to the next record. You are bypassing one record doing nothing with it before you even start using the While loop reader. 

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, September 12, 2018 10:59 AM

All replies

  • User1724605321 posted

    Hi shsu ,

    Please refer to document :

    https://msdn.microsoft.com/en-us/library/aa326245(v=vs.71).aspx 

    [Visual Basic] 
    Public Sub CreateMySqlDataReader(mySelectQuery As String, myConnection As SqlConnection)
        Dim myCommand As New SqlCommand(mySelectQuery, myConnection)
        myConnection.Open()
        Dim myReader As SqlDataReader
        myReader = myCommand.ExecuteReader()
        Try
        While myReader.Read()
            Console.WriteLine(myReader.GetString(0))
        End While
    Finally
        myReader.Close
        myConnection.Close
        End Try
    End Sub 'CreateMySqlCommand

    And I would recommend to you use the parametrized query to avoid SQL Injection ,  like this code sample .

    Best Regards,

    Nan Yu

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, September 12, 2018 7:32 AM
  • User1120430333 posted

    Why are you doing a drd.Read() before you do While drd.Read(s) in the loop?  What is the purpose of the single drd.Read() that reads one record and move the reader cursor to the next record? The reader is doing a read forward,  meaning one record is read, then the reader courser moves forward to the next record. You are bypassing one record doing nothing with it before you even start using the While loop reader. 

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, September 12, 2018 10:59 AM