Solved

Input string was not in a correct format.

Posted on 2009-05-13
9
859 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
Is Your Team Achieving Their Full Potential?

74% of employees feel they are not achieving their full potential. With Linux Academy, not only will you strengthen your team's core competencies but also their knowledge of of the newest IT topics.

With new material every week, we'll make sure that you stay ahead of the game.

 
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 500 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

Stressed Out?

Watch some penguins on the livecam!

Question has a verified solution.

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

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

696 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