Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 293
  • Last Modified:

Input string was not in a correct format.

Experts,

How can I correctly convert UnitPriceControl to double? At the moment, these 3 conversion try failed with same error.

Please share me your wisdom.

Thanks,

JimiJ

      *******************************************************************************         
                         double unitPrice  
                     
                   unitPrice = Convert.ToDouble(UnitPriceControl.Text);
                     // unitPrice = double.Parse(UnitPriceControl.Text);   - same error
                     // unitPrice = decimal.Parse(UnitPriceControl.Text); - same error
                  
           
0
JaimeJegonia
Asked:
JaimeJegonia
  • 11
  • 9
  • 6
1 Solution
 
YurichCommented:
hello,
what is your UnitPriceControl?

the error you're getting means the data in your UnitPriceControl is not in the format "123.456"

try to debug, put a break point and see what's your actual value of UnitPriceControl

If UnitPriceControl is your custom control or class or anything, override ToString() method returning the right value since by default it can be not what you're expecting it to be...

regards
0
 
cyberdevil67Commented:
Hi JaimeJegonia,

 Try unitPrice = Convert.ToDouble(UnitPriceControl.Text).Trim();

Cheers!
0
 
YurichCommented:
all spaces in the beginning and the end of the string are trimmed automatically during conversion ...

you can try:

string s = "    2.34223   ";

double d = Convert.ToDouble( s );

and it'll be 2.34233

BUT if you got any in between, like "2. 3 4 2 2 3", it'll be a problem...

but again, you can use:

 unitPrice = Convert.ToDouble( UnitPriceControl.Text.Replace( " ", "" ));

regards
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
JaimeJegoniaAuthor Commented:
Yuric,

The UnitPriceControl is a local variable control. I tried putting breakpoint so I may know what's the parsed input data but I have also a problem of "No symbols have been loaded for this documents - so my breakpoint can't be hit"

When I try  unitPrice = Convert.ToDouble(UnitPriceControl.Text).ToString() or unitprice = UnitPriceControl.Text.ToString()

This is the error:

H:\RTP\PO\AddPORecord.safe.aspx.cs(558): Cannot implicitly convert type 'string' to 'double'

Thanks for the try,

JimiJ
0
 
cyberdevil67Commented:
Remove the ToString() as it is trying to convert the type back to a string after the Convert.ToDouble
0
 
JaimeJegoniaAuthor Commented:
Yuric,

I always input whole number w/o space but the error is still the same.

I tried this also:
 unitPrice = Convert.ToDouble(UnitPriceControl.Text.Replace(" ",""));  - SAME ERROR

Thanks,


JimiJ
0
 
YurichCommented:
cyber is right,

unitPrice = Convert.ToDouble( UnitPriceControl.Text ); // or
unitPrice = Convert.ToDouble( UnitPriceControl.Text.Replace( " ", "" ));

should solve any situations as long as you have numeral values in your text box

techniqially you can use

unitPrice = Convert.ToDouble( UnitPriceControl.Text.ToString())

but ToString() is not necessary

regards
0
 
YurichCommented:
Ok, try the following

try
{
   unitPrice = Convert.ToDouble( UnitPriceControl.Text );
}
catch
{
   MessageBox.Show( UnitPriceControl.Text );
}

and you'll see what's an actual value in your UnitPriceControl - place this value in here and we'll see what's wrong ;)

regards
0
 
JaimeJegoniaAuthor Commented:

CyberAngel -  Sorry I changed your name because I don't really intend to work with the devil.

 Try unitPrice = Convert.ToDouble(UnitPriceControl.Text).Trim(); - Debug Error

I tried unitPrice = Convert.ToDouble(UnitPriceControl.Text.Trim());  - Same with the  original Error.

Your last suggestion had been done before with all the same error.


Jimi J
0
 
cyberdevil67Commented:
*lol* About Angel not being the devil

What is the actual error, you never really said. Unless its the conver to string to double you posted earlier:-)
0
 
JaimeJegoniaAuthor Commented:
Cyber,

The error is the title of my posting "Input string was not in a correct format."



