Why does this cause an error? (2005)

R

RvGrah

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
 
R

RvGrah

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
 
G

Guest

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
 
R

RvGrah

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

Bob
 
R

RvGrah

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
 
C

Chris Dunaway

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top