locked
I can not convert a varable to Double - Need help. RRS feed

  • Question

  • User-1999980185 posted

    Please see error message when I attempt to convert a variable to double.

    // Get Totals from Listview
            public void Display_Page_Routine()
            {
    
                double tIncome = 0;
    
                foreach (ListViewItem var1 in ListView_Budget.Items)
    
                {
                    // Label Lincome = var1.FindControl(@"dincome");
                    // string Lincome_Data = Lincome.Text;
    
                    
    
    
                    Label Lincome = var1.FindControl(@"dincome") as Label;
                    string Lincome_Data = Lincome == null ? "" : Lincome.Text;
    
                    // Problem below:
                   // When I code double LinecomeDouble = Convert.ToDouble(Lincome.Text);        // Error Message: Object reference not set to an instance of an object. 
                   // Whin I code double LinecomeDouble = Convert.ToDouble(Lincome_Data);       // Error Message: Input string was not in a correct format. 
                  
                    if (Lincome_Data == null)
                    {
                        tIncome = tIncome + LinecomeDouble;
                    }
    
                   
    
                }
    
            }

    Monday, August 6, 2018 2:25 PM

Answers

  • User-1999980185 posted

    Thanks to all that help.  Not getting any errors but It is not falling through the code.  I will have to do this in ItemDataBound because I know it will work in that method. I have only one transaction in the input and the value of dincome ls 250.00 but  when it get to

    if (L_income != null)       // <=== The value is null so it does not execute the add code.  I put a trace on, it is NULL

     private void Display_Page_Routine()
            {
               
                string holder;
    
                double T_income = 0;
    
                foreach (ListViewItem Lviincome in ListView_Budget.Items)
                {
    
                    Label L_income = Lviincome.FindControl("dincome") as Label;
    
                    if (L_income != null)      //   L_income is null but it should be 250.00
                    {
                        string L_income_data = L_income.Text;
                        holder = L_income_data;
    
                        double value;
                        if (Double.TryParse(holder, out value))
                            T_income = T_income + Convert.ToDouble(L_income_data);
                    }
                }
    
    
            }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, August 7, 2018 8:52 AM

