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
 
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
 

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