Results 1 to 4 of 4

Thread: System.IndexOutOfRangeException

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jul 2021
    Posts
    66

    System.IndexOutOfRangeException

    Hi,

    I have the following code - I am on cellcontent click I would like to populate textbox values and an image. See code below

    Code:
    Dim conn As SqlConnection = GetDbConnection()
    
    
            Dim command As New SqlCommand("SELECT * FROM dbo.tbltenants WHERE nationalID=@nationalid", conn)
            command.Parameters.Add("@nationalid", SqlDbType.VarChar).Value = DgTenantDetails.Rows(e.RowIndex).Cells(6).Value
    
    
            Dim table As New DataTable()
            Dim adapter As New SqlDataAdapter(command)
            adapter.Fill(table)
    
    
            TxtFNameUp.Text = table.Rows(0)(1).ToString()
            TxtLNameUp.Text = table.Rows(0)(2).ToString()
            TxtTelUp.Text = table.Rows(0)(4).ToString()
            TxtEmailUp.Text = table.Rows(0)(5).ToString()
            TxtNationalIDUp.Text = table.Rows(0)(6).ToString()
    
            If table.Rows.Count() <= 0 Then
    
                MessageBox.Show("No image available for this National ID")
    
            Else
                Dim img() As Byte
                img = table.Rows(0)(7)
    
                Dim ms As New MemoryStream(img)
                PictureBox1.Image = Image.FromStream(ms)
                PictureBox1.SizeMode = PictureBoxSizeMode.StretchImage
    
            End If

    I get the error message:

    Code:
    System.ArgumentOutOfRangeException: 'Index was out of range. Must be non-negative and less than the size of the collection.
    Parameter name: index'
    Please help!

    Thanks

  2. #2
    King of sapila
    Join Date
    Oct 2006
    Location
    Greece
    Posts
    6,024

    Re: System.IndexOutOfRangeException

    You don't specify the line the problem resides but I'm betting is this one:
    DgTenantDetails.Rows(e.RowIndex).Cells(6).Value

    Check that you actually return a value there. If it does check that the table row extend to the 1,2,4,5,6 values you set or if does have a value at all.
    .

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Jul 2021
    Posts
    66

    Re: System.IndexOutOfRangeException

    Tried to do this....

    Code:
            TxtNationalIDValue.Text = Me.DgTenantDetails.SelectedCells(6).Value.ToString()
    
    
            Dim command As New SqlCommand("SELECT * FROM dbo.tbltenants WHERE nationalID=@nationalid", conn)
            command.Parameters.Add("@nationalid", SqlDbType.VarChar).Value = TxtNationalIDValue.Text
    Same error exists

  4. #4
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    24,183

    Re: System.IndexOutOfRangeException

    Column indices are zero based

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width