0
 
JaimeJegoniaAuthor Commented:
Yurich,

Do i need to put Assembly reference for the Messagebox?




0
 
YurichCommented:
try my try-catch block and let us know what value you have in your message box

i actually thought that's "Input string was not in a correct format." and error message we're working with

you must have something not-convertable, that's obvious... We can try using Regex (regular expressions) for filtering everything out but that's rather a work-around and let's first see how you're getting on with this try-catch block

regards
0
 
cyberdevil67Commented:
Not if you you already have System.Windows.Forms already imported:-)

Yeah try Yurich try catch because the code we both gave you should be working.
0
 
JaimeJegoniaAuthor Commented:
Yurich,

I know what you mean, but i got this debug error " (The type or namespace name 'MessageBox' could not be found (are you missing a using directive or an assembly reference?)"  I don't know what Assy ref i need to add.



0
 
cyberdevil67Commented:
imports System.Windows.Forms
0
 
JaimeJegoniaAuthor Commented:
I'm developing Web App, what's the equivalent?
0
 
YurichCommented:
add

using System.Windows.Forms

to your using block at the very top of your class (document)

i'm not sure about import above...
0
 
YurichCommented:
aaahh,
it won't work in webs... there's no message box in webs
0
 
YurichCommented:
ok, do the following:

try
{
   unitPrice = Convert.ToDouble( UnitPriceControl.Text );
}
catch
{
   string str = UnitPriceControl.Text;   // ***
   int x = 0;
}

click your left-mouse on the left margin where my stars are - or place your breakpoint at this location

int x = 0; - means nothing but if you don't have it, you won't see what's in your str when you debug since the focus will leave the scope as soon as your str is assigned...

Run debug (F5) and when it hits this breakpoint, press F10 once, and then look in the locals or in the watchlist for str and let us know what your misterious UnitPriceControl is

regards
0
 
JaimeJegoniaAuthor Commented:
OK, thanks for both of you.

I used Response.Write( UnitPriceControl.Text );

and it gave me $260.00 -  currency formatted by default.
In this case if I remove formatting manually it will work?




0
 
JaimeJegoniaAuthor Commented:
The $ sign is the one giving trouble- that's the puzzle that we have just solve. But it has to be formated in any currency and it automatically filled upon selecting product ot item from dropdownlist.

My original function worked by just re-entering the unitprice without dollar sign. What I need from you guys is to help me remove before parsing the entry. So we can close this topic / posting. the "catch" thing was really the one that give us the lead.

 

 
0
 
YurichCommented:
there could be several ways, but try this:

unitPrice = Convert.ToDouble( UnitPriceControl.Text.Replace( "$", "" ));

that's the easiest, but if your application is using more that just $ sign, you'll hit the same problem when you use yens or pounds, in this long run, we can use either replace of the string class or more elegant Regex as I suggested once above:

add to your using block:
using System.Text.RegularExpressions


...

// then somewhere where you want to check it:

Regex myRegex = new Regex( "[^0-9.,]" );
string s = myRegex.Replace( UnitPriceControl.Text, "" );
unitPrice = Convert.ToDouble( UnitPriceControl.Text );

should work just fine - it will replace everything what is not a number, a dot, or a comma (European delimiter)

regards

0
 
JaimeJegoniaAuthor Commented:
Everything works fine now.

Yurich thanks for your aggressive reponses.
Cyber thanks also for your helpful assistance.

I hope that I would be able also to help in the future.

Have a great day!

Jimi J.
 
0
 
cyberdevil67Commented:
What no points for Assistance?

Well at least you got it sorted thats the main thing.
0
 
JaimeJegoniaAuthor Commented:

Final Code:

double unitPrice

Regex myRegex = new Regex( "[^0-9.,]" );
string s = myRegex.Replace( UnitPriceControl.Text, "" );
unitPrice = Convert.ToDouble( s );

It works perfect regardless of currency format.

Thanks to both of you once again, you really made my day!
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 11
  • 9
  • 6
Tackle projects and never again get stuck behind a technical roadblock.
Join Now