Index was out of range. Must be non-negative and less than the size of the collection Error

P

Pat

I have a 3 Datagrids.
The dtgMaster is the main datagrid and after selecting a ROW i populate the
second datagrid dtgDetails and the same for
the 3rd datagrid dtgChild.
But the thing is after clicking on a ROW on the dtgMaster and then select a
ROW in the second datagrid and the go back to the
dtgMaster which is the main datagrid and try to PAGE i get the error

"Index was out of range. Must be non-negative and less than the size of the
collection. Parameter name: index"

At the line
Dim strFilter As String =
CStr(dtgDetails.DataKeys(dtgDetails.SelectedIndex)).Replace("'", "''")
of the second DataGrid

Any ideas

Sub BindMaster()
Dim strQuery As String = "Select OrderID,InvoiceDate,OrderDate From
CMRC_Orders ORDER BY " & viewstate("sortField").ToString & " " &
viewstate("sortDirection").ToString

'Dim objConn As SqlConnection = New
SqlConnection(ConfigurationSettings.AppSettings("ConnectionString"))

Dim dataAdapter As New SqlDataAdapter(strQuery, objConn)
Dim ds As New DataSet

dataAdapter.Fill(ds)
dtgMaster.DataSource = ds
'dtgMaster.CurrentPageIndex = 0
dtgMaster.DataBind()

End Sub

Sub BindDetails()
' getting the filter value from the master Grid's DataKeys
collection

If dtgMaster.SelectedIndex <> -1 Then

Dim strFilter As String =
CStr(dtgMaster.DataKeys(dtgMaster.SelectedIndex)).Replace("'", "''")


Dim strQuery As String = "Select t1.OrderDetailID, t1.OrderID,
where t1.OrderID = '" & strFilter & "'"


Dim dataAdapter As New SqlCommand(strQuery, objConn)

objConn.Open()
dtgDetails.DataSource = dataAdapter.ExecuteReader()

End If
dtgDetails.DataBind()

End Sub




Sub BindChild()
If dtgDetails.SelectedIndex <> -1 Then
'Dim objConn As SqlConnection = New
SqlConnection(ConfigurationSettings.AppSettings("ConnectionString"))

'Error here
Dim strFilter As String =
CStr(dtgDetails.DataKeys(dtgDetails.SelectedIndex)).Replace("'", "''")

'Dim strQuery As String = "Select t3.Feature from Orders where
t1.OrderDetailID= '" & strFilter & "'"

Dim dataAdapter As New SqlCommand(strQuery, objConn)
objConn.Open()

dtgChild.DataSource =
dataAdapter.ExecuteReader(CommandBehavior.CloseConnection)

'Response.Write("<b>" & strFilter & "</b>")

End If
dtgChild.DataBind()


End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top