[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Input string was not in a correct format.

Posted on 2009-05-13
9
Medium Priority
?
862 Views
Last Modified: 2013-12-17
Hello there,

I'm getting this error: pointing to: Line 430:            pCDI_C = Convert.ToDouble(dr["CDI_C"].ToString());

 Input string was not in a correct format.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.FormatException: Input string was not in a correct format.

Source Error:

Line 428:
Line 429:            //CDI from previous yr
Line 430:            pCDI_C = Convert.ToDouble(dr["CDI_C"].ToString());
Line 431:            if (Convert.ToString(dr["CDI_C"].ToString()) == "")
Line 432:            {
0
Comment
Question by:Romacali
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 2
9 Comments
 
LVL 39

Expert Comment

by:abel
ID: 24377090
That is a very common error. It looks very much like your database value does not contain the expected data. The Convert.ToDouble function expects input that is equal to that of the current Culture settings. For instance, in Europe, "1,12" would be processed, but in UK and US, it requires "1.12" (dot versus comma).

Other reasons that this code fails exist. The data can be a string that is not convertible (i.e., an empty string) or the data can be null (but that will raise a different error).

Can you place a breakpoint at the offending line and copy to us what's in the CDI_C field? (just move over it with your mouse to see it while on the breakpoint).

-- Abel --
0
 
LVL 3

Expert Comment

by:mpf1748
ID: 24377135
You could also use Double.TryParse(dr["CDI_C"].ToString(), out result). If this returns true, it sucessfully parsed a double and is stored in result; otherwise, it was not a valid string representation of a double.
0
 

Author Comment

by:Romacali
ID: 24377234
On the DB this CDI_C is Null
I placed the mouse on the line and it says:
"double Convert.ToDouble(dateTime value) (+17 overload(s)) Calling this method always throws System.InvalidcastException"

Exceptions:
System.InvalidCastException
pCDI_C = Convert.ToDouble(dr["CDI_C"].ToString());
                if (Convert.ToString(dr["CDI_C"].ToString()) == "")
                {
                    txtCompPrevData.Text = "*No data for previous month (CDIC)\n";
                }
                else if (CDI_C == 0)
                {
                    //do nothing
                }
            
                else
                {
                    if (CDI_C > (pCDI_C * 1.1) || CDI_C < (pCDI_C * 0.9))
                    {
                        txtCompPrevData.Text += "*CDIC: Warning >10% diff from prev. Qtr.  (" + Convert.ToString(VAP_C) + " " + Convert.ToString(pVAP_C) + ")\n";
                    }
 
                    else
                    {
                        txtCompPrevData.Text += "*CDI: Data seems fine.\n";
                    }
 
                }

Open in new window

0
Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

 
LVL 39

Expert Comment

by:abel
ID: 24377282
That's slightly different then we had previously.

So, the data is NULL in the database? Use the TryParse method. It will fill your variable with 0.0 when the data is NULL. And, like mpf1748 is telling, you can use the outcome of the method to find out whether the TryParse was successful:


double pCDI_C = 0;
if(Double.TryParse(dr["CDI_C"].ToString(), out pCDI_C))
{
    // the value is valid
}
{
    // the value is NULL in the DB or not a number
}

Open in new window

0
 

Author Comment

by:Romacali
ID: 24377421
now I'm having a problem that it is saving the data but it doesn't display it on the screen.. I see it on the database.. but it should display the CDI_N and CD_D for the user...

the checkbox5 is checked and it shouldn't be.. it should display the data... any idea?
   //CDI BEGIN
            string xCDI_N = Convert.ToString(dr["CDI_N"].ToString());
            if (xCDI_N.Length != 0)
            {
                CDI_N = Convert.ToDouble(xCDI_N);
                TextBox9.Text = xCDI_N;
            }
 
            string xCDI_D = Convert.ToString(dr["CDI_D"].ToString());
            if (xCDI_D.Length != 0)
            {
                CDI_D = Convert.ToDouble(xCDI_D);
                TextBox10.Text = xCDI_D;
            }
         
 
 
            sCheckbox = dr["CDI_NA"].ToString();
            if (dr["CDI_NA"] != System.DBNull.Value) Convert.ToBoolean(dr["CDI_NA"]);
           // if (Convert.ToBoolean(dr["CDI_NA"]))
            {
                CheckBox5.Checked = true; 
                TextBox9.Text = "";
                TextBox10.Text = "";
            }
 
            //CDI END
 
  ///read CDI_C from DB as an int
            //if (dr2["CDI_C"] != null)
            if (dr2["CDI_C"] != System.DBNull.Value)
                CDI_C = Convert.ToDouble(dr2["CDI_C"]);
            else CDI_C = 0;

Open in new window

0
 
LVL 3

Expert Comment

by:mpf1748
ID: 24377516
Your
   if (dr["CDI_NA"] != System.DBNull.Value) Convert.ToBoolean(dr["CDI_NA"]);
statement probably will not compile, but if the actual if statement contains a semicolon, then anything in the following brackets will be executed, which is probably why you checkbox is being checked. You can remove the semicolon and fix the if statement to correct the problem.
0
 

Author Comment

by:Romacali
ID: 24377586
I tried but I'm getting an error:
Line 256:
Line 257:            sCheckbox = dr["CDI_NA"].ToString();
Line 258:            if (dr["CDI_NA"] != System.DBNull.Value) Convert.ToBoolean(dr["CDI_NA"])
Line 259:           // if (Convert.ToBoolean(dr["CDI_NA"]))
Line 260:            {

I also tried this:
  if ((dr["CDI_NA"] != System.DBNull.Value) Convert.ToBoolean(dr["CDI_NA"]))
but didn't work
0
 

Author Comment

by:Romacali
ID: 24377610
also it gave this error:

Compiler Error Message: CS1002: ; expected

Source Error:

Line 256:
Line 257:            sCheckbox = dr["CDI_NA"].ToString();
Line 258:            if (dr["CDI_NA"] != System.DBNull.Value) Convert.ToBoolean(dr["CDI_NA"])
Line 259:           // if (Convert.ToBoolean(dr["CDI_NA"]))
Line 260:            {


Source File: c:\WWWroot\SoCalPatientSafety\DataPoint\TrackTestCDI.aspx.cs    Line: 258


Compiler Warning Messages:
Warning: CS0219: The variable 'sCheckbox' is assigned but its value is never used
Source Error:

c:\WWWroot\SoCalPatientSafety\DataPoint\TrackTestCDI.aspx.cs

Line 330:        if (dr.Read())
Line 331:        {
Line 332:            String sCheckbox = "";
Line 333:
L
0
 
LVL 3

Accepted Solution

by:
mpf1748 earned 2000 total points
ID: 24377643
Yes. That was what I was talking about. Your
  if (dr["CDI_NA"] != System.DBNull.Value) Convert.ToBoolean(dr["CDI_NA"])
statement will not compile. Do you mean this?
  if (dr["CDI_NA"] != System.DBNull.Value && Convert.ToBoolean(dr["CDI_NA"])
??

You might also run into an exception while parsing if CDI_NA does not contain either true or false; or probably 1 (possible any non-zero number) or 0.
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…

656 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question