Wrox Programmer Forums
|
ASP.NET 1.0 and 1.1 Basics ASP.NET discussion for users new to coding in ASP.NET 1.0 or 1.1. NOT for the older "classic" ASP 3 or the newer ASP.NET 2.0.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 1.0 and 1.1 Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old June 9th, 2005, 11:25 PM
Authorized User
 
Join Date: Apr 2005
Posts: 60
Thanks: 0
Thanked 0 Times in 0 Posts
Default DataKey error in Editable datagrid

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





 
Old June 10th, 2005, 01:40 AM
Authorized User
 
Join Date: Jul 2004
Posts: 88
Thanks: 0
Thanked 0 Times in 0 Posts
Default

When do you get this error?


 
Old June 10th, 2005, 10:51 PM
Authorized User
 
Join Date: Apr 2005
Posts: 60
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I get it when i edit the specific row, then click update link button.






Similar Threads
Thread Thread Starter Forum Replies Last Post
Can't read values from editable datagrid collie ASP.NET 1.x and 2.0 Application Design 5 May 9th, 2007 11:16 AM
editable datagrid column sarah lee ASP.NET 1.0 and 1.1 Basics 3 September 7th, 2006 02:45 PM
editable datagrid .............help maddy137 ASP.NET 1.0 and 1.1 Professional 0 May 17th, 2006 07:33 PM
Error aout datagrid.datakey(i) Blueman137 ASP.NET 1.0 and 1.1 Basics 0 March 21st, 2004 09:37 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.