Need help converting GridViewCommandEventArg CommandArgument to an integer


K

keithb

I have a command button in GridView row that fires the RowCommand Event when
clicked. The event handler looks like this:

protected void grid_RowCommand(Object sender, GridViewCommandEventArgs e)
{
int rowIndex = Convert.ToInt32(e.CommandArgument);

When this code runs it triggers an "Input string was not in a correct
format" error.

According to Microsoft documentation, the rowindex value is an integer, so I
do not know how to troublshoot this problem. Can someone offer a suggestion?

Thanks,

Keith

PS Here is the error message with stack trace info in case that is useful:

Input string was not in a correct format.
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.FormatException: Input string was not in a correct
format.

Source Error:


Line 107: // Convert the row index stored in the CommandArgument
Line 108: // property to an Integer.
Line 109: int rowIndex = Convert.ToInt32(e.CommandArgument);
Line 110: // Retrieve the row that contains the button clicked
Line 111: // by the user from the Rows collection.


Source File: c:\local_websites\btm_root\UserControls\Rem.ascx.cs Line:
109

Stack Trace:


[FormatException: Input string was not in a correct format.]
System.Number.StringToNumber(String str, NumberStyles options,
NumberBuffer& number, NumberFormatInfo info, Boolean parseDecimal) +2753539
System.Number.ParseInt32(String s, NumberStyles style, NumberFormatInfo
info) +102
System.String.System.IConvertible.ToInt32(IFormatProvider provider) +43
System.Convert.ToInt32(Object value) +25
UserControls_Rem.grid_RowCommand(Object sender, GridViewCommandEventArgs
e) in c:\local_websites\btm_root\UserControls\Rem.ascx.cs:109
System.Web.UI.WebControls.GridView.OnRowCommand(GridViewCommandEventArgs
e) +96
System.Web.UI.WebControls.GridView.HandleEvent(EventArgs e, Boolean
causesValidation, String validationGroup) +121
System.Web.UI.WebControls.GridView.OnBubbleEvent(Object source, EventArgs
e) +162
System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args) +56
System.Web.UI.WebControls.GridViewRow.OnBubbleEvent(Object source,
EventArgs e) +118
System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args) +56
System.Web.UI.WebControls.ImageButton.OnCommand(CommandEventArgs e) +106
System.Web.UI.WebControls.ImageButton.RaisePostBackEvent(String
eventArgument) +200
System.Web.UI.WebControls.ImageButton.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String
eventArgument) +31
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler
sourceControl, String eventArgument) +32
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +72
System.Web.UI.Page.ProcessRequestMain(Boolean
includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +3839
 
Ad

Advertisements

K

keithb

Replying to my own question for the benefit of other newbies: It turns out
that the example I was following was wrong. GridViewCommandEventArgs does
not contain row index information. It is necessary to pass identifying
information as a CommandArgument.

Keith

keithb said:
I have a command button in GridView row that fires the RowCommand Event
when clicked. The event handler looks like this:

protected void grid_RowCommand(Object sender, GridViewCommandEventArgs e)
{
int rowIndex = Convert.ToInt32(e.CommandArgument);

When this code runs it triggers an "Input string was not in a correct
format" error.

According to Microsoft documentation, the rowindex value is an integer, so
I do not know how to troublshoot this problem. Can someone offer a
suggestion?

Thanks,

Keith

PS Here is the error message with stack trace info in case that is useful:

Input string was not in a correct format.
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.FormatException: Input string was not in a
correct format.

Source Error:


Line 107: // Convert the row index stored in the CommandArgument
Line 108: // property to an Integer.
Line 109: int rowIndex = Convert.ToInt32(e.CommandArgument);
Line 110: // Retrieve the row that contains the button clicked
Line 111: // by the user from the Rows collection.


Source File: c:\local_websites\btm_root\UserControls\Rem.ascx.cs Line:
109

Stack Trace:


[FormatException: Input string was not in a correct format.]
System.Number.StringToNumber(String str, NumberStyles options,
NumberBuffer& number, NumberFormatInfo info, Boolean parseDecimal)
+2753539
System.Number.ParseInt32(String s, NumberStyles style, NumberFormatInfo
info) +102
System.String.System.IConvertible.ToInt32(IFormatProvider provider) +43
System.Convert.ToInt32(Object value) +25
UserControls_Rem.grid_RowCommand(Object sender, GridViewCommandEventArgs
e) in c:\local_websites\btm_root\UserControls\Rem.ascx.cs:109
System.Web.UI.WebControls.GridView.OnRowCommand(GridViewCommandEventArgs
e) +96
System.Web.UI.WebControls.GridView.HandleEvent(EventArgs e, Boolean
causesValidation, String validationGroup) +121
System.Web.UI.WebControls.GridView.OnBubbleEvent(Object source,
EventArgs e) +162
System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args)
+56
System.Web.UI.WebControls.GridViewRow.OnBubbleEvent(Object source,
EventArgs e) +118
System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args)
+56
System.Web.UI.WebControls.ImageButton.OnCommand(CommandEventArgs e) +106
System.Web.UI.WebControls.ImageButton.RaisePostBackEvent(String
eventArgument) +200

