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

Input string was not in a correct format.

Hello,

Here is the problem I have a dropdown for year which has 2007, 2008 and 2009
and for me to enter the data I have to choose the year and a quarter .. I entered the data in order it will work, otherwise it will get this error. But It supposed to work.. for example
If I entered just 2009 it should read the data from last year as 0 instead of NULL or getting an error.

I'm getting this error:
Line 432:
Line 433:            //CDI from previous yr
Line 434:            pCDI_C = Convert.ToDouble(dr["CDI_C"].ToString());
Line 435:            if (Convert.ToString(dr["CDI_C"].ToString()) == "")
Line 436:            {
//CDI from previous yr
            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(CDI_C) + " " + Convert.ToString(pCDI_C) + ")\n";
                }
 
                else
                {
                    txtCompPrevData.Text += "*CDI: Data seems fine.\n";
                }
 
            }

Open in new window

0
Romacali
Asked:
Romacali
4 Solutions
 
williamcampbellCommented:
Have you looked at TryParse?



string value;
double number;
 
value = Double.MinValue.ToString();
if (Double.TryParse(value, out number))
   Console.WriteLine(number);
else
   Console.WriteLine("{0} is outside the range of a Double.", 
                     value);
 
value = Double.MaxValue.ToString();
if (Double.TryParse(value, out number))
   Console.WriteLine(number);
else
   Console.WriteLine("{0} is outside the range of a Double.",
                     value);

Open in new window

0
 
RomacaliAuthor Commented:
where do i add this? added the code you sent and I got another error:
 Object cannot be cast from DBNull to other types.
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.InvalidCastException: Object cannot be cast from DBNull to other types.

Source Error:

Line 262:
Line 263:            sCheckbox = dr["CDI_NA"].ToString();
Line 264:            if (Convert.ToBoolean(dr["CDI_NA"]))
Line 265:            {
Line 266:                CheckBox5.Checked = true;
0
 
williamcampbellCommented:

double result = 0;
if ( dr["CDI_C"] && Double.TryParse(dr["CDI_C"], out result) )
{
     pCDI_C = result.ToString ();
}
else
{
    // Problems
}

Open in new window

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.

 
RomacaliAuthor Commented:
on the database values for CDI_C are:
NULL
NULL
0.33333333333333337
8.0000000000000002E-2
7.3333333333333334E-2
0.01
NULL
NULL
NULL
NULL
NULL
NULL
NULL

0
 
williamcampbellCommented:
Guess I'm not understanding the problem ... What is the error you are getting .. what is the piece of code with the problem?
0
 
RomacaliAuthor Commented:
I tried and I got this:
Compiler Error Message: CS1502: The best overloaded method match for 'double.TryParse(string, out double)' has some invalid arguments

Source Error:

Line 435:            //CDI from previous yr
Line 436:            double result = 0;
Line 437:            if (dr["CDI_C"] && Double.TryParse(dr["CDI_C"], out result))
Line 438:            {
Line 439:                pCDI_C = result.ToString();
 //CDI from previous yr
            double result = 0;
            if (dr["CDI_C"] && Double.TryParse(dr["CDI_C"], out result))
            {
                pCDI_C = result.ToString();
            }
            else
            {
                // Problems
            }
           
            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(CDI_C) + " " + Convert.ToString(pCDI_C) + ")\n";
                }
 
                else
                {
                    txtCompPrevData.Text += "*CDI: Data seems fine.\n";
                }
 
            }

Open in new window

0
 
RomacaliAuthor Commented:
when I tried to add this data for 2008 or 2009 I get this error.. 2007 is fine

