Click here to Skip to main content
12,072,536 members (58,824 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: C#
I am trying to convert a string to an int like this and to increment it:
count = Convert.ToInt32(Hidden_id.Value) + 1;
but results this error: Input string was not in a correct format.
Posted 30-Nov-12 3:24am

1 solution

Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

The string in Hidden_id.Value is not an integer - it contains characters which are not numeric.

Either use int.TryParse instead, or check what you are loading into the field, as it is not a valid number!
  Permalink  
Comments
Annnaa 30-Nov-12 8:41am
   
it gives me error even doing this:
int count = int.Parse(Hidden_id.Value);
even this one:
int count = Convert.ToInt32(Hidden_id.Value);
OriginalGriff 30-Nov-12 8:59am
   
That is why I said use TryParse, not Parse - it reports a problem via the bool return value if the conversion fails, rather than throwing an exception.
But you need to look at what is in the value, and how it got there since the user can't enter a value to a Hidden field.
Annnaa 30-Nov-12 8:41am
   
how to convert a hidden field value to int?
CodeNinja-C# 30-Nov-12 9:04am
   
Annaa,
can you provide your Hidden_id.Value?
--SJ
Annnaa 30-Nov-12 9:02am
   
I want the hidden value to keep nje row index of the datatable.. so to store it during the navigation through records
OriginalGriff 30-Nov-12 9:05am
   
So what do you put in it? How do you do it?
Annnaa 30-Nov-12 9:09am
   
for example to navigate to the next record through a button:
protected void Button2_Click(object sender, EventArgs e)
{


if (Hidden_id.Value.Trim() != null)
count = int.Parse(Hidden_id.Value);

count += 1;




if (count < ds.Tables["Person"].Rows.Count - 1)
{

navigate();
Hidden_id.Value = count.ToString();


}
OriginalGriff 30-Nov-12 9:24am
   
Why put it on the client at all? Why not keep it on the server via the Session variable? It's probably a lot less hassle (and it's certainly a little less traffic between the server and client)
Annnaa 30-Nov-12 9:31am
   
i tried this and doesnt give any error
protected void Button2_Click(object sender, EventArgs e)
{


if (Hidden_id.Value.Trim() != null)
{
bool res = int.TryParse(Hidden_id.Value, out count);

if (res == true)
{
count = int.Parse(Hidden_id.Value);

count += 1;

if (count < MaxRows - 1)
{

navigo();
Hidden_id.Value = count.ToString();


}
}

}


But still the next button doesnt work correctly.. when i click it it doesnt give any result :/
what to do now?
OriginalGriff 30-Nov-12 10:25am
   
As I said: you need to look at what the value is (because it isn't an integer) - but I would put the value in the Session rather than a hidden field.

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web03 | 2.8.160208.1 | Last Updated 30 Nov 2012
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100