daniel
daniel

Reputation: 405

format exception was unhandled input string was not in a correct formats

Exception

I am getting an exception in that line. My coding is correct yet still I am getting an error. Can anyone help me to rewrite the code for that particular line. I am using that line for viewing of a value from the database to textbox. getprogress is method name from webservice.

Upvotes: 2

Views: 4770

Answers (6)

Aswin
Aswin

Reputation: 1

No need to parse any integer value, when using RadTextBox

Hashtable newval = new Hashtable();
int Age = Convert.ToInt32(newval["Age"].ToString());

Upvotes: 0

Romil Kumar Jain
Romil Kumar Jain

Reputation: 20745

The value of menuitemno.Text is like 32.64 (having decimal), so it is not possible to convert decimal to integer using convert.toint.

Use Convert.ToInt64(Convert.ToDouble(menuitemno.Text)) for conversion.

To do this discounting rounding you could do:

Convert.ToInt64(Math.Floor(Convert.ToDouble(menuitemno.Text)));

If you need to round you could replace Math.Floor with Math.Round.

Convert.ToInt64(Math.Round(Convert.ToDouble(menuitemno.Text)));

if value of menuitemno.Text is empty or has invalid char than also the error may come.

If you are now allowing decimal place in the menuitemno.Text then use Int64.TryParse

Upvotes: 1

Dan
Dan

Reputation: 45741

Is menuitemno a textbox? You should rather use a numericupdown if it only accepts numbers. That way you won't have to convert to a number and you can apply restriction, such as no decimal places allowed, quite easily.

Upvotes: 0

Renatas M.
Renatas M.

Reputation: 11820

use int.TryParse()

int result;
if(int.TryParse( menuitemno.Text, out result))
   progress = web.getprogress(result);
else
   //You have incorrect integer in menuitemno.Text

Upvotes: 1

Tigran
Tigran

Reputation: 62248

the value of the text box, most probabbly i null or empty, to fix this issued, assuming that those values are in the range of acceptable ones, you can do:

int i =0; 
int.TryParse(menuitemno.Text, out i);
progress = i;

Something like this.

Upvotes: 0

daryal
daryal

Reputation: 14919

It seems menuitemno is not a valid string which can be converted to integer. Just check the value of menuitemno.Text, I suspect it does not have an integer value. Right click to "menuitemno.Text" and select quick watch; it will show the value.

Upvotes: 0

Related Questions