Stupid undocumented grid indexing . . .

L

Lauchlan M

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
 
J

John Saunders

Lauchlan M said:
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?
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,228
Members
46,818
Latest member
SapanaCarpetStudio

Latest Threads

Top