Click here to Skip to main content
15,881,248 members
Please Sign up or sign in to vote.
3.00/5 (3 votes)
See more:
unhandled exception input string was not in a correct format when click on gridview row,
code work on other machine.
Code:
C#
int index = Convert.ToInt32(e.CommandName.ToString());
txtname.Text = Convert.ToString(gridview1.Rows[index].Cells[2].Text.ToString());


Following Exception Details:
Server Error in '/' Application.
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:
An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

Stack Trace:
[FormatException: Input string was not in a correct format.]
System.Number.StringToNumber(String str, NumberStyles options, NumberBuffer& number, NumberFormatInfo info, Boolean parseDecimal) +9594283
System.Number.ParseInt32(String s, NumberStyles style, NumberFormatInfo info) +119
System.Convert.ToInt32(String value, IFormatProvider provider) +48
System.Web.UI.WebControls.GridView.HandleEvent(EventArgs e, Boolean causesValidation, String validationGroup) +1047
System.Web.UI.WebControls.GridView.RaisePostBackEvent(String eventArgument) +210
System.Web.UI.WebControls.GridView.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +13
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +13
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +176
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +5563


Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.1

Thanks
Posted
Updated 14-Oct-13 4:09am
v2
Comments
ZurdoDev 14-Oct-13 10:25am    
The error explains that you are trying to convert something to int that is not an int. Likely Convert.ToInt32(e.CommandName.ToString()); CommandName will be "Edit", "Delete" or something like that so you can't convert to int.
Mahesh_Bhosale 14-Oct-13 10:29am    
but iam not trying to convert anything to int that is not an int.
these work on another computer but not working on my machine, i trouble from this .
ZurdoDev 14-Oct-13 10:58am    
What is e.CommandName.ToString()
Mahesh_Bhosale 14-Oct-13 11:04am    
protected void gridview1_RowDataBound(object sender, GridViewRowEventArgs e)
{
int index = Convert.ToInt32(e.CommandName.ToString());
txtname.Text = Convert.ToString(gridview1.Rows[index].Cells[2].Text.ToString());
}

here e is GridView Row Event Argument
ZurdoDev 14-Oct-13 11:10am    
What is the value of e.CommandName.ToString()

i solved this problem:

just change in row data bound
This only occurs because I happened to use the same CommandName that the CLR code uses (Select$)
If I change this to SelectRow$, I no longer get the error.
C#
e.Row.Attributes["onclick"] = ClientScript.GetPostBackClientHyperlink(this.gridview1, "SelectRow$" + e.Row.RowIndex.ToString(), false);

and in row command
change
C#
int index = Convert.ToInt32(e.CommandArgument.ToString());
 
Share this answer
 
v2
You can debug and see whats the value in
e.CommandName.ToString()
which may be something other than a int

Or Try changing

int index = Convert.ToInt32(e.CommandName.ToString());


to

C#
int result;
bool check = int.TryParse(e.CommandName.ToString(),out result)


This will help you to check whether the conversion is failing or not. Then proceed.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900