Sql 位置0和System.IndexOutOfRangeException处没有行

Sql 位置0和System.IndexOutOfRangeException处没有行,sql,vb.net,Sql,Vb.net,错误: 类型为“System.IndexOutOfRangeException”的未处理异常 发生在System.Data.dll中,并且还显示a:上没有行 位置0 此错误表示未返回任何行,并且由于您试图获取第一行(…DT.rows(0)…),因此将抛出此错误 我的第一个建议是对实现iDisposable的所有对象使用语句来包装代码。我的第二个建议是使用参数化查询。我的最后一个建议是在尝试访问行(0)之前检查行(0)是否存在 下面是一个简单的例子: 'Declare the connec

错误:

类型为“System.IndexOutOfRangeException”的未处理异常 发生在System.Data.dll中,并且还显示a:上没有行 位置0


此错误表示未返回任何行,并且由于您试图获取第一行(…DT.rows(0)…),因此将抛出此错误

我的第一个建议是对实现iDisposable的所有对象使用语句来包装代码。我的第二个建议是使用参数化查询。我的最后一个建议是在尝试访问行(0)之前检查行(0)是否存在

下面是一个简单的例子:

    'Declare the connection object
    Dim con As SqlConnection

    'Wrap code in Try/Catch
    Try
        'Set the connection object to a new instance
        con = New SqlConnection(connection)

        'Create a new instance of the command object
        Using cmd As SqlCommand = New SqlCommand("SELECT * FROM [tblUser] WHERE [username]=@username;", con)
            'Parameterize the query
            cmd.Parameters.AddWithValue("@username", txtRet.Text)

            'Open the connection
            con.Open()

            'Declare a new adapter to fill the data table
            Dim adapter As SqlDataAdapter = New SqlDataAdapter(cmd)
            adapter.Fill(DT)

            'Close the connection
            con.Close()
        End Using
    Catch ex As Exception
        'Display the error
        Console.WriteLine(ex.Message)
    Finally
        'Check if the connection object was initialized
        If con IsNot Nothing Then
            If con.State = ConnectionState.Open Then
                'Close the connection if it was left open(exception thrown)
                con.Close()
            End If

            'Dispose of the connection object
            con.Dispose()
        End If
    End Try

    If DT.Rows.Count > 0 Then
        txtUserID.Text = DT.Rows(0)("aid").ToString()
        txtFirstName.Text = DT.Rows(0)("fname").ToString()
        txtMiddleName.Text = DT.Rows(0)("mi").ToString()
        txtLastName.Text = DT.Rows(0)("lname").ToString()
        DateOfBirthDateTimePicker.Text = DT.Rows(0)("bday").ToString()
        txtAge.Text = DT.Rows(0)("age").ToString()
        cmbGender.Text = DT.Rows(0)("gender").ToString()
        txtContactNo.Text = DT.Rows(0)("contactno").ToString()
        txtEmail.Text = DT.Rows(0)("email").ToString()
        txtAddress.Text = DT.Rows(0)("address").ToString()
        txtUsernamePS.Text = DT.Rows(0)("username").ToString()
        txtPasswordPS.Text = DT.Rows(0)("password").ToString()
        rtbSQuestions.Text = DT.Rows(0)("squestion").ToString()
        rtbAnswer.Text = DT.Rows(0)("answer").ToString()
    End If

DT.行(0)为空,您应该首先向DT添加一些内容。或者,您的问题可能是:如何将某些SQL表中的行添加到变量中……我同意Hansa的观点,如果您提供一些上下文,并将其设置为更清晰的问题,这将更好地为您服务。:)没有描述性的标题,没有解释,还有一个错误1)很容易修复,因为异常完全清楚出错的地方,2)已经被回答了一万亿次,所以(DataAdapter,SQL参数,SQL注入,…)在使用
DT.Rows.Count>0
之前检查
DT
非常感谢,我已经得到了它。非常感谢:)
    'Declare the connection object
    Dim con As SqlConnection

    'Wrap code in Try/Catch
    Try
        'Set the connection object to a new instance
        con = New SqlConnection(connection)

        'Create a new instance of the command object
        Using cmd As SqlCommand = New SqlCommand("SELECT * FROM [tblUser] WHERE [username]=@username;", con)
            'Parameterize the query
            cmd.Parameters.AddWithValue("@username", txtRet.Text)

            'Open the connection
            con.Open()

            'Declare a new adapter to fill the data table
            Dim adapter As SqlDataAdapter = New SqlDataAdapter(cmd)
            adapter.Fill(DT)

            'Close the connection
            con.Close()
        End Using
    Catch ex As Exception
        'Display the error
        Console.WriteLine(ex.Message)
    Finally
        'Check if the connection object was initialized
        If con IsNot Nothing Then
            If con.State = ConnectionState.Open Then
                'Close the connection if it was left open(exception thrown)
                con.Close()
            End If

            'Dispose of the connection object
            con.Dispose()
        End If
    End Try

    If DT.Rows.Count > 0 Then
        txtUserID.Text = DT.Rows(0)("aid").ToString()
        txtFirstName.Text = DT.Rows(0)("fname").ToString()
        txtMiddleName.Text = DT.Rows(0)("mi").ToString()
        txtLastName.Text = DT.Rows(0)("lname").ToString()
        DateOfBirthDateTimePicker.Text = DT.Rows(0)("bday").ToString()
        txtAge.Text = DT.Rows(0)("age").ToString()
        cmbGender.Text = DT.Rows(0)("gender").ToString()
        txtContactNo.Text = DT.Rows(0)("contactno").ToString()
        txtEmail.Text = DT.Rows(0)("email").ToString()
        txtAddress.Text = DT.Rows(0)("address").ToString()
        txtUsernamePS.Text = DT.Rows(0)("username").ToString()
        txtPasswordPS.Text = DT.Rows(0)("password").ToString()
        rtbSQuestions.Text = DT.Rows(0)("squestion").ToString()
        rtbAnswer.Text = DT.Rows(0)("answer").ToString()
    End If