471,317 Members | 1,833 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,317 software developers and data experts.

Why does this cause an error? (2005)

I have an old database from which I'm pulling phone numbers for
display. The datatype in Sql Server is varchar, therefore comes into my
program as string. It (almost always) is pure digits, but I can't
change the datatype right now because some users still have legacy apps
that write fully formatted phone numbers to the database (ie: (555)
555-1234).

I'm using a procedure to format the number as follows:

private void PhoneNumberFormatting( object sender, EventArgs e )
{
TextBox tb = (TextBox)sender;
if (tb.Text!=string.Empty) {
double i = Convert.ToDouble(tb.Text);
try {
tb.Text = i.ToString("(###) 000-0000");
}
catch (Exception ex){
Console.WriteLine(ex.Message);
}
}
}

This flies but produces a "first chance exception" that "Input string
was not in the correct format".

Even copying and pasting directly from the MSDN help code on formatting
produces the same exception:

private void PhoneNumberFormatting( object sender, EventArgs e )
{
TextBox tb = (TextBox)sender;
if (tb.Text!=string.Empty) {
double i = Convert.ToDouble(tb.Text);
try {
Double myDouble = 4085106501; //this part
straight out of MSDN
String myString = myDouble.ToString("(###) ###
- ####");
tb.Text = myString;
}
catch (Exception ex){
Console.WriteLine(ex.Message);
}
}
}

It appears this code flies alright, the app runs fine, it's just that I
want to be a real pro and write code that's type correct. Any ideas?

Bob Graham

Feb 7 '06 #1
5 979
Well, a little red-faced, but still perplexed. On further examination,
it's the assigning of the string to the textboxe's text property that
is causing the error, not the conversion of the double with the
ToString method. Why would the clr object to passing a string to the
text value????

Bob

Feb 7 '06 #2
Bob,
I suspect that it is this line:
double i = Convert.ToDouble(tb.Text);
that is throwing an exception.
You could use Double.TryParse which doesn't throw an exception, or even
better,
pre-process the string with Replace("-", "") etc. to ensure the likelihood
that is is all numeric before attempting to convert.
Peter

--
Co-founder, Eggheadcafe.com developer portal:
http://www.eggheadcafe.com
UnBlog:
http://petesbloggerama.blogspot.com


"RvGrah" wrote:
I have an old database from which I'm pulling phone numbers for
display. The datatype in Sql Server is varchar, therefore comes into my
program as string. It (almost always) is pure digits, but I can't
change the datatype right now because some users still have legacy apps
that write fully formatted phone numbers to the database (ie: (555)
555-1234).

I'm using a procedure to format the number as follows:

private void PhoneNumberFormatting( object sender, EventArgs e )
{
TextBox tb = (TextBox)sender;
if (tb.Text!=string.Empty) {
double i = Convert.ToDouble(tb.Text);
try {
tb.Text = i.ToString("(###) 000-0000");
}
catch (Exception ex){
Console.WriteLine(ex.Message);
}
}
}

This flies but produces a "first chance exception" that "Input string
was not in the correct format".

Even copying and pasting directly from the MSDN help code on formatting
produces the same exception:

private void PhoneNumberFormatting( object sender, EventArgs e )
{
TextBox tb = (TextBox)sender;
if (tb.Text!=string.Empty) {
double i = Convert.ToDouble(tb.Text);
try {
Double myDouble = 4085106501; //this part
straight out of MSDN
String myString = myDouble.ToString("(###) ###
- ####");
tb.Text = myString;
}
catch (Exception ex){
Console.WriteLine(ex.Message);
}
}
}

It appears this code flies alright, the app runs fine, it's just that I
want to be a real pro and write code that's type correct. Any ideas?

Bob Graham

Feb 7 '06 #3
Peter,
I have tried replacing the myString value with any old text ("this is a
test") and it still causes an error.

Bob

Feb 7 '06 #4
Peter,
I have more (stuff I should have included in the first place). The text
box has it's text property bound to the database field. Since I can't
put a Custom Format string at Design time for a non numeric field, I'm
handling the formatting in the TextChanged event. I suppose I could
just live with this, or modify the sql to return a numeric instead of
VarChar. This leaves me with missing data if anyone has put a formatted
value in the database from one of the legacy apps.

Bob

Feb 7 '06 #5
Why do you want to convert a phone number to numeric? That makes no
sense to me. Are you doing it just to format it? You would probably
be better off iterating through the characters, removing any non digits
and then return a string with the correct format. When storing in the
db, I would suggest storing digits only and not any other characters.

Feb 8 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by Lloyd Sheen | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.