Stupid undocumented grid indexing . . .

Discussion in 'ASP .Net' started by Lauchlan M, May 20, 2004.

  1. Lauchlan M

    Lauchlan M Guest

    Hi

    I have a grid, I want to delete a row.

    The relevant code is something like

    private void dGridErrorLog_DeleteCommand(object source,
    System.Web.UI.WebControls.DataGridCommandEventArgs e)
    {

    // delete the row
    nxCmdDeleteErrorLogItem.Parameters["@LogID"].Value =
    dGridErrorLog.DataKeys[e.Item.ItemIndex];
    nxConnection.Open();
    nxCmdDeleteErrorLogItem.ExecuteNonQuery();
    nxConnection.Close();

    // rebind the grid:
    nxConnection.Open();
    nxDataAdapterErrorLog.Fill(nxDataAdapterErrorLog_DataSet, "errorlog");
    dGridErrorLog.DataSource = nxDataAdapterErrorLog_DataSet;
    dGridErrorLog.DataMember = "errorlog";
    dGridErrorLog.DataBind();
    nxConnection.Close();

    }

    The grid has four rows, which I can see return values for e.Item.ItemIndex
    of 0, 1, 2, 3 depending on which row I click on. But calculating

    dGridErrorLog.DataKeys[e.Item.ItemIndex];

    gives me errors like

    <<

    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 347: private void dGridErrorLog_DeleteCommand(object source,
    System.Web.UI.WebControls.DataGridCommandEventArgs e)
    Line 348: {
    Line 349: lblTest.Text =
    dGridErrorLog.DataKeys[e.Item.ItemIndex].ToString();

    Source File:
    e:\inetpub\wwwroot\Mywebinterface_nexusdb\admin\viewerrorlog.aspx.cs
    Line: 349

    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_Item(Int32 index) +10

    MyWebInterface_NexusDB.Admin.ViewErrorLog.dGridErrorLog_DeleteCommand(Object
    source, DataGridCommandEventArgs e) in
    e:\inetpub\wwwroot\Mywebinterface_nexusdb\admin\viewerrorlog.aspx.cs:349

    System.Web.UI.WebControls.DataGrid.OnDeleteCommand(DataGridCommandEventArgs
    e) +110
    System.Web.UI.WebControls.DataGrid.OnBubbleEvent(Object source, EventArgs
    e) +589
    System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args) +26
    System.Web.UI.WebControls.DataGridItem.OnBubbleEvent(Object source,
    EventArgs e) +106
    System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args) +26
    System.Web.UI.WebControls.LinkButton.OnCommand(CommandEventArgs e) +121

    System.Web.UI.WebControls.LinkButton.System.Web.UI.IPostBackEventHandler.Rai
    sePostBackEvent(String eventArgument) +115
    System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler
    sourceControl, String eventArgument) +18
    System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +138
    System.Web.UI.Page.ProcessRequestMain() +1277

    >>


    Any suggestions?

    Thanks!

    Lauchlan M
     
    Lauchlan M, May 20, 2004
    #1
    1. Advertising

  2. "Lauchlan M" <> wrote in message
    news:...
    > Hi
    >
    > I have a grid, I want to delete a row.
    >
    > The relevant code is something like
    >
    > private void dGridErrorLog_DeleteCommand(object source,
    > System.Web.UI.WebControls.DataGridCommandEventArgs e)
    > {
    >
    > // delete the row
    > nxCmdDeleteErrorLogItem.Parameters["@LogID"].Value =
    > dGridErrorLog.DataKeys[e.Item.ItemIndex];
    > nxConnection.Open();
    > nxCmdDeleteErrorLogItem.ExecuteNonQuery();
    > nxConnection.Close();
    >
    > // rebind the grid:
    > nxConnection.Open();
    > nxDataAdapterErrorLog.Fill(nxDataAdapterErrorLog_DataSet, "errorlog");
    > dGridErrorLog.DataSource = nxDataAdapterErrorLog_DataSet;
    > dGridErrorLog.DataMember = "errorlog";
    > dGridErrorLog.DataBind();
    > nxConnection.Close();
    >
    > }


    Should you be clearing nxDataAdapterErrorLog_DataSet before filling it
    again?
    --
    John Saunders
     
    John Saunders, May 20, 2004
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. C
    Replies:
    0
    Views:
    514
  2. Brandon McCombs
    Replies:
    4
    Views:
    525
    Richard Wheeldon
    Aug 28, 2006
  3. Emin
    Replies:
    4
    Views:
    419
    Paul McGuire
    Jan 12, 2007
  4. Skybuck Flying
    Replies:
    30
    Views:
    1,125
    Bill Reid
    Sep 19, 2011
  5. C
    Replies:
    3
    Views:
    233
    Manohar Kamath [MVP]
    Oct 17, 2003
Loading...

Share This Page