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