So my issue is I am creating textboxes in the code behind, & I am trying to insert the values from the textbox to a SQL database. If the textboxes are blank (which is fine) the insert works and if the textboxes are populated with figures then the insert
works as well. The textboxes are pre-populated based on their figures in the SQL table, so some are populated whereas some aren't. I only get the error when a text box is pre-populated and I erase/delete the value by making it blank and then try to perform
the 'Insert'. The error message is 'Input String Is Not In A Correct Format', here is my code:
???? in your code snippet, which line is actually crashing?
g.
B-) Please help me by completing my school survey about computer programmers on my website. Thank you!!! Gerry Lowry +1 705-429-7550 wasaga beach, ontario, canada
B-) Please help me by completing my school survey about computer programmers on my website. Thank you!!! Gerry Lowry +1 705-429-7550 wasaga beach, ontario, canada
With any.TryParse method always check the
bool that gets returned; only use the out value if the
bool is true.
g.
B-) Please help me by completing my school survey about computer programmers on my website. Thank you!!! Gerry Lowry +1 705-429-7550 wasaga beach, ontario, canada
The prolem is that you are attempting to convert an empty string to a double. Convert.ToDouble expects that the string should contain a string representation of a number. Since it does not (it contains a blank during the crash) you get an error saying the
input string is not in the correct format (ie, not something i can extract a number from).
To fix it, add a try catch and refactor your code to look like this:
What I've done is removed the conversion from inside SetPrice and instead wrapped the conversion around a try catch, if the catch gets an error it sets the default value to zero.
FWIW, .TryParse simply does the try/catch for the programmer, if an exception occurs, .TryParse suppresses the exception and returns
false; if an exception does not occur, .TryParse returns
true and assigns the value of the conversion to the out parameter.
g.
* instead of Convert.ToDouble, consider Double.TryParse;
With any.TryParse method always check the
bool that gets returned; only use the out value if the
bool is true.
B-) Please help me by completing my school survey about computer programmers on my website. Thank you!!! Gerry Lowry +1 705-429-7550 wasaga beach, ontario, canada
Yes, you are correct. I forgot about TryParse. In either case, the error is generated by the empty string. Using TryParse or a Try/Catch block should fix the issue.
rankone
Member
53 Points
53 Posts
Input String Is Not In A Correct Format - Help!
Apr 12, 2012 09:37 PM|LINK
Hey Everyone!
So my issue is I am creating textboxes in the code behind, & I am trying to insert the values from the textbox to a SQL database. If the textboxes are blank (which is fine) the insert works and if the textboxes are populated with figures then the insert works as well. The textboxes are pre-populated based on their figures in the SQL table, so some are populated whereas some aren't. I only get the error when a text box is pre-populated and I erase/delete the value by making it blank and then try to perform the 'Insert'. The error message is 'Input String Is Not In A Correct Format', here is my code:
void tb2_TextChanged(object sender, EventArgs e) { TextBox tb = (TextBox)sender; int item = Convert.ToInt32(tb.ID.Substring(tb.ID.LastIndexOf("_") + 1).Replace("N", "-")); int store = (int)Session["PItem"]; if (tb.Text.Length > 0) { Data.SetPrice(store, item, Convert.ToDouble(tb.Text.Replace(".", ","))); } else { string str = ((TableCell)((TableRow)tb.Parent.Parent).Cells[((TableRow)tb.Parent.Parent).Cells.GetCellIndex((TableCell)tb.Parent) - 1]).Text.Replace("Kd", "").Trim(); Data.SetPrice(store, item, Convert.ToDouble(str)); } }Any help would be appreciated.
Thanks
gerrylowry
All-Star
20525 Points
5713 Posts
Re: Input String Is Not In A Correct Format - Help!
Apr 12, 2012 10:02 PM|LINK
@ rankone
???? in your code snippet, which line is actually crashing?
g.
gerrylowry
All-Star
20525 Points
5713 Posts
Re: Input String Is Not In A Correct Format - Help!
Apr 12, 2012 10:03 PM|LINK
@ rankone
in your SQL schema, do you allow nulls?
g.
gerrylowry
All-Star
20525 Points
5713 Posts
Re: Input String Is Not In A Correct Format - Help!
Apr 12, 2012 10:29 PM|LINK
@ rankone
instead of Convert.ToDouble, consider Double.TryParse;
http://msdn.microsoft.com/en-us/library/system.double.tryparse.aspx
http://msdn.microsoft.com/en-us/library/994c0zb1.aspx "Double.TryParse Method (String, Double%)"
With any .TryParse method always check the bool that gets returned; only use the out value if the bool is true.
g.
Paul Linton
Star
13431 Points
2535 Posts
Re: Input String Is Not In A Correct Format - Help!
Apr 12, 2012 11:02 PM|LINK
Put a breakpoint on the failing line and find out show us which line is failing and what the input is when it fails.
rankone
Member
53 Points
53 Posts
Re: Input String Is Not In A Correct Format - Help!
Apr 12, 2012 11:04 PM|LINK
Hi Gerry,
This is the line that it crashes on:
Do you mean if the SQL table allows NULL's ? Yes it does.
Paul Linton
Star
13431 Points
2535 Posts
Re: Input String Is Not In A Correct Format - Help!
Apr 12, 2012 11:16 PM|LINK
So what is the value of 'str' when you get the exception? (Note - not what do you think it is, but what does the debugger actually tell you it is)
CodeHobo
All-Star
18647 Points
2647 Posts
Re: Input String Is Not In A Correct Format - Help!
Apr 12, 2012 11:16 PM|LINK
The prolem is that you are attempting to convert an empty string to a double. Convert.ToDouble expects that the string should contain a string representation of a number. Since it does not (it contains a blank during the crash) you get an error saying the input string is not in the correct format (ie, not something i can extract a number from).
To fix it, add a try catch and refactor your code to look like this:
if (tb.Text.Length > 0){ double numericValue; try{ numericValue=Convert.ToDouble(tb.Text.Replace(".", ",")); } catch(Exception e){ numericValue=0; } Data.SetPrice(store, item, numericValue); }What I've done is removed the conversion from inside SetPrice and instead wrapped the conversion around a try catch, if the catch gets an error it sets the default value to zero.
Blog | Twitter : @Hattan
gerrylowry
All-Star
20525 Points
5713 Posts
Re: Input String Is Not In A Correct Format - Help!
Apr 12, 2012 11:44 PM|LINK
or, alternatively, use .TryParse*
FWIW, .TryParse simply does the try/catch for the programmer, if an exception occurs, .TryParse suppresses the exception and returns false; if an exception does not occur, .TryParse returns true and assigns the value of the conversion to the out parameter.
g.
* instead of Convert.ToDouble, consider Double.TryParse;
http://msdn.microsoft.com/en-us/library/system.double.tryparse.aspx
http://msdn.microsoft.com/en-us/library/994c0zb1.aspx "Double.TryParse Method (String, Double%)"
With any .TryParse method always check the bool that gets returned; only use the out value if the bool is true.
CodeHobo
All-Star
18647 Points
2647 Posts
Re: Input String Is Not In A Correct Format - Help!
Apr 13, 2012 12:02 AM|LINK
Yes, you are correct. I forgot about TryParse. In either case, the error is generated by the empty string. Using TryParse or a Try/Catch block should fix the issue.
Blog | Twitter : @Hattan