• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 688
  • Last Modified:

Input string was not in a correct format

when iam trying to convert a session to double , igot that error
note that : GRB_CLOSE is an output paramter at oracle procedure ,sometimes it return 0


CMD2.Parameters.Add("GRB_CLOSE", OracleDbType.Int64);
CMD2.Parameters["GRB_CLOSE"].Direction = ParameterDirection.Output
//make a session for this result
Session["GRB_CLOSE"]=CMD2.Parameters["GRB_CLOSE"].Value.ToString();
//at the second page 
double grbclose=Convert.ToDouble(Session["GRB_CLOSE"]);

Open in new window

0
alaa_nasr
Asked:
alaa_nasr
  • 8
  • 3
  • 2
2 Solutions
 
abelCommented:
Can you show what's inside GRB_CLOSE? This error will occur if, for instance, the value from the database is NULL or a string that cannot be converted to a double. To find out you can use TryParse instead of Convert.ToDouble.

double grbclose = 0.0;
Double.TryParse(Session["GRB_CLOSE"].ToString(), grbclose);
0
 
sunithnairCommented:
Try and see what is the value for Session["GRB_CLOSE"] in the second page. The value might be null.
0
 
abelCommented:
This will put 0.0 in grbclose if the value in the session cannot be seen as a double.

Now, there's something else that may be a problem here. You put the string value in the session. You use ToString(). Depending on where you run this code (server, local, from web page), the locale settings might be of influence and change, par example, "1234567.89" into "12.234.567,89", which is then not backwards convertible anymore. To change this behavior, either do not use ToString (the session can store any object type, but don't forget to cast) or set the locale correctly programmatically.
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
alaa_nasrAuthor Commented:
i use double grbclose = 0.0;
Double.TryParse(Session["GRB_CLOSE"].ToString(), grbclose);
then i got that error:
No overload for method 'TryParse' takes '2' arguments

 
0
 
alaa_nasrAuthor Commented:
the session contain null value and i want to convert it to 0
0
 
abelCommented:
My apologies, you need to add the "out" modifier:

grbclose = 0.0;
Double.TryParse(Session["GRB_CLOSE"].ToString(), out grbclose);
0
 
abelCommented:
I still think that this won't work. We should change the code a bit, like as follows:

grbclose = 0.0;
Double.TryParse(Session["GRB_CLOSE"], out grbclose);

Because the TryParse will catch the NullPointerException which results of the ToString() function. If we do it the other way (my previous post) the NPE will be thrown before it goes into the TryParse function.
0
 
sunithnairCommented:
You could try this one
double grbclose = Session["GRB_CLOSE"] == null ? 0 : Convert.ToDouble(Session["GRB_CLOSE"]);

Open in new window

0
 
abelCommented:
@sunithnair: that works, of course, but not for the situation where Session("GRB_CLOSE") would contain a string, say "", that cannot be parsed to a double. The TryParse does both in one go.
0
 
sunithnairCommented:
@abel: I dont think you can do it this way (Double.TryParse(Session["GRB_CLOSE"], out grbclose);) because the Double.TryParse will only accept a string as the 1st parameter)
0
 
abelCommented:
I feared you were right, but looking up some old code, it seems to work though. To be sure I tried this in an empty asp.net project and it sets d = 0.0. I guess the automatic conversion is at work...

        Dim d As Double = 1.0
        Double.TryParse(Session("testnonexisting"), d)  ' d holds 0.0

Then, I tried the same in C#, fearing the stricter syntax and type checking might not work the same as with VB. And you were absolutely right. Of course, to change it, a simple cast or an as-clause is enough:

            double d = 1.0;
            Double.TryParse(Session["testnonexisting"] as string, out d);   // d holds 0.0

            double d = 1.0;
            Double.TryParse((string) Session["testnonexisting"], out d);  //d holds 0.0


0
 
abelCommented:
@TheLearnedOne: thanks for wanting to clean this one up, but I believe that is in error. The question is answered. The last remark from OP was "the session contain null value and i want to convert it to 0". My last remark, using Double.TryParse explains exactly how to do that.

Suggest: points split between http:#23720624 (suntihnair) and http:#23731720 (abel).
0
 
abelCommented:
Clickable version (retry): points split between http:#23720624 (sunithnair) and http:#23731720 (abel).
0

Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

  • 8
  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now