2017-11-09 20 views
-2

エラー:ない行は、位置0とSystem.IndexOutOfRangeExceptionではありません

「System.IndexOutOfRangeException」 タイプの未処理の例外がのSystem.Data.dllで発生し、また示す:NO行ではありませんそれがエラーを投げている(... ... DT.Rows(0)) 位置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

DT.Rows、最初DTに何かを追加する必要があります。代わりにあなたの質問かもしれません:いくつかのSQLテーブルから変数に行を追加する方法... – Hansa

+0

私はハンサに同意します、これはあなたがいくつかの文脈を与え、明確な質問としてより多くを額にした方が良いでしょう。 :) – Schwad

+1

説明のタイトル、説明、エラーはありません。1)何がうまくいかないかについて絶対に明確であり、2)SO(DataAdapter、SQL Parameter、SQL Injection、。 ..) –

答えて

0

は、このエラーが何行が返されなかったことを示し、あなたは最初の行を取得しようとしているので、 。

私の最初の提案は、iDisposableを実装するすべてのオブジェクトに対してUsingステートメントでコードをラップすることです。私の2番目の提案は、パラメータ化されたクエリを利用することです。私の最後の提案は、アクセスしようとする前に行(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][email protected];", 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 
関連する問題