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

c# error message:The name 'TotalDays3' does not exist in the current context

Hi experts, I am getting the following error in c# with visual web developer 2005 express edition.
My code is at the end, I do realize I have not added any namespace for type class libraries such as:
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
Compiler Error Message: CS0103: The name 'TotalDays3' does not exist in the current context
Source Error:
Line 114:            Response.Write("TotalDays:");
Line 115:            Response.Write("#");
Line 116:            Response.Write(TotalDays3);
Line 117:            Response.Write("#");
Line 118:        }
 The complete code is as follows, it works if line 116 is deleted.
<script runat="server">
    void Page_Load(Object Src, EventArgs E)
    {
        if (!Page.IsPostBack)
        {
            ArrayList YAL = new ArrayList();
            ArrayList MAL = new ArrayList();
            ArrayList DAL = new ArrayList();
            int i = 0;
            int a = 1900;
            int j = 0;
            int b = 1;
            int k = 0;
            int c = 1;

            ArrayList YALE = new ArrayList();
            ArrayList MALE = new ArrayList();
            ArrayList DALE = new ArrayList();
            int l = 0;
            int d = 1900;
            int m = 0;
            int e = 1;
            int n = 0;
            int f = 1;
           
            while (i < 110)
            {
                YAL.Add(a++);
                i++;
            }
            Dropdownlist1.Items.Add("");
            Dropdownlist1.DataSource = YAL;
            Dropdownlist1.DataBind();
            while (l < 110)
            {
                YALE.Add(d++);
                l++;
            }
            Dropdownlist4.Items.Add("");
            Dropdownlist4.DataSource = YALE;
            Dropdownlist4.DataBind();
           
            while (j < 12)
            {
                MAL.Add(b++);
                j++;
            }
            Dropdownlist2.Items.Add("");
            Dropdownlist2.DataSource = MAL;
            Dropdownlist2.DataBind();
            while (m < 12)
            {
                MALE.Add(e++);
                m++;
            }
            Dropdownlist5.Items.Add("");
            Dropdownlist5.DataSource = MALE;
            Dropdownlist5.DataBind();
           
            while (k < 30)
            {
                DAL.Add(c++);
                k++;
            }
            Dropdownlist3.Items.Add("");
            Dropdownlist3.DataSource = DAL;
            Dropdownlist3.DataBind();
            while (n < 30)
            {
                DALE.Add(f++);
                n++;
            }
            Dropdownlist6.Items.Add("");
            Dropdownlist6.DataSource = DALE;
            Dropdownlist6.DataBind();

            long years = 0;
            years = Convert.ToInt64(Dropdownlist1.SelectedItem.Value);
            years = (years - 1900) * 365;
            ///Response.Write(years + "days + ");
            long months = 0;
            months = Convert.ToInt64(Dropdownlist2.SelectedItem.Value);
            months = months * 30;
            ///Response.Write(months + "days + ");
            long days = 0;
            days = Convert.ToInt64(Dropdownlist3.SelectedItem.Value);
            ///Response.Write(days + "days = ");
            long TotalDays = 0;
            TotalDays = years + months + days;
            ///Response.Write(TotalDays + "Total days");
            ///Response.Write("            ");
            long years2 = 0;
            years2 = Convert.ToInt64(Dropdownlist4.SelectedItem.Value);
            years2 = (years2 - 1900) * 365;
            ///Response.Write(years2 + "days + ");
            long months2 = 0;
            months2 = Convert.ToInt64(Dropdownlist5.SelectedItem.Value);
            months2 = months2 * 30;
            ///Response.Write(months2 + "days + ");
            long days2 = 0;
            days2 = Convert.ToInt64(Dropdownlist6.SelectedItem.Value);
            ///Response.Write(days2 + "days = ");
            long TotalDays2 = 0;
            TotalDays2 = years2 + months2 + days2;
           
            long TotalDays3 = 0;
            TotalDays3 = TotalDays2 - TotalDays;
            ///output3.Text = "TotalDays:" + TotalDays2.ToString();
            ///Response.Write("<br/><br/><br/><br/><br/><br/><br/>" + "TotalDays:" + TotalDays3);
        }
        else
        {
            Response.Write("TotalDays:");
            Response.Write("#");
            Response.Write(TotalDays3);
            Response.Write("#");
        }
    }
    protected void Button1_Click1(object sender, EventArgs e)
    {
        output.Text = "Day:" + Dropdownlist3.SelectedItem.Value + " month:" + Dropdownlist2.SelectedItem.Value + " year:" + Dropdownlist1.SelectedItem.Value + " ";
    }
    protected void Button2_Click2(object sender, EventArgs e)
    {
        output2.Text = "Day:" + Dropdownlist6.SelectedItem.Value + " month:" + Dropdownlist5.SelectedItem.Value + " year:" + Dropdownlist4.SelectedItem.Value;
    }
    </script>
