-
Jul 25th, 2021, 10:20 AM
#1
Thread Starter
Lively Member
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
-
Jul 25th, 2021, 10:33 AM
#2
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.
ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς μάλα πολλὰ
πλάγχθη, ἐπεὶ Τροίης ἱερὸν πτολίεθρον ἔπερσεν·
-
Jul 25th, 2021, 10:41 AM
#3
Thread Starter
Lively Member
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
-
Jul 25th, 2021, 10:48 AM
#4
Re: System.IndexOutOfRangeException
Column indices are zero based
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|