the error is:
Line 436:                
Line 437:          
Line 438:            pCDI_C = Convert.ToDouble(dr["CDI_C"].ToString());
Line 439:            
Line 440:            if (Convert.ToString(dr["CDI_C"].ToString()) == "")
Line 436:                
Line 437:          
Line 438:            pCDI_C = Convert.ToDouble(dr["CDI_C"].ToString());
Line 439:            
Line 440:            if (Convert.ToString(dr["CDI_C"].ToString()) == "")






   SqlCommand cmd2 = new SqlCommand("sp_GetPreviousQuarterData", myConnection);
        cmd2.CommandType = CommandType.StoredProcedure;
        cmd2.Parameters.Add(new SqlParameter("@Login", Session["Login"].ToString()));
        cmd2.Parameters.Add(new SqlParameter("@DataYr", ddlDataYr.Text.Trim()));
        cmd2.Parameters.Add(new SqlParameter("@DataQtr", ddlDataQtr.Text.Trim()));
        SqlDataReader dr = null;
        myConnection.Open();
        dr = cmd2.ExecuteReader();
        if (dr.Read())
        {
           String sCheckbox = "";
  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(CDI_C) + " " + Convert.ToString(pCDI_C) + ")\n";
                }
 
                else
                {
                    txtCompPrevData.Text += "*CDI: Data seems fine.\n";
                }
 
            }
 
            //txtCompPrevData.Text += "\n\nDebug: " + "@" + VAP_C + "@" + CLBSI_C + "@" + MRSA_C + "@" + Sepsis_C + "\n" + pVAP_C + "@" + pCLBSI_C + "@" + pMRSA_C + "@" + pSepsis_C;
 
        }
        if (dr != null)
            dr.Close();
        myConnection.Close();
    }

Open in new window

0
 
RomacaliAuthor Commented:
there error points to:  pCDI_C = Convert.ToDouble(dr["CDI_C"].ToString());
0
 
williamcampbellCommented:
Ignore the tryparse thing the data is not strings

dr["CDI_C"].ToString()  is failing because
dr["CDI_C"] is null

you need to do the check below
if ( dr["CDI_C"] != NULL )
{
      pCDI_C = Convert.ToDouble(dr["CDI_C"]);
}
else
{
      pCDI_C = 0;
}

Open in new window

0
 
RomacaliAuthor Commented:
No the error moved to:
 Object cannot be cast from DBNull to other types.
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.InvalidCastException: Object cannot be cast from DBNull to other types.

Source Error:

