Click here to Skip to main content
15,884,859 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hey guys,
I got an error at this called says" Index was out of range. Must be non-negative and less than the size of the collection, Parameter name : Index"

Please help ASAP

VB
Private Sub dgw_MouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles dgw.MouseClick
        Try
            If dgw.Rows.Count > 0 Then

                If lblSet.Text = "Product Entry" Then
                    Dim dr As DataGridViewRow = dgw.SelectedRows(1)
                    frmProduct.Show()
                    Me.Hide()
                    frmProduct.txtID.Text = dr.Cells(0).Value.ToString()
                    frmProduct.txtProductCode.Text = dr.Cells(1).Value.ToString()
                    frmProduct.txtBarcode.Text = dr.Cells(2).Value.ToString()
                    frmProduct.Date1.Text = dr.Cells(3).Value.ToString()
                    frmProduct.SendName.Text = dr.Cells(4).Value.ToString()
                    frmProduct.SendCity.Text = dr.Cells(5).Value.ToString()
                    frmProduct.SendAddress.Text = dr.Cells(6).Value.ToString()
                    frmProduct.RecName.Text = dr.Cells(7).Value.ToString()
                    frmProduct.RecCity.Text = dr.Cells(8).Value.ToString()
                    frmProduct.RecAddress.Text = dr.Cells(9).Value.ToString()
                    frmProduct.txtCostPrice.Text = dr.Cells(10).Value.ToString()
                    frmProduct.txtOpeningStock.Text = dr.Cells(11).Value.ToString()
                    frmProduct.txtDiscount.Text = dr.Cells(12).Value.ToString()
                    frmProduct.txtSellingPrice.Text = dr.Cells(13).Value.ToString()
                    frmProduct.txtReorderPoint.Text = dr.Cells(14).Value.ToString()
                    frmProduct.cmbSalesUnit.Text = dr.Cells(15).Value.ToString()
                    con = New SqlConnection(cs)
                    con.Open()
                    cmd = New SqlCommand("SELECT Photo from Product,Product_Join where Product.PID=Product_Join.ProductID and Product.PID=@d1", con)
                    cmd.Parameters.AddWithValue("@d1", dr.Cells(0).Value.ToString())
                    rdr = cmd.ExecuteReader(CommandBehavior.CloseConnection)
                    frmProduct.dgw.Rows.Clear()
                    While (rdr.Read() = True)
                        Dim img4 As Image
                        Dim data As Byte() = DirectCast(rdr(0), Byte())
                        Dim ms As New MemoryStream(data)
                        img4 = Image.FromStream(ms)
                        frmProduct.dgw.Rows.Add(img4)
                    End While
                    con.Close()
                    frmProduct.btnUpdate.Enabled = True
                    frmProduct.btnDelete.Enabled = True
                    frmProduct.btnSave.Enabled = False
                    lblSet.Text = ""
                End If
            End If

        Catch ex As Exception
            MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try

    End Sub


What I have tried:

I tried to remove and add some cells.
Posted
Updated 8-May-18 4:07am
v2
Comments
Maciej Los 8-May-18 9:17am    
What line? What part of error message is unclear?

1 solution

The error message is pretty explicit:
Index was out of range. Must be non-negative and less than the size of the collection, Parameter name : Index
What it is saying is that one of the values you are using to access a collection is either negative, or larger than the number of items in the collection, minus one (as C# indexes are always zero based).
Since your code fragment only accesses the DataRow Cells collection via an index, you need to check the DataTable and see how many columns it has, then find out which index is at fault. Use the debugger: put a breakpoint on the first line of the function and when it is reached, you can look at the variable contents to find the appropriate array index values. Then you can start looking at why the row isn't as big as you think it is...

We can't do that for you: we can't run your code, and don't have access to your data...
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900