Click here to Skip to main content
Sign Up to vote bad
good
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 - 2:24


1 solution

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:41
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:59
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:41
how to convert a hidden field value to int?
CodeNinja-C# - 30 Nov '12 - 9:04
Annaa, can you provide your Hidden_id.Value? --SJ
Annnaa - 30 Nov '12 - 9:02
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:05
So what do you put in it? How do you do it?
Annnaa - 30 Nov '12 - 9:09
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:24
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:31
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:25
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
Your Filters
Interested
Ignored
     
0 Mahesh Bailwal 353
1 Sergey Alexandrovich Kryukov 339
2 Maciej Los 290
3 CPallini 245
4 Rohan Leuva 175
0 Sergey Alexandrovich Kryukov 9,162
1 OriginalGriff 7,194
2 CPallini 3,923
3 Rohan Leuva 3,176
4 Maciej Los 2,633


Advertise | Privacy | Mobile
Web01 | 2.6.130516.1 | Last Updated 30 Nov 2012
Copyright © CodeProject, 1999-2013
All Rights Reserved. Terms of Use
Layout: fixed | fluid