Line 262:
Line 263:            sCheckbox = dr["CDI_NA"].ToString();
Line 264:            if (Convert.ToBoolean(dr["CDI_NA"]))
Line 265:            {
Line 266:                CheckBox5.Checked = true;
 private void Get_Data()
    {
        Reset_Data();
 
        SqlCommand cmd = new SqlCommand("sp_GetHospitalData", myConnection);
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.Add(new SqlParameter("@Login", Session["Login"].ToString()));
        cmd.Parameters.Add(new SqlParameter("@DataYr", ddlDataYr.Text.Trim()));
        cmd.Parameters.Add(new SqlParameter("@DataQtr", ddlDataQtr.Text.Trim()));
        SqlDataReader dr = null;
        myConnection.Open();
        dr = cmd.ExecuteReader();
        if (dr.Read())
        {
            String sCheckbox = "";
 
 //  CDI
 
            string xCDIN = Convert.ToString(dr["CDI_N"].ToString());
            if (xCDIN.Length != 0)
            {
                CDI_N = Convert.ToDouble(xCDIN);
                TextBox9.Text = xCDIN;
            }
 
            string xCDID = Convert.ToString(dr["CDI_D"].ToString());
            if (xCDID.Length != 0)
            {
                CDI_D = Convert.ToDouble(xCDID);
                TextBox10.Text = xCDID;
            }
 
            
           //// read CDI_C from DB as an int
            if (!dr.IsDBNull(dr.GetOrdinal("CDI_C")))
            {
                CDI_C = Convert.ToInt32(dr["CDI_C"]);
            }
            else CDI_C = 0;
 
            sCheckbox = dr["CDI_NA"].ToString();
            if (Convert.ToBoolean(dr["CDI_NA"]))
            {
                CheckBox5.Checked = true;
                TextBox9.Text = "";
                TextBox10.Text = "";
            }
        }
        if (dr != null)
            dr.Close();
        myConnection.Close();
    }

Open in new window

0
 
williamcampbellCommented:
Yeah you cant do this

 sCheckbox = dr["CDI_NA".ToString();

 if dr["CDI_NA"] is NULL

 again you have to check

 if ( dr["CDI_NA"] != Null )   .....
0
 
RomacaliAuthor Commented:
I'm completely lost..
the page has 2 ddl one for Data year and one for Data Quarter..
the year has 2007, 2008 and 2009 option
the 2007 is fine it showed me the field to enter the data if I select 2008 gives me this error:

Line 263:            sCheckbox = dr["CDI_NA"].ToString();
Line 264:            if (Convert.ToBoolean(dr["CDI_NA"]))

IF I select 2009 gives me this one:
Line 438:            if (dr["CDI_C"] != null)
Line 439:            {
Line 440:                pCDI_C = Convert.ToDouble(dr["CDI_C"]);
Line 441:            }
Line 442:            else
not sure why it keep going back  forth

Please help me!
0
 
RomacaliAuthor Commented:
The old ones there are fine just these new fields I added are giving me headache
0
 
RomacaliAuthor Commented:
check the codes the VAP is working great
public partial class DataPoint_TrackTestCDI : BasePage
{
    //Initialize variables for calculations
    Double VAP_N;
    Double VAP_D;
    Double VAP_C;
    
    Double CDI_N; //CDI TEST
    Double CDI_D; //CDI
    Double CDI_C; //CDI
 
 
    //Variables for previous month
    Double pVAP_C;
    Double pCDI_C;
 
    protected void Page_Load(object sender, EventArgs e)
    {
        MasterPage myMaster = (MasterPage)this.Master;
        myMaster.Bind_Menu("Empty");
 
        if (Session["OSHPDID"] != null)
        {
            if (!Page.IsPostBack)
            {
                Get_Data();
 
                ddlDataYr.SelectedValue = "2007";
                ddlDataQtr.SelectedValue = "1";
            }
        }
        else
        {
            Session.Contents.RemoveAll();
            FormsAuthentication.SignOut();
            FormsAuthentication.RedirectToLoginPage();
            //Code below redirects client browser to login page when the session times out on the server
            Response.Redirect("login.aspx");
        }
 
 
        Get_PrevMonth();
 
        // add safety features
        CheckBox1.InputAttributes.Add("onChange", "validate();");
        CheckBox5.InputAttributes.Add("onChange", "validate();");
 
 
 
 
 
    }
 
    private void Reset_Data()
    {
        //Clears all fields values on the form
        TextBox1.Text = "";
        TextBox2.Text = "";
        TextBox9.Text = "";
        TextBox10.Text = "";
 
        CheckBox1.Checked = false;
        CheckBox5.Checked = false;
    }
 
    private void Get_Data()
    {
        Reset_Data();
 
        SqlCommand cmd = new SqlCommand("sp_GetHospitalData", myConnection);
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.Add(new SqlParameter("@Login", Session["Login"].ToString()));
        cmd.Parameters.Add(new SqlParameter("@DataYr", ddlDataYr.Text.Trim()));
        cmd.Parameters.Add(new SqlParameter("@DataQtr", ddlDataQtr.Text.Trim()));
        SqlDataReader dr = null;
        myConnection.Open();
        dr = cmd.ExecuteReader();
        if (dr.Read())
        {
            String sCheckbox = "";
 
            //VAP
            //Code assigns database value to a textbox for display and 
            //also assigns it to a variable(double) for calculations
            //VAP_N = Convert.ToInt32(dr["VAP_N"].ToString());
 
            string xVAPN = Convert.ToString(dr["VAP_N"].ToString());
            if (xVAPN.Length != 0)
            {
                VAP_N = Convert.ToDouble(xVAPN);
                TextBox1.Text = xVAPN;
            }
 
            string xVAPD = Convert.ToString(dr["VAP_D"].ToString());
            if (xVAPD.Length != 0)
            {
                VAP_D = Convert.ToDouble(xVAPD);
                TextBox2.Text = xVAPD;
            }
 
            //read VAP_C from DB as an int
            if (dr["VAP_C"] != null)
            {
                VAP_C = Convert.ToInt32(dr["VAP_C"]);
            }
            else VAP_C = 0;
 
            sCheckbox = dr["VAP_NA"].ToString();
            if (Convert.ToBoolean(dr["VAP_NA"]))
            {
                CheckBox1.Checked = true;
                TextBox1.Text = "";
                TextBox2.Text = "";
            }
 
 
          //  CDI
 
            string xCDIN = Convert.ToString(dr["CDI_N"].ToString());
            if (xCDIN.Length != 0)
            {
                CDI_N = Convert.ToDouble(xCDIN);
                TextBox9.Text = xCDIN;
            }
 
            string xCDID = Convert.ToString(dr["CDI_D"].ToString());
            if (xCDID.Length != 0)
            {
                CDI_D = Convert.ToDouble(xCDID);
                TextBox10.Text = xCDID;
            }
 
            
           //// read CDI_C from DB as an int
            if (!dr.IsDBNull(dr.GetOrdinal("CDI_C")))
            {
                CDI_C = Convert.ToInt32(dr["CDI_C"]);
            }
            else CDI_C = 0;
 
            
            sCheckbox = dr["CDI_NA"].ToString();
            if (Convert.ToBoolean(dr["CDI_NA"]))
            {
                CheckBox5.Checked = true;
                TextBox9.Text = "";
                TextBox10.Text = "";
            }
        }
        if (dr != null)
            dr.Close();
        myConnection.Close();
    }
 
    private void Get_PrevMonth()
    {
        SqlCommand cmd3 = new SqlCommand("sp_GetHospitalData", myConnection);
        cmd3.CommandType = CommandType.StoredProcedure;
        cmd3.Parameters.Add(new SqlParameter("@Login", Session["Login"].ToString()));
        cmd3.Parameters.Add(new SqlParameter("@DataYr", ddlDataYr.Text.Trim()));
        cmd3.Parameters.Add(new SqlParameter("@DataQtr", ddlDataQtr.Text.Trim()));
        SqlDataReader dr2 = null;
        myConnection.Open();
        dr2 = cmd3.ExecuteReader();
        if (dr2.Read())
        {
            //read VAP_C from DB as an int
            if (dr2["VAP_C"] != null)
                VAP_C = Convert.ToDouble(dr2["VAP_C"]);
            else VAP_C = 0;
 
 
            //read CDI_C from DB as an int
            //if (dr2["CDI_C"] != null)
            //    CDI_C = Convert.ToDouble(dr2["CDI_C"]);
            //else CDI_C = 0;
 
            
            if (!dr2.IsDBNull(dr2.GetOrdinal("CDI_C")))
            {
                CDI_C = Convert.ToInt32(dr2["CDI_C"]);
            }
            else CDI_C = 0;
 
        }
        if (dr2 != null)
            dr2.Close();
        myConnection.Close();
 
 
        //initialize text fields
        txtCompPrevData.Text = "";
 
        SqlCommand cmd2 = new SqlCommand("sp_GetPreviousQuarterData", myConnection);
        cmd2.CommandType = CommandType.StoredProcedure;
        cmd2.Parameters.Add(new SqlParameter("@Login", Session["Login"].ToString()));
        cmd2.Parameters.Add(new SqlParameter("@DataYr", ddlDataYr.Text.Trim()));
        cmd2.Parameters.Add(new SqlParameter("@DataQtr", ddlDataQtr.Text.Trim()));
        SqlDataReader dr = null;
        myConnection.Open();
        dr = cmd2.ExecuteReader();
        if (dr.Read())
        {
            String sCheckbox = "";
 
            //VAP from previous yr
            pVAP_C = Convert.ToDouble(dr["VAP_C"].ToString());
            if (Convert.ToString(dr["VAP_C"].ToString()) == "")
            {
                txtCompPrevData.Text = "*No data for previous month (VAPC)\n";
            }
            else if (VAP_C == 0)
            {
                //do nothing
            }
            else
            {
                if (VAP_C > (pVAP_C * 1.1) || VAP_C < (pVAP_C * 0.9))
                {
                    txtCompPrevData.Text += "*VAPC: Warning >10% diff from prev. Qtr.  (" + Convert.ToString(VAP_C) + " " + Convert.ToString(pVAP_C) + ")\n";
                }
 
                else
                {
                    txtCompPrevData.Text += "*VAP: Data seems fine.\n";
                }
 
            }
 
            //CDI from previous yr
            if (dr["CDI_C"] != null)
            {
                pCDI_C = Convert.ToDouble(dr["CDI_C"]);
            }
            else
            {
                pCDI_C = 0;
            }
           
            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(CDI_C) + " " + Convert.ToString(pCDI_C) + ")\n";
                }
 
                else
                {
                    txtCompPrevData.Text += "*CDI: Data seems fine.\n";
                }
 
            }
 
            //txtCompPrevData.Text += "\n\nDebug: " + "@" + VAP_C + "@" + CLBSI_C + "@" + MRSA_C + "@" + Sepsis_C + "\n" + pVAP_C + "@" + pCLBSI_C + "@" + pMRSA_C + "@" + pSepsis_C;
 
        }
        if (dr != null)
            dr.Close();
        myConnection.Close();
    }
 
    protected void btnSubmit_Click1(object sender, EventArgs e)
    {
        //Code assigns current text field values to declared variables on button_click event
        //if statement prevents error from trying to convert an empty string
        if (TextBox1.Text != "") VAP_N = Convert.ToDouble(TextBox1.Text);
        if (TextBox2.Text != "") VAP_D = Convert.ToDouble(TextBox2.Text);
        if (TextBox9.Text != "") CDI_N = Convert.ToDouble(TextBox9.Text);
        if (TextBox10.Text != "") CDI_D = Convert.ToDouble(TextBox10.Text);
 
        //Calculations for derived columns - VAP_C, CLBSI_C, MRSA_N, Sepsis_N
        //Formula for VAP_C is N/D*1000
        if (VAP_D != 0) { VAP_C = (VAP_N / VAP_D) * 1000; } else VAP_C = 0;
 
 
        //Formula for CDI_C is N/D*100 (Percentage)
        if (CDI_D != 0) { CDI_C = (CDI_N / CDI_D) * 100; }
 
        SqlCommand myCommand = new SqlCommand("sp_InsertData", myConnection);
        myCommand.CommandType = CommandType.StoredProcedure;
        myCommand.Parameters.Add(new SqlParameter("@Login", Session["Login"].ToString()));
        myCommand.Parameters.Add(new SqlParameter("@DataYr", ddlDataYr.Text.Trim()));
        myCommand.Parameters.Add(new SqlParameter("@DataQtr", ddlDataQtr.Text.Trim()));
 
        if (CheckBox1.Checked == true)
        {
            myCommand.Parameters.Add(new SqlParameter("@VAP_N", '0'));
            myCommand.Parameters.Add(new SqlParameter("@VAP_D", '0'));
            myCommand.Parameters.Add(new SqlParameter("@VAP_C", '0'));
            myCommand.Parameters.Add(new SqlParameter("@VAP_NA", '1'));
        }
        else
        {
            myCommand.Parameters.Add(new SqlParameter("@VAP_N", TextBox1.Text.Trim()));
            myCommand.Parameters.Add(new SqlParameter("@VAP_D", TextBox2.Text.Trim()));
            myCommand.Parameters.Add(new SqlParameter("@VAP_C", VAP_C));
            myCommand.Parameters.Add(new SqlParameter("@VAP_NA", '0'));
        }
 
 
        if (CheckBox5.Checked == true)
        {
            myCommand.Parameters.Add(new SqlParameter("@CDI_N", '0'));
            myCommand.Parameters.Add(new SqlParameter("@CDI_D", '0'));
            myCommand.Parameters.Add(new SqlParameter("@CDI_C", '0'));
            myCommand.Parameters.Add(new SqlParameter("@CDI_NA", '1'));
        }
        else
        {
            myCommand.Parameters.Add(new SqlParameter("@CDI_N", TextBox9.Text.Trim()));
            myCommand.Parameters.Add(new SqlParameter("@CDI_D", TextBox10.Text.Trim()));
            myCommand.Parameters.Add(new SqlParameter("@CDI_C", CDI_C));
            myCommand.Parameters.Add(new SqlParameter("@CDI_NA", '0'));
        }
 
        try
        {
            myConnection.Open();
            myCommand.ExecuteNonQuery();
            lblMessage.ForeColor = System.Drawing.Color.Black;
        }
        catch (Exception ex)
        {
            lblMessage.Text = ex.Message;
        }
        finally
        {
            myConnection.Close();
            Get_PrevMonth();
 
            Get_Data();
        }
 
        HyperLink lnk = new HyperLink();
        lnk.NavigateUrl = "~/Data/Table1.aspx?yr=" + ddlDataYr.Text.Trim() + "&qtr=" + ddlDataQtr.Text.Trim();
    }
 
    protected void ddlDataQtr_SelectedIndexChanged(object sender, EventArgs e)
    {
        Reset_Data();
        Get_Data();
    }
    protected void ddlDataYr_SelectedIndexChanged(object sender, EventArgs e)
    {
        Reset_Data();
        Get_Data();
    }
}