System.Web.UI.WebControls.ImageButton.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String
eventArgument) +31
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler
sourceControl, String eventArgument) +32
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +72
System.Web.UI.Page.ProcessRequestMain(Boolean
includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +3839
 
Ad

Advertisements

Joined
May 22, 2008
Messages
1
Reaction score
0
solution for the commandargument error

<%@ Page language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">

void CustomersGridView_RowCommand(Object sender, GridViewCommandEventArgs e)
{
// If multiple buttons are used in a GridView control, use the
// CommandName property to determine which button was clicked.
if(e.CommandName=="Add")
{
// Convert the row index stored in the CommandArgument
// property to an Integer.
int index = Convert.ToInt32(e.CommandArgument);

// Retrieve the row that contains the button clicked
// by the user from the Rows collection.
GridViewRow row = CustomersGridView.Rows[index];

// Create a new ListItem object for the customer in the row.
ListItem item = new ListItem();
item.Text = Server.HtmlDecode(row.Cells[2].Text);

// If the customer is not already in the ListBox, add the ListItem
// object to the Items collection of the ListBox control.
if (!CustomersListBox.Items.Contains(item))
{
CustomersListBox.Items.Add(item);
}
}
}

void CustomersGridView_RowCreated(Object sender, GridViewRowEventArgs e)
{

// The GridViewCommandEventArgs class does not contain a
// property that indicates which row's command button was
// clicked. To identify which row's button was clicked, use
// the button's CommandArgument property by setting it to the
// row's index.
if(e.Row.RowType == DataControlRowType.DataRow)
{
// Retrieve the LinkButton control from the first column.
LinkButton addButton = (LinkButton)e.Row.Cells[0].Controls[0];

// Set the LinkButton's CommandArgument property with the
// row's index.
addButton.CommandArgument = e.Row.RowIndex.ToString();
}

}

</script>

<html >
<head runat="server">
<title>GridView RowCommand Example</title>
</head>
<body>
<form id="form1" runat="server">

<h3>GridView RowCommand Example</h3>

<table width="100%">
<tr>
<td style="width:50%">

<asp:gridview id="CustomersGridView"
datasourceid="CustomersSource"
allowpaging="true"
autogeneratecolumns="false"
onrowcommand="CustomersGridView_RowCommand"
onrowcreated="CustomersGridView_RowCreated"
runat="server">

<columns>
<asp:buttonfield buttontype="Link"
commandname="Add"
text="Add"/>
<asp:boundfield datafield="CustomerID"
headertext="Customer ID"/>
<asp:boundfield datafield="CompanyName"
headertext="Company Name"/>
<asp:boundfield datafield="City"
headertext="City"/>
</columns>

</asp:gridview>

</td>

<td style="vertical-align:top; width:50%">

Customers: <br/>
<asp:listbox id="CustomersListBox"
runat="server"/>

</td>
</tr>
</table>

<!-- This example uses Microsoft SQL Server and connects -->
<!-- to the Northwind sample database. Use an ASP.NET -->
<!-- expression to retrieve the connection string value -->
<!-- from the Web.config file. -->
<asp:sqldatasource id="CustomersSource"
selectcommand="Select [CustomerID], [CompanyName], [City] From [Customers]"
connectionstring="<%$ ConnectionStrings:NorthWindConnectionString%>"
runat="server"/>

</form>
</body>
</html>
 

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

Top