Hi, I get this following error which I don't know what causes it.
I have an editable datagrid where user can edit, update, delete
a specific row n add a new blank row.
the data displayed in datagrid is retrieved fr Library table in database.The primary key is SeqNo which is generated automatically.
Server Error in '/e-library' Application.
--------------------------------------------------------------------------------
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.ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index
Source Error:
Line 88: 'Dim keyValue As String = CStr(DgLibrary.DataKeys(e.Item.ItemIndex))
Line 89: 'Dim Title = DgLibrary.DataKeys(e.Item.ItemIndex)
Line 90: Dim id = DgLibrary.DataKeys(e.Item.ItemIndex)
Line 91: Dim CatID As String = CType(e.Item.Cells(1).Controls(1), TextBox).Text
Line 92: Dim Category As String = CType(e.Item.Cells(2).Controls(1), TextBox).Text
Source File: c:\inetpub\wwwroot\e-library\LibraryRecords.aspx.
vb Line: 90
Stack Trace:
[ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index]
System.Collections.ArrayList.get_Item(Int32 index) +91
System.Web.UI.WebControls.DataKeyCollection.get_It em(Int32 index)
e_library.LibraryRecords.DgLibrary_UpdateCommand(O bject source, DataGridCommandEventArgs e) in c:\inetpub\wwwroot\e-library\LibraryRecords.aspx.
vb:90
System.Web.UI.WebControls.DataGrid.OnUpdateCommand (DataGridCommandEventArgs e)
System.Web.UI.WebControls.DataGrid.OnBubbleEvent(O bject source, EventArgs e)
System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args)
System.Web.UI.WebControls.DataGridItem.OnBubbleEve nt(Object source, EventArgs e)
System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args)
System.Web.UI.WebControls.LinkButton.OnCommand(Com mandEventArgs e)
System.Web.UI.WebControls.LinkButton.System.Web.UI .IPostBackEventHandler.RaisePostBackEvent(String eventArgument)
System.Web.UI.Page.RaisePostBackEvent(IPostBackEve ntHandler sourceControl, String eventArgument)
System.Web.UI.Page.RaisePostBackEvent(NameValueCol lection postData)
System.Web.UI.Page.ProcessRequestMain()
Code:
Private Sub DgLibrary_EditCommand(ByVal [source] As System.Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles DgLibrary.EditCommand
DgLibrary.EditItemIndex = e.Item.ItemIndex
BindData()
End Sub
Private Sub DgLibrary_CancelCommand(ByVal [source] As System.Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles DgLibrary.CancelCommand
DgLibrary.EditItemIndex = -1
BindData()
End Sub
Private Sub DgLibrary_UpdateCommand(ByVal [source] As Object, ByVal e As DataGridCommandEventArgs) Handles DgLibrary.UpdateCommand
Dim id = DgLibrary.DataKeys(e.Item.ItemIndex)
Dim CatID As String = CType(e.Item.Cells(1).Controls(1), TextBox).Text
Dim Category As String = CType(e.Item.Cells(2).Controls(1), TextBox).Text
Dim TitleID As String = CType(e.Item.Cells(3).Controls(1), TextBox).Text
Dim Title As String = CType(e.Item.Cells(4).Controls(1), TextBox).Text
Dim Qty As Integer = CType(e.Item.Cells(5).Controls(1), TextBox).Text
Dim price = e.Item.Cells(6).Text
Dim DBCommand1 As SqlDataAdapter
Dim DStab As New DataSet
Dim con1 As New SqlConnection("data source=localhost; initial catalog=""E-Library Records""; user id=sa")
DBCommand1 = New SqlDataAdapter("Select * from Library Where SeqNo = " & id, con1)
DBCommand1.Fill(DStab, _
"Library")
Dim strSQL As String = "UPDATE [Library] SET [CategoryID] = @CategoryID, [Category]=@Category, [Title_id]=@Title_id, [Title]=@Title, " & _
"[Quantity]=@Quantity, [Price]=@Price " & _
"WHERE [SeqNo] = @id"
Dim con As New SqlConnection("data source=localhost; initial catalog=""E-Library Records""; user id=sa")
con.Open()
Dim myCommand As SqlCommand = New SqlCommand(strSQL, con)
myCommand.CommandType = CommandType.Text
Dim PCatID As SqlParameter = New SqlParameter("@CatID", SqlDbType.Char)
PCatID.Value = CatID
myCommand.Parameters.Add(PCatID)
Dim PCategory As SqlParameter = New SqlParameter("@Category", SqlDbType.VarChar)
PCategory.Value = Category
myCommand.Parameters.Add(PCategory)
Dim PTitleID As SqlParameter = New SqlParameter("@TitleID", SqlDbType.Char)
PTitleID.Value = TitleID
myCommand.Parameters.Add(PTitleID)
Dim PTitle As SqlParameter = New SqlParameter("@Title ", SqlDbType.VarChar)
PTitle.Value = Title
myCommand.Parameters.Add(PTitle)
Dim PQty As SqlParameter = New SqlParameter("@Qty", SqlDbType.Int)
PQty.Value = Qty
myCommand.Parameters.Add(PQty)
Dim pPrice As SqlParameter = New SqlParameter("@Price", SqlDbType.Money)
pPrice.Value = Price
myCommand.Parameters.Add(pPrice)
Dim pSeqNo As SqlParameter = New SqlParameter("@SeqNo", SqlDbType.Int)
pSeqNo.Value = id
myCommand.Parameters.Add(pSeqNo)
myCommand.ExecuteNonQuery() 'Execute the UPDATE query
con.Close()
DgLibrary.EditItemIndex = -1
BindData()
End Sub
Private Sub DgLibrary_ItemCommand(ByVal [source] As Object, ByVal e As DataGridCommandEventArgs) Handles DgLibrary.ItemCommand
If e.CommandName = "Add" Then
Dim CatID As String = CType(e.Item.FindControl("Textbox2"), TextBox).Text
Dim Category As String = CType(e.Item.FindControl("Textbox3"), TextBox).Text
Dim TitleID As String = CType(e.Item.FindControl("Textbox4"), TextBox).Text
Dim Title As String = CType(e.Item.FindControl("Textbox5"), TextBox).Text
Dim Qty As Integer = CType(e.Item.FindControl("Textbox6"), TextBox).Text
Dim Price As Decimal = CType(e.Item.FindControl("TextBox7"), TextBox).Text
Dim strSQL As String = "INSERT INTO [Library] ([CategoryID], [Category], [Title_id], [Title], [Quantity], [Price]) " _
& " VALUES ([@CategoryID], [@Category], [@Title_id], [@Title], [@Quantity], [@Price])"
Dim myCommand As SqlCommand
Dim con As New SqlConnection("data source=localhost; initial catalog=""E-Library Records""; user id=sa")
myCommand = New SqlCommand(strSQL, con)
Dim PCatID As SqlParameter = New SqlParameter("@CatID", SqlDbType.Char)
PCatID.Value = CatID
myCommand.Parameters.Add(PCatID)
Dim PCategory As SqlParameter = New SqlParameter("@Category", SqlDbType.VarChar)
PCategory.Value = Category
myCommand.Parameters.Add(PCategory)
Dim PTitleID As SqlParameter = New SqlParameter("@TitleID", SqlDbType.Char)
PTitleID.Value = TitleID
myCommand.Parameters.Add(PTitleID)
Dim PTitle As SqlParameter = New SqlParameter("@Title ", SqlDbType.VarChar)
PTitle.Value = Title
myCommand.Parameters.Add(PTitle)
Dim PQty As SqlParameter = New SqlParameter("@Qty", SqlDbType.Int)
PQty.Value = Qty
myCommand.Parameters.Add(PQty)
Dim pPrice As SqlParameter = New SqlParameter("@Price", SqlDbType.Money)
pPrice.Value = Price
myCommand.Parameters.Add(pPrice)
con.Open()
myCommand.ExecuteNonQuery() 'execute INSERT query
con.Close()
Dim strMessage1 As String
strMessage1 = "Sucessfully Task Added ."
Dim strScript1 As String = "<script language=JavaScript>"
strScript1 += "alert(""" & strMessage1 & """);"
strScript1 += "</script>"
If (Not Page.IsStartupScriptRegistered("clientScript")) Then
Page.RegisterStartupScript("clientScript", strScript1)
End If
End If
DgLibrary.EditItemIndex = -1
BindData()
End Sub
Private Sub DgLibrary_pagech(ByVal [source] As Object, ByVal e As DataGridPageChangedEventArgs) Handles DgLibrary.PageIndexChanged
DgLibrary.EditItemIndex = -1
DgLibrary.CurrentPageIndex = e.NewPageIndex
BindData()
End Sub
Can somebody help me? Thanks!
Irene