Solved

Input string was not in a correct format.

Posted on 2009-05-13
9
839 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
  • 4
  • 3
  • 2
9 Comments
 
LVL 39

Expert Comment

by:abel
Comment Utility
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
Comment Utility
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
Comment Utility
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
 
LVL 39

Expert Comment

by:abel
Comment Utility
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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 

Author Comment

by:Romacali
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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 500 total points
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

743 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now