<html>
<head>
<title>Date Test</title>
</head>
<body>
<form id="form1" runat="server">
<asp:ValidationSummary id="validSummary" runat="server"
headerText="*** Errors On Your Form ***"
showSummary="True"
displayMode="List" />
    <br />
<asp:DropDownList runat="server" id="Dropdownlist1" />
<asp:DropDownList runat="server" id="Dropdownlist2" />
<asp:DropDownList runat="server" id="Dropdownlist3" />
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click1" />
<asp:Label runat="server" id="output" />

<asp:DropDownList runat="server" id="Dropdownlist4" />
<asp:DropDownList runat="server" id="Dropdownlist5" />
<asp:DropDownList runat="server" id="Dropdownlist6" />
<asp:Button ID="Button2" runat="server" Text="Button" OnClick="Button2_Click2" />
<asp:Label runat="server" id="output2" />
    <br />
    <br />
<asp:Label runat="server" id="output3" />
</form>
</body>
</html>

0
homeshopper
Asked:
homeshopper
1 Solution
 
Bob LearnedCommented:
C# has very strict variable scoping rules.  You declared the variable inside the if block, and tried to access it in the else--no can do.

You would have to move the declaration outside of the if block.

      long TotalDays3 = 0;

Bob


0
 
homeshopperAuthor Commented:
Thanks Bob,
I have moved alot of the code around as you suggested.
It now works displaying two dates and the number of days in between.
I have listed the revised code below:
<%@ Page Language="C#" Debug="true" Trace="False"%>
<script runat="server">
    void Page_Load(Object Src, EventArgs E)
    {
        long years = 0;
        long years2 = 0;
        long months2 = 0;
        long months = 0;
        long days2 = 0;
        long days = 0;
        long TotalDays = 0;
        long TotalDays2 = 0;
        long TotalDays3 = 0;
        if (!Page.IsPostBack)
        {
            ArrayList YAL = new ArrayList();
            ArrayList MAL = new ArrayList();
            ArrayList DAL = new ArrayList();
            int i = 0;
            int a = 1900;
            int j = 0;
            int b = 1;
            int k = 0;
            int c = 1;

            ArrayList YALE = new ArrayList();
            ArrayList MALE = new ArrayList();
            ArrayList DALE = new ArrayList();
            int l = 0;
            int d = 1900;
            int m = 0;
            int e = 1;
            int n = 0;
            int f = 1;
           
            while (i < 110)
            {
                YAL.Add(a++);
                i++;
            }
            Dropdownlist1.Items.Add("");
            Dropdownlist1.DataSource = YAL;
            Dropdownlist1.DataBind();
            while (l < 110)
            {
                YALE.Add(d++);
                l++;
            }
            Dropdownlist4.Items.Add("");
            Dropdownlist4.DataSource = YALE;
            Dropdownlist4.DataBind();
           
            while (j < 12)
            {
                MAL.Add(b++);
                j++;
            }
            Dropdownlist2.Items.Add("");
            Dropdownlist2.DataSource = MAL;
            Dropdownlist2.DataBind();
            while (m < 12)
            {
                MALE.Add(e++);
                m++;
            }
            Dropdownlist5.Items.Add("");
            Dropdownlist5.DataSource = MALE;
            Dropdownlist5.DataBind();
           
            while (k < 30)
            {
                DAL.Add(c++);
                k++;
            }
            Dropdownlist3.Items.Add("");
            Dropdownlist3.DataSource = DAL;
            Dropdownlist3.DataBind();
            while (n < 30)
            {
                DALE.Add(f++);
                n++;
            }
            Dropdownlist6.Items.Add("");
            Dropdownlist6.DataSource = DALE;
            Dropdownlist6.DataBind();

            years = Convert.ToInt64(Dropdownlist1.SelectedItem.Value);
           
            months = Convert.ToInt64(Dropdownlist2.SelectedItem.Value);
           
            days = Convert.ToInt64(Dropdownlist3.SelectedItem.Value);
           
            years2 = Convert.ToInt64(Dropdownlist4.SelectedItem.Value);
           
            months2 = Convert.ToInt64(Dropdownlist5.SelectedItem.Value);
           
            days2 = Convert.ToInt64(Dropdownlist6.SelectedItem.Value);
        }
        else
        {
            years = Convert.ToInt64(Dropdownlist1.SelectedItem.Value);
            years = (years - 1900) * 365;
            months = Convert.ToInt64(Dropdownlist2.SelectedItem.Value);
            months = months * 30;
            days = Convert.ToInt64(Dropdownlist3.SelectedItem.Value);
            years2 = Convert.ToInt64(Dropdownlist4.SelectedItem.Value);
            years2 = (years2 - 1900) * 365;
            months2 = Convert.ToInt64(Dropdownlist5.SelectedItem.Value);
            months2 = months2 * 30;
            days2 = Convert.ToInt64(Dropdownlist6.SelectedItem.Value);
            TotalDays = years + months + days;
            TotalDays2 = years2 + months2 + days2;
            TotalDays3 = TotalDays2 - TotalDays;
            Response.Write("TotalDays:");
            Response.Write("#");
            Response.Write(TotalDays3);
            Response.Write("#");
        }
    }
    protected void Button1_Click1(object sender, EventArgs e)
    {
        output.Text = "Day:" + Dropdownlist3.SelectedItem.Value + " month:" + Dropdownlist2.SelectedItem.Value + " year:" + Dropdownlist1.SelectedItem.Value + " ";
    }
    protected void Button2_Click2(object sender, EventArgs e)
    {
        output2.Text = "Day:" + Dropdownlist6.SelectedItem.Value + " month:" + Dropdownlist5.SelectedItem.Value + " year:" + Dropdownlist4.SelectedItem.Value;
    }
    </script>
<html>
<head>
<title>Date Test</title>
</head>
<body>
<form id="form1" runat="server">
<asp:ValidationSummary id="validSummary" runat="server"
headerText="*** Errors On Your Form ***"
showSummary="True"
displayMode="List" />
    <br />
<asp:DropDownList runat="server" id="Dropdownlist1" />
<asp:DropDownList runat="server" id="Dropdownlist2" />
<asp:DropDownList runat="server" id="Dropdownlist3" />
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click1" />
<asp:Label runat="server" id="output" />

<asp:DropDownList runat="server" id="Dropdownlist4" />
<asp:DropDownList runat="server" id="Dropdownlist5" />
<asp:DropDownList runat="server" id="Dropdownlist6" />
<asp:Button ID="Button2" runat="server" Text="Button" OnClick="Button2_Click2" />
<asp:Label runat="server" id="output2" />
    <br />
    <br />
<asp:Label runat="server" id="output3" />
</form>
</body>
</html>
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

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