Open in new window

0
 
williamcampbellCommented:
OK

 Everywhere you do this         dr[ something ].ToString()     will fail if
dr[ something ] is null

You cant say NULL.ToString()

So everywhere you do  dr[ something ].TOString()  you have to change it to

 if ( dr[ something ] != null )  x = dr [ something ] .ToString ();

I think VAP works is because none of the data for dr is NULL
0
 
RomacaliAuthor Commented:
I see I will try and let you know ..
thanks!
I am going bananas
0
 
RomacaliAuthor Commented:
so for the checkbox

is it right?

 if (!dr.IsDBNull(dr.GetOrdinal("CDI_C")))
            {
                CDI_C = Convert.ToInt32(dr["CDI_C"]);
            }
            else CDI_C = 0;

            if dr(["CDI_NA"] != null)
              {
             sCheckbox = dr["CDI_NA"].ToString();
              }
                else
            {
             CheckBox5.Checked = false
            }
            if (Convert.ToBoolean(dr["CDI_NA"]))
            {
                CheckBox5.Checked = true;
                TextBox9.Text = "";
                TextBox10.Text = "";
            }
        }
0
 
RomacaliAuthor Commented:
I'm still getting an error here:
 Object cannot be cast from DBNull to other types.
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.InvalidCastException: Object cannot be cast from DBNull to other types.

