473,387 Members | 1,721 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 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 1044
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Mike | last post by:
I am sure that I am making a simple boneheaded mistake and I would appreciate your help in spotting in. I have just installed apache_2.0.53-win32-x86-no_ssl.exe php-5.0.3-Win32.zip...
5
by: David Webb | last post by:
The problem started when the Working Folder for a project was somehow set to the folder of another project. I set the correct working folder in VSS and deleted the .vbproj files that had been...
4
by: Robin Tucker | last post by:
Hi, I'm trying to determine with my program whether or not a given database supports a given feature set. To do this I'm querying for certain stored procedures in the sysobjects table and if...
5
by: NG | last post by:
Hi, We are having DB2-V7.2 DB on AIX 5.2 machine. Recently we upgraded our system to fixpack 13 and activated activate AIX asynchronous IO function. Our DB is going to crash recovery with...
20
by: TTroy | last post by:
Hello, I have found some peculiar behaviour in the fgets runtime library function for my compiler/OS/platform (Dev C++/XP/P4) - making a C console program (which runs in a CMD.exe shell). The...
1
by: Robert Halford | last post by:
On 4th May at 7.45 in the evening my asp.net web sites stopped working on my development server. The page that appears says: Server Application Unavailable The web application you are...
1
by: Dave | last post by:
I am getting te following error in a ASP.Net app that is running on Win XP Pro (SP2): Server cannot access application directory 'C:\Documents and Settings\dave\My Documents\My Visual Studio...
1
by: ME | last post by:
I was running into a problem with the DataGridView while binding it to an object Collection. I got it working and I thought others might like to know how. -------------- Problem -------------...
3
by: Richard Lewis Haggard | last post by:
We are having a lot of trouble with problems relating to failures relating to 'The located assembly's manifest definition with name 'xxx' does not match the assembly reference" but none of us here...
6
by: Lloyd Sheen | last post by:
Ok I copied a VS 2005 web site to another folder. Open my new VS 2008. Attempt to run. I get the following: Error 1 It is an error to use a section registered as...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.