Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Index was out of range

Status
Not open for further replies.

sila

Technical User
Aug 8, 2003
76
GB
Hi
I'm getting the following error for the code below it:
----------------------------------------------------------
Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index
----------------------------------------------------------
Private Sub dGridTypes_UpdateCommand(ByVal source As System.Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles dGridTypes.UpdateCommand

Dim txtCode As TextBox
Dim txtDescription As TextBox

'store textbox values into variables
txtCode = CType(e.Item.Cells(0).Controls(0), TextBox)
txtDescription = CType(e.Item.Cells(1).Controls(0), TextBox)


'Assign Parameters to SqlCommand
SqlComm_UpdateType.Parameters("@Code").Value = txtCode.Text
SqlComm_UpdateType.Parameters("@Description").Value = txtDescription.Text

SqlComm_UpdateType.Parameters("@TypeID").Value = dGridTypes.DataKeys(e.Item.ItemIndex)


'Execute SqlCommand
Me.SqlConnection1.Open()
SqlComm_UpdateType.ExecuteNonQuery()


'Deselect Row for editing
dGridTypes.EditItemIndex = -1
BindGrid()
Me.SqlConnection1.Close()

End Sub

Any help greatly appreciated.
 
DataKeys is an object that could be a proble.

SqlComm_UpdateType.Parameters("@TypeID").Value = dGridTypes.DataKeys(e.Item.ItemIndex).ToString()

Marty
 
I've just tried that and its still showing the same error message unfortunately. Any other ideas?

Thanks.
 
which line throws the error? should give a line number.
 
Which line of code is causing the error?


I don't see where you add the parameters to the command object.

This line adds the parameter and sets the value of it all at the same time
SqlComm_UpdateType.Parameters.Add("@Code", CType(e.Item.Cells(0).Controls(0), TextBox).Text)

 
Line 143 after I added your code to the end:

Line 141:
Line 142: 'SqlComm_UpdateType.Parameters("@TypeID").Value = dGridTypes.DataKeys(e.Item.ItemIndex)
Line 143: SqlComm_UpdateType.Parameters("@TypeID").Value = dGridTypes.DataKeys(e.Item.ItemIndex).ToString()
Line 144:
Line 145:
 
Region " Web Form Designer Generated Code "

'This call is required by the Web Form Designer.
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.SqlConnection1 = New System.Data.SqlClient.SqlConnection
Me.SqlComm_SelectType = New System.Data.SqlClient.SqlCommand
Me.SqlComm_InsertType = New System.Data.SqlClient.SqlCommand
Me.SqlComm_UpdateType = New System.Data.SqlClient.SqlCommand
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top