All replies

  • User-369506445 posted

    hi

    you can't convert a null or empty value to <g class="gr_ gr_59 gr-alert gr_gramm gr_inline_cards gr_run_anim Style multiReplace" id="59" data-gr-id="59">double ,</g> please try below code

    var LinecomeDouble = Convert.ToDouble(string.IsNullOrEmpty(Lincome.Text) ? "0": Lincome.Text);        
        LinecomeDouble = Convert.ToDouble(string.IsNullOrEmpty(Lincome_Data) ? "0" : Lincome_Data); 

    Monday, August 6, 2018 2:55 PM
  • User753101303 posted

    Hi,

    Do you really expect that "dincome" could be not found at all and that if found it could be another type of control than a Label ? It seems an attempt to add code for fixing a situation that is perhaps not expected to happen anyway ?

    Monday, August 6, 2018 3:26 PM
  • User-1999980185 posted

    OK, I have use the converter and I still have problems.   Any help would be appreciated.  code and error message below.

    Code below:
    
    private void Display_Page_Routine()
            {
               // Iusername = Iusername.ToUpper;
              //  Textbox_iusername.Text = Iusername;
    
                string holder;
    
                double T_income = "0";                      // line 224
    
                foreach (ListViewItem Lviincome in ListView_Budget.Items)
                {
                    Label L_income = Lviincome.FindControl("dincome");   // line 228
                    string L_income_data = L_income.Text;
                    holder = L_income_data;
    
                    if (Information.IsNumeric(holder) == true)           // line 232
                        T_income = T_income + L_income_data;             // line 233
                }
            
            }
    
    
    error message below:
    Severity	Code	Description	Project	File	Line	Suppression State
    Error	CS0029	Cannot implicitly convert type 'string' to 'double'	FirstCWeb	C:\Users\Edward\documents\visual studio 2015\Projects\MyFirstCWeb_Solution\FirstCWeb\Budget_Week_Month_Year.aspx.cs	224	Active
    
    Severity	Code	Description	Project	File	Line	Suppression State
    Error	CS0266	Cannot implicitly convert type 'System.Web.UI.Control' to 'System.Web.UI.WebControls.Label'. An explicit conversion exists (are you missing a cast?)	FirstCWeb	C:\Users\Edward\documents\visual studio 2015\Projects\MyFirstCWeb_Solution\FirstCWeb\Budget_Week_Month_Year.aspx.cs	228	Active
    
    Severity	Code	Description	Project	File	Line	Suppression State
    Error	CS0103	The name 'Information' does not exist in the current context	FirstCWeb	C:\Users\Edward\documents\visual studio 2015\Projects\MyFirstCWeb_Solution\FirstCWeb\Budget_Week_Month_Year.aspx.cs	232	Active
    
    Severity	Code	Description	Project	File	Line	Suppression State
    Error	CS0029	Cannot implicitly convert type 'string' to 'double'	FirstCWeb	C:\Users\Edward\documents\visual studio 2015\Projects\MyFirstCWeb_Solution\FirstCWeb\Budget_Week_Month_Year.aspx.cs	233	Active
    


    Monday, August 6, 2018 5:51 PM
  • User-369506445 posted
    Error	CS0029	Cannot implicitly convert type 'string' to 'double'	FirstCWeb	C:\Users\Edward\documents\visual studio 2015\Projects\MyFirstCWeb_Solution\FirstCWeb\Budget_Week_Month_Year.aspx.cs	224	Active

    the error appears when you have converted a string to double, I highlighted it with yellow color

    Error	CS0266	Cannot implicitly convert type 'System.Web.UI.Control' to 'System.Web.UI.WebControls.Label'. An explicit conversion exists (are you missing a cast?)	FirstCWeb	C:\Users\Edward\documents\visual studio 2015\Projects\MyFirstCWeb_Solution\FirstCWeb\Budget_Week_Month_Year.aspx.cs	228	Active

    the error appear when you can't find the label and it is null, you have to check it for <g class="gr_ gr_357 gr-alert gr_gramm gr_inline_cards gr_run_anim Style multiReplace" id="357" data-gr-id="357">null ,</g> I   highlighted with blue color

    Error	CS0103	The name 'Information' does not exist in the current context	FirstCWeb	C:\Users\Edward\documents\visual studio 2015\Projects\MyFirstCWeb_Solution\FirstCWeb\Budget_Week_Month_Year.aspx.cs	232	Active

    the "Information" variable does not exist in <g class="gr_ gr_526 gr-alert gr_gramm gr_inline_cards gr_run_anim Grammar only-ins replaceWithoutSep" id="526" data-gr-id="526">current</g> scope, you have <g class="gr_ gr_568 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling ins-del multiReplace" id="568" data-gr-id="568">decalre</g> a variable called Information,  I   highlighted with Red color

    Error	CS0029	Cannot implicitly convert type 'string' to 'double'	FirstCWeb	C:\Users\Edward\documents\visual studio 2015\Projects\MyFirstCWeb_Solution\FirstCWeb\Budget_Week_Month_Year.aspx.cs	233	Active

    <g class="gr_ gr_695 gr-alert gr_gramm gr_inline_cards gr_run_anim Style multiReplace" id="695" data-gr-id="695">again ,</g> you Cannot use implicitly convert, you have to use explicit convert, I   highlighted with green color

    please try below code

               string holder;
    
    // double T_income = "0"; double T_income = 0; // line 224 foreach (ListViewItem Lviincome in ListView_Budget.Items) { try { Label L_income = Lviincome.FindControl("dincome") as Label; // line 228 if (L_income != null) { string L_income_data = L_income.Text; holder = L_income_data; if (Information.IsNumeric(holder) == true) // line 232 { T_income = T_income + Convert.ToDouble(L_income_data); // line 233 } } } catch (Exception ex) { Response.Write(ex.Message); } }

    Tuesday, August 7, 2018 4:08 AM
  • User-1999980185 posted

    vahid, Thanks for your help.  I still have one error.  The one you marked in RED did not change and it still has an error. Please see code and error message below:    Am I missing a Using Statement above or something?   It does not like the INFORMATION  statement.

    Code below:
    
    if (Information.IsNumeric(holder) == true)
    
    Error Message Below:
    Severity	Code	Description	Project	File	Line	Suppression State
    Error	CS0103	The name 'Information' does not exist in the current context	FirstCWeb	C:\Users\Edward\documents\visual studio 2015\Projects\MyFirstCWeb_Solution\FirstCWeb\Budget_Week_Month_Year.aspx.cs	233	Active
    

    Tuesday, August 7, 2018 6:55 AM
  • User753101303 posted

    This is from a VB namespace. You could use also:

    double LincomeDouble = 0;
    Double.TryParse(holder,LinecomeDouble);

    Still it means that if holder can't be parsed as a double, you'll use 0 instead
    If the control is not found, you'll use 0.
    If the control is found but is not a label,  you'll use 0.

    I'm not sure if any of this situation is expected. If not, IMO it's better that the code just fails so that the unexpected situation can  be fixed rather than using 0 until someone notice this wrong behavior should be fixed.

    Edit: or add a reference to Microsoft.VisualBasic.dll and use :

     if (Microsoft.VisuaBasic.Information.IsNumeric(holder) == true)  

    I

    Tuesday, August 7, 2018 7:30 AM
  • User-369506445 posted

    what's the "Information<g class="gr_ gr_26 gr-alert gr_gramm gr_inline_cards gr_run_anim Style multiReplace" id="26" data-gr-id="26">" ?</g> the <g class="gr_ gr_38 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling ins-del multiReplace" id="38" data-gr-id="38">varibale</g> doesn't  exist in your method, if you copy and paste this code from otherwhere, you can remove it

                     //if (Information.IsNumeric(holder) == true)           // line 232
                       //     {
                         //       T_income = T_income + Convert.ToDouble(L_income_data);             
                         //   }
    
     T_income = T_income + Convert.ToDouble(L_income_data);
    

    Tuesday, August 7, 2018 7:31 AM
  • User-1999980185 posted

    Now it does not like the new code.  Microsoft.VisuaBasic

    Code below:
     private void Display_Page_Routine()
            {
               // Iusername = Iusername.ToUpper;
              //  Textbox_iusername.Text = Iusername;
    
                string holder;
    
                double T_income = 0;
    
                foreach (ListViewItem Lviincome in ListView_Budget.Items)
                {
                    // Label L_income = Lviincome.FindControl("dincome");
                    Label L_income = Lviincome.FindControl("dincome") as Label;
    
    
                    if (L_income != null)
                    {
                        string L_income_data = L_income.Text;
                        holder = L_income_data;
    
                       if (Microsoft.VisuaBasic.Information.IsNumeric(holder) == true)     // <==== Error 
    
                       //if ((IsNumeric(holder) == true))
    
                        {
                            T_income = T_income + Convert.ToDouble(L_income_data);
                        }
    
                    }
                }
    
    
    Error Message Below:
    Severity	Code	Description	Project	File	Line	Suppression State
    Error	CS0234	The type or namespace name 'VisuaBasic' does not exist in the namespace 'Microsoft' (are you missing an assembly reference?)	FirstCWeb	C:\Users\Edward\documents\visual studio 2015\Projects\MyFirstCWeb_Solution\FirstCWeb\Budget_Week_Month_Year.aspx.cs	237	Active
    

    Tuesday, August 7, 2018 7:51 AM
  • User-369506445 posted

    try below

    private void Display_Page_Routine()
            {
                // Iusername = Iusername.ToUpper;
                //  Textbox_iusername.Text = Iusername;
    
                string holder;
    
                double T_income = 0;
    
                foreach (ListViewItem Lviincome in ListView_Budget.Items)
                {
                    // Label L_income = Lviincome.FindControl("dincome");
                    Label L_income = Lviincome.FindControl("dincome") as Label;
    
    
                    if (L_income != null)
                    {
                        string L_income_data = L_income.Text;
                        holder = L_income_data;
                        T_income = T_income + Convert.ToDouble(L_income_data);
    
                    }
                }
    
    
            }

    Tuesday, August 7, 2018 7:55 AM
  • User-369506445 posted

    also the Information.IsNumric check for the valid number, you can change it to below

    if (L_income != null)
                    {
                        string L_income_data = L_income.Text;
                        holder = L_income_data;
                        double value;
                        if (Double.TryParse(holder, out value))
                           T_income = T_income + Convert.ToDouble(L_income_data);
    
                    }

    Tuesday, August 7, 2018 7:58 AM
  • User-1999980185 posted

    Thanks to all that help.  Not getting any errors but It is not falling through the code.  I will have to do this in ItemDataBound because I know it will work in that method. I have only one transaction in the input and the value of dincome ls 250.00 but  when it get to

    if (L_income != null)       // <=== The value is null so it does not execute the add code.  I put a trace on, it is NULL

     private void Display_Page_Routine()
            {
               
                string holder;
    
                double T_income = 0;
    
                foreach (ListViewItem Lviincome in ListView_Budget.Items)
                {
    
                    Label L_income = Lviincome.FindControl("dincome") as Label;
    
                    if (L_income != null)      //   L_income is null but it should be 250.00
                    {
                        string L_income_data = L_income.Text;
                        holder = L_income_data;
    
                        double value;
                        if (Double.TryParse(holder, out value))
                            T_income = T_income + Convert.ToDouble(L_income_data);
                    }
                }
    
    
            }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, August 7, 2018 8:52 AM
  • User-1003897979 posted

    You have marked your last past as the answer but the words you have written imply that you still have a problem.

    Can you show the .aspx for the page.  It seems that dincome is not a label.

    Thursday, August 9, 2018 5:18 AM