This is a migrated thread and some comments may be shown as answers.

RadGrid Update index

4 Answers 112 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Steve
Top achievements
Rank 1
Steve asked on 12 Oct 2008, 11:44 PM
 I am trying to get my grid to update my supplier table but I keep getting an error.
Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index

Protected Sub grdSuppliers_Competetitors_UpdateCommand(ByVal source As Object, ByVal e As Telerik.Web.UI.GridCommandEventArgs) Handles grdSuppliers_Competetitors.UpdateCommand
        Dim oSup As New SupplierCompetitor
        'Get the GridEditableItem of the RadGrid       
        Dim editedItem As GridEditableItem = TryCast(e.Item, GridEditableItem)
        'Get the primary key value using the DataKeyValue.       
        Dim SupplierCompetitorID As String = editedItem.OwnerTableView.DataKeyValues(editedItem.ItemIndex)("SupplierCompetitorID").ToString()
        'Access the textbox from the edit form template and store the values in string variables.       
        'Dim SupplierCompetitorID As String = (TryCast(editedItem("SupplierCompetitorID").Controls(0), TextBox)).Text
        Dim supplierCompetitorName As String = (TryCast(editedItem("supplierCompetitorName").Controls(0), TextBox)).Text
        Dim IsCompAs Boolean = (TryCast(editedItem("IsComp").Controls(0), CheckBox)).Checked

        If (TryCast(editedItem("IsWard").Controls(0), CheckBox)).Checked Then
            If oSup.IsCompExists Then
                Me.Label1.Text = "Another supplier/competitor has already been defined as Ward."
                Me.Label1.Visible = True
            End If
        ElseIf oSup.Exists(supplierCompetitorName) Then
            Me.Label1.Text = "Supplier/Competitor entered already exists."
            Me.Label1.Visible = True
        Else
            oSup.UpdateSupplierCompetitor(SupplierCompetitorID, supplierCompetitorName, IsComp)
        End If

4 Answers, 1 is accepted

Sort by
0
Sebastian
Telerik team
answered on 15 Oct 2008, 11:50 AM
Hello Steve,

This is rather a generic exception that indicates you attempt to reference an item from a collection with invalid index. Can you please check which exact line in your code generates the exception (debugging it)? Thus we will be able to provide more to-the-point answer/solution for your case.

Best regards,
Stephen
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
0
Steve
Top achievements
Rank 1
answered on 15 Oct 2008, 12:07 PM
I have highlighted the line of code where the error occurs...is there a way I can see the code that is generated by the auto-update...that seems to work fine, but I need to check if the company exist prior to updating. Is there another way to handle this while still using the auto-update or auto-insert?

Protected Sub grdSuppliers_Competetitors_UpdateCommand(ByVal source As Object, ByVal e As Telerik.Web.UI.GridCommandEventArgs) Handles grdSuppliers_Competetitors.UpdateCommand
        Dim oSup As New SupplierCompetitor
        'Get the GridEditableItem of the RadGrid       
        Dim editedItem As GridEditableItem = TryCast(e.Item, GridEditableItem)
        'Get the primary key value using the DataKeyValue.       
        Dim SupplierCompetitorID As String = editedItem.OwnerTableView.DataKeyValues(editedItem.ItemIndex)("SupplierCompetitorID").ToString()
        'Access the textbox from the edit form template and store the values in string variables.       
        'Dim SupplierCompetitorID As String = (TryCast(editedItem("SupplierCompetitorID").Controls(0), TextBox)).Text
        Dim supplierCompetitorName As String = (TryCast(editedItem("supplierCompetitorName").Controls(0), TextBox)).Text
        Dim IsComp As Boolean = (TryCast(editedItem("IsComp").Controls(0), CheckBox)).Checked

        If (TryCast(editedItem("IsComp").Controls(0), CheckBox)).Checked Then
            If oSup.IsCompExists Then
                Me.Label1.Text = "Another supplier/competitor has already been defined as Ward."
                Me.Label1.Visible = True
            End If
        ElseIf oSup.Exists(supplierCompetitorName) Then
            Me.Label1.Text = "Supplier/Competitor entered already exists."
            Me.Label1.Visible = True
        Else
            oSup.UpdateSupplierCompetitor(SupplierCompetitorID, supplierCompetitorName, IsComp)
        End If
0
Accepted
Shinu
Top achievements
Rank 2
answered on 15 Oct 2008, 12:47 PM
Hello Steve,

Can you try replacing the error causing line of code with the code shown below and see if it helps.
cs:
Dim SupplierCompetitorID As String = editedItem.GetDataKeyValue("SupplierCompetitorID").ToString() 

Also check if you have set the DataKeyName Attribute of the MasterTableView as "SupplierCompetitorID".

Thanks
Shinu.
0
Steve
Top achievements
Rank 1
answered on 15 Oct 2008, 01:48 PM
That was it...the DataKeyName was not set.

Thanks, Steve
Tags
Grid
Asked by
Steve
Top achievements
Rank 1
Answers by
Sebastian
Telerik team
Steve
Top achievements
Rank 1
Shinu
Top achievements
Rank 2
Share this question
or