错误:
“System.IndexOutOfRangeException”类型的未处理异常 发生在System.Data.dll中并且还显示:没有行 位置0
Dim mycn As New SqlConnection(connection)
Dim DT As New DataTable
mycn.Open()
Dim Adapter As New SqlDataAdapter("SELECT * FROM tblUser where username ='" & txtRet.Text & "'", connection)
Adapter.Fill(DT)
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()
Dim bytBLOBData() As Byte = _
DT.Rows(0)("userimage")
Dim stmBLOBData As New MemoryStream(bytBLOBData)
UserPictureBox.Image = Image.FromStream(stmBLOBData)
mycn.Close()
答案 0 :(得分:0)
此错误表示没有返回任何行,因为您尝试获取第一行(... DT.Rows(0)...),它会抛出错误。
我的第一个建议是将代码包装在Using语句中,用于实现iDisposable的所有对象。我的第二个建议是利用参数化查询。我的最后一个建议是在尝试访问它之前检查Rows(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