Cheers!
While I'm doing that can you see anything wrong witht the code, or is this a common thing with a simple fix?
Main Topics
Browse All TopicsHi there,
I am using the delete command within ASP DataGrid. When I click the delete link on the datagrid, I get the following error:
<<Index was out of range. Must be non-negative and less than the size of the collection. Parameter name index>>
The error appears on line 411. I have included both the unhandled exception detail, and the complete application script.
Can you help me dubug, I'm lost trying to fix this one.
Cheers - LoveToSpod
--------------------------
UNHANDLED EXEPTION DETAIL - START
--------------------------
Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.ArgumentOutOfRangeE
Source Error:
Line 409: Private Sub DataGrid1_DeleteCommand(By
Line 410:
Line 411: SqlCommand2.Parameters("@I
Line 412: SqlConnection1.Open()
Line 413: SqlCommand2.ExecuteNonQuer
Source File: c:\inetpub\wwwroot\Technic
Stack Trace:
[ArgumentOutOfRangeExcepti
Parameter name: index]
System.Collections.ArrayLi
System.Web.UI.WebControls.
TechnicalCourier.EditTechC
System.Web.UI.WebControls.
System.Web.UI.WebControls.
System.Web.UI.Control.Rais
System.Web.UI.WebControls.
System.Web.UI.Control.Rais
System.Web.UI.WebControls.
System.Web.UI.WebControls.
System.Web.UI.Page.RaisePo
System.Web.UI.Page.RaisePo
System.Web.UI.Page.Process
--------------------------
UNHANDLED EXEPTION DETAIL - END
--------------------------
--------------------------
APPLICATION SCRIPT - START
--------------------------
Public Class EditTechCourier
Inherits System.Web.UI.Page
Public SortValue As String
Protected WithEvents SqlCommand2 As System.Data.SqlClient.SqlC
Public StrCurrentPageIndex As String
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here
If Not Page.IsPostBack Then
BindGrid("[BusinessUnit]")
End If
End Sub
Sub BindGrid(ByVal strSort As String)
SqlDataAdapter1.Fill(DataS
DataView1 = DataSet1.Tables(0).Default
DataView1.Sort = strSort
DataGrid1.DataSource = DataSet1
DataGrid1.DataBind()
End Sub
Private Sub DataGrid1_EditCommand(ByVa
DataGrid1.EditItemIndex = e.Item.ItemIndex
BindGrid(SortValue)
End Sub
Private Sub DataGrid1_CancelCommand(By
DataGrid1.EditItemIndex = -1
BindGrid(SortValue)
End Sub
Private Sub DataGrid1_UpdateCommand(By
'Retrieve textbox controls from datagrid
'Dim txtBusinessUnit As TextBox
'Dim txtCountry As TextBox
Dim txtMinDelCost As TextBox
Dim txtDelCostLoadKM As TextBox
Dim txtDelSchedCost As TextBox
Dim txtSetDelTime As TextBox
Dim txtSetDelWindow As TextBox
Dim txtWeekendSvc As TextBox
Dim txtUnpack As TextBox
Dim txtPack As TextBox
Dim txtSvcConnect As TextBox
Dim txtSvcDisconnect As TextBox
Dim txtSWAPUnpackPack As TextBox
Dim txtSWAPDisconRecon As TextBox
Dim txtSvcProvider As TextBox
Dim txtSvcMode As TextBox
Dim txtAddTimeCost As TextBox
Dim txtComments As TextBox
'txtBusinessUnit = CType(e.Item.Cells(2).Cont
'txtCountry = CType(e.Item.Cells(3).Cont
txtMinDelCost = CType(e.Item.Cells(4).Cont
txtDelCostLoadKM = CType(e.Item.Cells(5).Cont
txtDelSchedCost = CType(e.Item.Cells(6).Cont
txtSetDelTime = CType(e.Item.Cells(7).Cont
txtSetDelWindow = CType(e.Item.Cells(8).Cont
txtWeekendSvc = CType(e.Item.Cells(9).Cont
txtUnpack = CType(e.Item.Cells(10).Con
txtPack = CType(e.Item.Cells(11).Con
txtSvcConnect = CType(e.Item.Cells(12).Con
txtSvcDisconnect = CType(e.Item.Cells(13).Con
txtSWAPUnpackPack = CType(e.Item.Cells(14).Con
txtSWAPDisconRecon = CType(e.Item.Cells(15).Con
txtSvcProvider = CType(e.Item.Cells(16).Con
txtSvcMode = CType(e.Item.Cells(17).Con
txtAddTimeCost = CType(e.Item.Cells(18).Con
txtComments = CType(e.Item.Cells(19).Con
'Assign params to sqlcommand
'SqlCommand1.Parameters("@
'SqlCommand1.Parameters("@
SqlCommand1.Parameters("@M
SqlCommand1.Parameters("@D
SqlCommand1.Parameters("@D
SqlCommand1.Parameters("@S
SqlCommand1.Parameters("@S
SqlCommand1.Parameters("@W
SqlCommand1.Parameters("@U
SqlCommand1.Parameters("@P
SqlCommand1.Parameters("@S
SqlCommand1.Parameters("@S
SqlCommand1.Parameters("@S
SqlCommand1.Parameters("@S
SqlCommand1.Parameters("@S
SqlCommand1.Parameters("@S
SqlCommand1.Parameters("@A
SqlCommand1.Parameters("@C
SqlCommand1.Parameters("@I
'execute sqlcommand
SqlConnection1.Open()
SqlCommand1.ExecuteNonQuer
'Deselct row for editing
DataGrid1.EditItemIndex = -1
BindGrid(SortValue)
SqlConnection1.Close()
End Sub
Private Sub DataGrid1_PageIndexChanged
DataGrid1.CurrentPageIndex
StrCurrentPageIndex = e.NewPageIndex
DataGrid1.EditItemIndex = -1
SqlDataAdapter1.Fill(DataS
DataView1 = DataSet1.Tables(0).Default
DataView1.Sort = SortValue
DataGrid1.DataSource = DataSet1
DataGrid1.DataKeyField = "ID"
DataGrid1.DataBind()
End Sub
Sub DataGrid1_SortCommand(ByVa
SortValue = e.SortExpression
BindGrid(SortValue)
End Sub
Private Sub DataGrid1_SelectedIndexCha
BindGrid(SortValue)
End Sub
Private Sub DataGrid1_DeleteCommand(By
SqlCommand2.Parameters("@I
SqlConnection1.Open()
SqlCommand2.ExecuteNonQuer
BindGrid(SortValue)
SqlConnection1.Close()
End Sub
End Class
--------------------------
APPLICATION SCRIPT - END
--------------------------
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
Hai,
You r using DataGrid1.DataKeys(e.Item.
DataKeys() collection is populated only if u assign the DataKeyfield property while binding the grid
ie DataGrid1.DataKeyField = "ID" .. before binding the grid..
but in u r code.. u r binding the grid in the page_load event..u r using the funtion
Sub BindGrid(ByVal strSort As String)
SqlDataAdapter1.Fill(DataS
DataView1 = DataSet1.Tables(0).Default
DataView1.Sort = strSort
DataGrid1.DataSource = DataSet1
DataGrid1.DataBind()
End Sub
but u have not set the DataKeyField value here..
so try the code DataGrid1.DataKeyField = "ID" in the bindgrid funtion before DataBind()
Sub BindGrid(ByVal strSort As String)
SqlDataAdapter1.Fill(DataS
DataView1 = DataSet1.Tables(0).Default
DataView1.Sort = strSort
DataGrid1.DataSource = DataSet1
DataGrid1.DataKeyField = "ID"
DataGrid1.DataBind()
End Sub
Also just ensure at the point of deletecommand.. what's the value in the DataGrid1.DataKeys() collection
I think u have only one primary key/ ID for that row.. so try using
DataGrid1.DataKeys(0) in the delete command instead of DataGrid1.DataKeys(e.Item.
hope this helps u..
regards
Vinodh
Business Accounts
Answer for Membership
by: SandeepRRPosted on 2006-06-02 at 03:51:46ID: 16815475
You can try with putting breakPoint on that statement and open the
Immediate Windows
and type e.Item.ItemIndex in that and hit the enter
u will get the current value for the INdex and see whether it is less than 0 or grtaer than the total no of rows in the grid
How to open Immediate Windows
Menu Debud>Windows>Immediate
by this atleast u can know the why the error is thr
SandeepRR