Source Error:

Line 267:                CheckBox5.Checked = false;
Line 268:            }
Line 269:            if (Convert.ToBoolean(dr["CDI_NA"]))
Line 270:            {
Line 271:                CheckBox5.Checked = true;
           if (dr["CDI_NA"] != null )
              {
             sCheckbox = dr["CDI_NA"].ToString();
              }
                else
            {
                CheckBox5.Checked = false;
            }
            if (Convert.ToBoolean(dr["CDI_NA"]))
            {
                CheckBox5.Checked = true;
                TextBox9.Text = "";
                TextBox10.Text = "";
            }

Open in new window

0
 
williamcampbellCommented:
No  if (Convert.ToBoolean(dr["CDI_NA"]))

Will blow up ifdr["CDI_NA"] if its null

if ( dr["CDI_NA"] != null ) Convert.ToBoolean( dr["CDI_NA"] );


0
 
RomacaliAuthor Commented:
now I got this:
Exception Details: System.InvalidCastException: Object cannot be cast from DBNull to other types.

Source Error:

Line 178:            else CLBSI_C = 0;
Line 179:
Line 180:            if (dr["CDI_NA"] != null) Convert.ToBoolean(dr["CDI_NA"]);
Line 181:            {

  if (!dr.IsDBNull(dr.GetOrdinal("CDI_C")))
            {
                CDI_C = Convert.ToInt32(dr["CDI_C"]);
            }
            else CDI_C = 0;
 
            sCheckbox = dr["CDI_NA"].ToString();
            if (dr["CDI_NA"] != null) Convert.ToBoolean(dr["CDI_NA"]);
            {
                CheckBox1.Checked = true;
                TextBox1.Text = "";
                TextBox2.Text = "";
            }
            

Open in new window

0
 
tcullerCommented:
There is a large difference between "null" and System.Data.DBNull.

"DBNull" represents a null within the database, while "null" just means that a reference doesn't reference anything at all. DBNull still is "something", so you'll never get a "null" result from a call to a Database value. Try the following code snippet. Any attempt to convert "DBNull" to another type will throw an exception, so that should be a good indication that you must check for DBNull before you do anything. "null" should never be returned, so we shouldn't have to check for that (I hope, heh).
CDI_C = (dr.IsDBNull(dr.GetOrdinal("CDI_C"))) ? 0 : Convert.ToInt32(dr["CDI_C"]);
//sCheckbox = dr["CDI_NA"].ToString(); What exactly is this?
 
if(!dr.IsDBNull(dr.GetOrdinal("CDI_NA")
{
     CheckBox1.Checked = true;
     TextBox1.Text = TextBox2.Text = ""
}

Open in new window

0
 
crazymanCommented:
Create a utility class with a method something like..

public static T GetValueFromDB<T>(object o, object defaultValue)
        {
            return o == DBNull.Value ? (T)defaultValue : (T)o;
        }

public static T GetValueFromDB<T>(object o)
        {
            return GetValueFromDB<T>(o, default(T));
        }

then

bool b = Utils.GetValueFromDB<bool>(dr["SomeColumn"],false);

0
 
RomacaliAuthor Commented:
ok.. now how can I check this:

How can I check this now:
 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.
it points to: Line 244:                CDI_N = Convert.ToDouble(xCDIN);
Source Error:

Line 242:            if (xCDIN.Length != 0)
Line 243:            {
Line 244:                CDI_N = Convert.ToDouble(xCDIN);
Line 245:                TextBox9.Text = xCDIN;
Line 246:            }
  string xCDIN = Convert.ToString(dr["CDI_N"].ToString());
            if (xCDIN.Length != 0)
            {
                CDI_N = Convert.ToDouble(xCDIN);
                TextBox9.Text = xCDIN;
            }
 
            string xCDID = Convert.ToString(dr["CDI_D"].ToString());
            if (xCDID.Length != 0)
            {
                CDI_D = Convert.ToDouble(xCDID);
                TextBox10.Text = xCDID;
            }

Open in new window

0
 
williamcampbellCommented:
use this function every time you need to convert the dr to a string

String   ConvertToString ( DataRow dr )
{
     if ( dr != null )
    {
            return dr.ToString ();
    }
else
  {
    return "0";
 }
}

   string  xCDIN = ConvertToString ( dr["CDI_N" ] );
use this function
 
String   ConvertToString ( DataRow dr )
{
     if ( dr != null )
    {
            return dr.ToString ();
    }
else
  {
    return "0";
 }
}
 
   string  xCDIN = ConvertToString ( dr["CDI_N" ] );

Open in new window

0
 
RomacaliAuthor Commented:
thanks
I'm trying to put this page together.
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!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now