.NextResult() method does gives an error that states that no data is present

こ雲淡風輕ζ 提交于 2021-02-10 19:50:53

问题


Can you check my coding and let me know what I'm doing wrong?

I'm trying to use the .NextResult() method of a DataReader but I get an error that no data is present.

The 1st query returns a value but the 2nd query is the problem.

Dim strSqlStatement As String = "Select Count(*) As TotalRows " & _
                                          "From Parents " & _
                                         "Where (FatherName = @SearchValue " & _
                                         "   Or  MotherName = @SearchValue);"

strSqlStatement = strSqlStatement & "Select FatherName, MotherName " & _
                                          "From Parents " & _
                                         "Where (FatherName = @SearchValue " & _
                                         "   Or  MotherName = @SearchValue)"

' Set up the sql command and lookup the parent.
'----------------------------------------------
Using objSqlCommand As SqlCommand = New SqlCommand(strSqlStatement, ObjConnection)

    With objSqlCommand

        ' Add SqlParameters to the SqlCommand.
        '-------------------------------------
        .Parameters.Clear()
        .Parameters.AddWithValue("@SearchValue", TextBoxParentsName.Text)

        ' Open the SqlConnection before executing the query.
        '---------------------------------------------------
        Try
            ObjConnection.Open()

            ' Execute the query to see if the parents are in the database.
            '-------------------------------------------------------------

            ' Display the parent info.
            '-------------------------
            Dim reader As SqlDataReader = .ExecuteReader()

            reader.Read()

            Dim countOfRows = reader("TotalRows")

            If countOfRows = 1 Then

                reader.NextResult()

                TextBoxParentsName.Text = reader("FatherName").ToString()
                LabelBothParents.Text = "Father: " & TextBoxParentsName.Text & " Mother: " & reader("MotherName")
            End If

       Catch exErrors As Exception

            MessageBox.Show("Sorry, there was an error. Details follow: " & _
                                    vbCrLf & vbCrLf & exErrors.Message, _
                                    "Error")

            TextBoxParentsName.Focus()
        Finally
            blnDisableParentIdTextChanged = False

            ObjConnection.Close()
        End Try

    End With ' objSqlCommand
End Using ' objSqlCommand

回答1:


Found the missing statement:

I needed to add:

reader.Read()

after the reader.NextResult this area of coding:

If countOfRows = 1 Then

    reader.NextResult()

    reader.Read() ' This is what I needed to add.

    TextBoxParentsName.Text = reader("FatherName").ToString()
    LabelBothParents.Text = "Father: " & TextBoxParentsName.Text & " Mother: " & reader("MotherName")
End If

I hope this helps someone who gets stuck like I did.



来源:https://stackoverflow.com/questions/9595190/nextresult-method-does-gives-an-error-that-states-that-no-data-is-present

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!