Click here to Skip to main content
15,907,329 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Hi,
I am getting Input string was not in a correct format issues.

I'm using gridview
Im getting error over here
C#
DataBoundLiteralControl anchorText = (DataBoundLiteralControl) dataGridItem.Cells[1].Controls[0];
						int incLength =anchorText.Text.IndexOf("><")- (anchorText.Text.IndexOf("IncidentId=")+"IncidentId=".Length+2);
						int IncNum=Int32.Parse(anchorText.Text.Substring(anchorText.Text.IndexOf("IncidentId=")+ "IncidentId=".Length, incLength));


C#
int IncNum=Int32.Parse(anchorText.Text.Substring(anchorText.Text.IndexOf("IncidentId=")+ "IncidentId=".Length, incLength));


Exception Details: System.FormatException: Input string was not in a correct format.

Any help please
Posted
Updated 17-Dec-15 1:04am
v3
Comments
Herman<T>.Instance 17-Dec-15 6:32am    
what is the result of the part: anchorText.Text.Substring(anchorText.Text.IndexOf("IncidentId=")+ "IncidentId=".Length, incLength). Set that in a string and check with your debugger.
Suvendu Shekhar Giri 17-Dec-15 6:34am    
What have you provided as input to anchorText?
It's very easy to identify if you just put a breakpoint and try debugging.
Tips: Add Watch on
-- anchorText.Text.IndexOf("IncidentId=")
Salt Pepper 17-Dec-15 7:03am    
DataBoundLiteralControl anchorText = (DataBoundLiteralControl) dataGridItem.Cells[1].Controls[0];
int incLength =anchorText.Text.IndexOf("><")- (anchorText.Text.IndexOf("IncidentId=")+"IncidentId=".Length+2);
int IncNum=Int32.Parse(anchorText.Text.Substring(anchorText.Text.IndexOf("IncidentId=")+ "IncidentId=".Length, incLength));

I m unable to debug becoz i m working in server. (i.e) i taken code in iis n checking in url
xszaboj 17-Dec-15 7:37am    
I'am not sure but, are you trying to parse query string if so.. try this:
http://stackoverflow.com/questions/11956948/easiest-way-to-parse-querystring-formatted-data
ZurdoDev 17-Dec-15 8:02am    
This is actually a very easy thing for you to debug and you'll have to since we can't see the code executing. The error means that you're trying to convert something into an integer that is not an integer. For example, if the string were "" you can't convert that into an integer.

So, all you have to do is put a breakpoint there and examine each piece and see what is wrong. You should have it fixed in less than 5 minutes.

1 solution

How else? Int32.Parse cannot be successful for any arbitrary string. Isn't it obvious? You can always validate it and recover from this exception is you handle it by putting the call in some try-catch block with appropriate exception handling, or, better, just using Int32.TryParse instead:
https://msdn.microsoft.com/en-us/library/system.int32.tryparse%28v=vs.110%29.aspx[^].

Interestingly, if you read .NET BCL source code, you will see that TryParse methods don't use exception handling internally, instead, they prevent throwing exceptions.

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