Hi 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
xception: Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index
Source Error:
Line 409: Private Sub DataGrid1_DeleteCommand(By
Val source As Object, ByVal e As System.Web.UI.WebControls.
DataGridCo
mmandEvent
Args) Handles DataGrid1.DeleteCommand
Line 410:
Line 411: SqlCommand2.Parameters("@I
D").Value = DataGrid1.DataKeys(e.Item.
ItemIndex)
Line 412: SqlConnection1.Open()
Line 413: SqlCommand2.ExecuteNonQuer
y()
Source File: c:\inetpub\wwwroot\Technic
alCourier\
EditTechCo
urier.aspx
.vb Line: 411
Stack Trace:
[ArgumentOutOfRangeExcepti
on: Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index]
System.Collections.ArrayLi
st.get_Ite
m(Int32 index) +91
System.Web.UI.WebControls.
DataKeyCol
lection.ge
t_Item(Int
32 index) +10
TechnicalCourier.EditTechC
ourier.Dat
aGrid1_Del
eteCommand
(Object source, DataGridCommandEventArgs e) in c:\inetpub\wwwroot\Technic
alCourier\
EditTechCo
urier.aspx
.vb:411
System.Web.UI.WebControls.
DataGrid.O
nDeleteCom
mand(DataG
ridCommand
EventArgs e)
System.Web.UI.WebControls.
DataGrid.O
nBubbleEve
nt(Object source, EventArgs e) +589
System.Web.UI.Control.Rais
eBubbleEve
nt(Object source, EventArgs args) +26
System.Web.UI.WebControls.
DataGridIt
em.OnBubbl
eEvent(Obj
ect source, EventArgs e) +106
System.Web.UI.Control.Rais
eBubbleEve
nt(Object source, EventArgs args) +26
System.Web.UI.WebControls.
LinkButton
.OnCommand
(CommandEv
entArgs e) +121
System.Web.UI.WebControls.
LinkButton
.System.We
b.UI.IPost
BackEventH
andler.Rai
sePostBack
Event(Stri
ng eventArgument) +115
System.Web.UI.Page.RaisePo
stBackEven
t(IPostBac
kEventHand
ler sourceControl, String eventArgument) +18
System.Web.UI.Page.RaisePo
stBackEven
t(NameValu
eCollectio
n postData) +138
System.Web.UI.Page.Process
RequestMai
n() +1277
--------------------------
----------
----------
----------
----------
--
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
ommand
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
et1)
DataView1 = DataSet1.Tables(0).Default
View
DataView1.Sort = strSort
DataGrid1.DataSource = DataSet1
DataGrid1.DataBind()
End Sub
Private Sub DataGrid1_EditCommand(ByVa
l source As Object, ByVal e As System.Web.UI.WebControls.
DataGridCo
mmandEvent
Args) Handles DataGrid1.EditCommand
DataGrid1.EditItemIndex = e.Item.ItemIndex
BindGrid(SortValue)
End Sub
Private Sub DataGrid1_CancelCommand(By
Val source As Object, ByVal e As System.Web.UI.WebControls.
DataGridCo
mmandEvent
Args) Handles DataGrid1.CancelCommand
DataGrid1.EditItemIndex = -1
BindGrid(SortValue)
End Sub
Private Sub DataGrid1_UpdateCommand(By
Val source As Object, ByVal e As System.Web.UI.WebControls.
DataGridCo
mmandEvent
Args) Handles DataGrid1.UpdateCommand
'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
rols(0), TextBox)
'txtCountry = CType(e.Item.Cells(3).Cont
rols(0), TextBox)
txtMinDelCost = CType(e.Item.Cells(4).Cont
rols(0), TextBox)
txtDelCostLoadKM = CType(e.Item.Cells(5).Cont
rols(0), TextBox)
txtDelSchedCost = CType(e.Item.Cells(6).Cont
rols(0), TextBox)
txtSetDelTime = CType(e.Item.Cells(7).Cont
rols(0), TextBox)
txtSetDelWindow = CType(e.Item.Cells(8).Cont
rols(0), TextBox)
txtWeekendSvc = CType(e.Item.Cells(9).Cont
rols(0), TextBox)
txtUnpack = CType(e.Item.Cells(10).Con
trols(0), TextBox)
txtPack = CType(e.Item.Cells(11).Con
trols(0), TextBox)
txtSvcConnect = CType(e.Item.Cells(12).Con
trols(0), TextBox)
txtSvcDisconnect = CType(e.Item.Cells(13).Con
trols(0), TextBox)
txtSWAPUnpackPack = CType(e.Item.Cells(14).Con
trols(0), TextBox)
txtSWAPDisconRecon = CType(e.Item.Cells(15).Con
trols(0), TextBox)
txtSvcProvider = CType(e.Item.Cells(16).Con
trols(0), TextBox)
txtSvcMode = CType(e.Item.Cells(17).Con
trols(0), TextBox)
txtAddTimeCost = CType(e.Item.Cells(18).Con
trols(0), TextBox)
txtComments = CType(e.Item.Cells(19).Con
trols(0), TextBox)
'Assign params to sqlcommand
'SqlCommand1.Parameters("@
BusinessUn
it").Value
= txtBusinessUnit.Text
'SqlCommand1.Parameters("@
Country").
Value = txtCountry.Text
SqlCommand1.Parameters("@M
inDelCost"
).Value = txtMinDelCost.Text
SqlCommand1.Parameters("@D
elCostLoad
KM").Value
= txtDelCostLoadKM.Text
SqlCommand1.Parameters("@D
elSchedCos
t").Value = txtDelSchedCost.Text
SqlCommand1.Parameters("@S
etDelTime"
).Value = txtSetDelWindow.Text
SqlCommand1.Parameters("@S
etDelWindo
w").Value = txtSetDelWindow.Text
SqlCommand1.Parameters("@W
eekendSvc"
).Value = txtWeekendSvc.Text
SqlCommand1.Parameters("@U
npack").Va
lue = txtUnpack.Text
SqlCommand1.Parameters("@P
ack").Valu
e = txtPack.Text
SqlCommand1.Parameters("@S
vcConnect"
).Value = txtSvcConnect.Text
SqlCommand1.Parameters("@S
vcDisconne
ct").Value
= txtSvcDisconnect.Text
SqlCommand1.Parameters("@S
WAPUnpackP
ack").Valu
e = txtSWAPUnpackPack.Text
SqlCommand1.Parameters("@S
WAPDisconR
econ").Val
ue = txtSWAPDisconRecon.Text
SqlCommand1.Parameters("@S
vcProvider
").Value = txtSvcProvider.Text
SqlCommand1.Parameters("@S
vcMode").V
alue = txtSvcMode.Text
SqlCommand1.Parameters("@A
ddTimeCost
").Value = txtAddTimeCost.Text
SqlCommand1.Parameters("@C
omments").
Value = txtComments.Text
SqlCommand1.Parameters("@I
D").Value = DataGrid1.DataKeys(e.Item.
ItemIndex)
'execute sqlcommand
SqlConnection1.Open()
SqlCommand1.ExecuteNonQuer
y()
'Deselct row for editing
DataGrid1.EditItemIndex = -1
BindGrid(SortValue)
SqlConnection1.Close()
End Sub
Private Sub DataGrid1_PageIndexChanged
(ByVal source As Object, ByVal e As System.Web.UI.WebControls.
DataGridPa
geChangedE
ventArgs) Handles DataGrid1.PageIndexChanged
DataGrid1.CurrentPageIndex
= e.NewPageIndex
StrCurrentPageIndex = e.NewPageIndex
DataGrid1.EditItemIndex = -1
SqlDataAdapter1.Fill(DataS
et1)
DataView1 = DataSet1.Tables(0).Default
View
DataView1.Sort = SortValue
DataGrid1.DataSource = DataSet1
DataGrid1.DataKeyField = "ID"
DataGrid1.DataBind()
End Sub
Sub DataGrid1_SortCommand(ByVa
l source As Object, ByVal e As System.Web.UI.WebControls.
DataGridSo
rtCommandE
ventArgs) Handles DataGrid1.SortCommand
SortValue = e.SortExpression
BindGrid(SortValue)
End Sub
Private Sub DataGrid1_SelectedIndexCha
nged(ByVal
sender As Object, ByVal e As System.EventArgs) Handles DataGrid1.SelectedIndexCha
nged
BindGrid(SortValue)
End Sub
Private Sub DataGrid1_DeleteCommand(By
Val source As Object, ByVal e As System.Web.UI.WebControls.
DataGridCo
mmandEvent
Args) Handles DataGrid1.DeleteCommand
SqlCommand2.Parameters("@I
D").Value = DataGrid1.DataKeys(e.Item.
ItemIndex)
SqlConnection1.Open()
SqlCommand2.ExecuteNonQuer
y()
BindGrid(SortValue)
SqlConnection1.Close()
End Sub
End Class
--------------------------
----------
----------
----------
----------
--
APPLICATION SCRIPT - END
--------------------------
----------
----------
----------
----------
--
Start Free Trial