?
Solved

Name does no exist in the current context

Posted on 2010-11-30
2
Medium Priority
?
789 Views
Last Modified: 2012-06-27
I am trying to populate a DataSet in one Event Handler, then check the data in that dataset in another Event Handler and am getting the error:

The name 'myData' does not exist in the current context.

How do I make myData available to the other Event Handler? I thought Data Tables were accessible thorughout the code-behind?

<asp:Repeater ID="degreeDetail" runat="server" DataSourceID="dsDegree" OnItemDataBound="Repeater1_ItemDataBound"> 
<ItemTemplate>
    <asp:HiddenField ID="hdnID" runat="server" Value='<%# Eval("ID") %>' />
    <h1><asp:Label ID="lblDegreeName" runat="server" Text='<%# Eval("degreeName") %>'></asp:Label></h1>
    <p><asp:Literal ID="litDescription" runat="server" Text='<%# Eval("description") %>'></asp:Literal></p>
    <h2><asp:Label ID="lblAdmissionHdr" runat="server" Text="Admission"></asp:Label></h2>
    <p><asp:Literal ID="litAdmission" runat="server" Text='<%# Eval("admission") %>'></asp:Literal></p>
    <h2><asp:Label ID="lblStandardsHdr" runat="server" Text="Standards"></asp:Label></h2>
    <p><asp:Literal ID="litStandards" runat="server" Text='<%# Eval("standards") %>'></asp:Literal></p>
    <h2><asp:Label ID="lblAdvisingHdr" runat="server" Text="Advising"></asp:Label></h2>
    <p><asp:Literal ID="litAdvising" runat="server" Text='<%# Eval("advising") %>'></asp:Literal></p>
    <h2><asp:Label ID="lblPrerequisitesHdr" runat="server" Text="Prerequisites"></asp:Label></h2>
    <p><asp:Literal ID="litPrerequisites" runat="server" Text='<%# Eval("prerequisites") %>'></asp:Literal></p>
    <h2><asp:Label ID="lblRequirementsHdr" runat="server" Text="Requirements"></asp:Label></h2>
    <p><asp:Literal ID="litRequirements" runat="server" Text='<%# Eval("requirements") %>'></asp:Literal></p> 
     <asp:Repeater ID="courseDetail" runat="server" OnItemDatabound="Repeater2_ItemDataBound">
        <HeaderTemplate>
            <table width="95%" cellpadding="3" cellspacing="0" border="0" align="center">
        </HeaderTemplate>
        <ItemTemplate>
            <asp:Literal ID="litCategory" runat="server" Text=""></asp:Literal>
            <asp:Literal ID="litSubcategory" runat="server" Text=""></asp:Literal>
            <tr>
            <td width="5%">&nbsp;</td>
            <td align="left" valign="top" width="15%">
            <asp:Hyperlink ID="hlCourse" runat="server"></asp:Hyperlink>
            </td>
            <td align="left" valign="top" width="70%">
                <asp:Label ID="lblCourseName" runat="server" Text="" Visible="false"></asp:Label>
                <asp:HyperLink ID="hlCourseName" runat="server"></asp:HyperLink></td>
            <td align="left" valign="top" width="10%" nowrap="nowrap">.....<asp:Label ID="lblCredits" runat="server" Text=""></asp:Label></td>
           
        </ItemTemplate>
        <FooterTemplate>
            </table>
        </FooterTemplate>
        </asp:Repeater> 
    <p></p>
    <p><asp:Literal ID="litOther" runat="server" Text='<%# Eval("otherInfo") %>'></asp:Literal></p>
    <h2><asp:Label ID="lblCertificationHdr" runat="server" Text="Certification" Visible="false"></asp:Label></h2>
    <p><asp:Literal ID="litCertification" runat="server" Text='<%# Eval("certification") %>'></asp:Literal></p>
    </ItemTemplate>
    </asp:Repeater>

Open in new window

protected void Repeater1_ItemDataBound(Object Sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item)
            {
                string adm = DataBinder.Eval(e.Item.DataItem, "admission").ToString();
                if ((adm == "") || (adm == null))
                {
                    ((Label)e.Item.FindControl("lblAdmissionHdr")).Visible = false;
                }
                string st = DataBinder.Eval(e.Item.DataItem, "standards").ToString();
                if ((st == "") || (st == null))
                {
                    ((Label)e.Item.FindControl("lblStandardsHdr")).Visible = false;
                }
                string advising = DataBinder.Eval(e.Item.DataItem, "advising").ToString();
                if ((advising == "") || (advising == null))
                {
                    ((Label)e.Item.FindControl("lblAdvisingHdr")).Visible = false;
                }
                string prereq = DataBinder.Eval(e.Item.DataItem, "prerequisites").ToString();
                if ((prereq == "") || (prereq == null))
                {
                    ((Label)e.Item.FindControl("lblPrerequisitesHdr")).Visible = false;
                }
                string req = DataBinder.Eval(e.Item.DataItem, "requirements").ToString();
                if ((req == "") || (req == null))
                {
                    ((Label)e.Item.FindControl("lblRequirementsHdr")).Visible = false;
                }
                string cert = DataBinder.Eval(e.Item.DataItem, "certification").ToString();
                if ((cert == "") || (cert == null))
                {
                    ((Label)e.Item.FindControl("lblCertificationHdr")).Visible = false;
                }
                
                string degreeID = DataBinder.Eval(e.Item.DataItem, "ID").ToString();

                string connStr = ConfigurationManager.ConnectionStrings["ruUtility"].ToString();
                SqlConnection conn = new SqlConnection(connStr);
                SqlCommand cmd = new SqlCommand("sp_courseRecords", conn);
                cmd.Parameters.AddWithValue("@ID", degreeID);
                SqlDataAdapter myAdapter = new SqlDataAdapter(cmd);
                DataSet myData = new DataSet();
                myAdapter.Fill(myData, "courses");

                Repeater cd = ((Repeater)e.Item.FindControl("courseDetail"));
                cd.DataSource = myData;
                cd.DataBind();

            }
        }

protected void Repeater2_ItemDataBound(Object Sender, RepeaterItemEventArgs e)
        {
            /* Course data returned:
             * subjectID, courseNumber, courseName, credits, category, subcategory */
            if (e.Item.ItemType == ListItemType.Item)
            {
                foreach (DataRow row in myData.Tables["courses"].Rows())
                {
                    string category = (string)row["category"];
                    string subcat = (string)row["subcategory"];
                    //set up counter
                    //if counter = 0 then move category/subcategory into session
                    //check for equality between session & reader for cat & subcat
                    //if unequal, move new value into session

                    // CATEGORY HEADING
                    if ((category == "") || (category == "0"))
                    {
                        ((Literal)e.Item.FindControl("litCategory")).Visible = false;
                    }
                    else
                    {
                        ((Literal)e.Item.FindControl("litCategory")).Visible = true;
                        ((Literal)e.Item.FindControl("litCategory")).Text = "<tr><td colspan='4' align='left'><h2>" +
                            category + "</h2></td></tr>";
                    }
                    // SUBCATEGORY HEADING
                    if ((subcat == "") || (subcat == "0"))
                    {
                        ((Literal)e.Item.FindControl("litSubcategory")).Visible = false;
                    }
                    else
                    {
                        ((Literal)e.Item.FindControl("litSubcategory")).Visible = true;
                        ((Literal)e.Item.FindControl("litSubcategory")).Text = "<tr><td width='5%'>&nbsp;</td><td colspan='3' align='left'><h3><i>" +
                           subcat + "</i></h3></td></tr>";
                    }
                    // COURSE ROWS
                    string subj = (string)row["subjectID"];
                    string course = (string)row["courseNumber"];
                    string cname = (string)row["courseName"];
                    string credits = (string)row["credits"];

                    if ((subj == "") || (subj == "0") || (course == "") || (course == "0"))
                    {
                        ((HyperLink)e.Item.FindControl("hlCourse")).Visible = false;
                        ((HyperLink)e.Item.FindControl("hlCourseName")).Visible = false;
                        // No link, just course name
                        ((Label)e.Item.FindControl("lblCourseName")).Text = cname;
                        ((Label)e.Item.FindControl("lblCourseName")).Visible = true;
                    }
                    else
                    {
                        // Show linked subject & course
                        ((HyperLink)e.Item.FindControl("hlCourse")).Text = subj + " " + course;
                        ((HyperLink)e.Item.FindControl("hlCourse")).NavigateUrl =
                            Session["path"] + "Course.aspx?subj=" + subj + "&num=" + course;
                        ((HyperLink)e.Item.FindControl("hlCourse")).Visible = true;
                        // Hide unlinked course name
                        ((Label)e.Item.FindControl("lblCourseName")).Visible = false;
                    }
                    if ((cname == "") || (cname == "0"))
                    {
                        ((HyperLink)e.Item.FindControl("hlCourseName")).Visible = false;
                    }
                    else
                    {
                        // Show linked course name
                        ((HyperLink)e.Item.FindControl("hlCourseName")).Text = cname;
                        ((HyperLink)e.Item.FindControl("hlCourseName")).Visible = true;
                        ((HyperLink)e.Item.FindControl("hlCourseName")).NavigateUrl =
                            Session["path"] + "Course.aspx?subj=" + subj + "&num=" + course;
                    }

                    if ((credits == "") || (credits == "0"))
                    {
                        ((Label)e.Item.FindControl("lblCredits")).Visible = false;
                    }
                    else
                    {
                        ((Label)e.Item.FindControl("lblCredits")).Visible = true;
                        ((Label)e.Item.FindControl("lblCredits")).Text = credits;
                    }

                }
            }
        }

Open in new window

0
Comment
Question by:vcbertini
[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
2 Comments
 
LVL 52

Accepted Solution

by:
Carl Tawn earned 2000 total points
ID: 34242410
They are, but you have myData declared within the ItemDataBound method, which makes it local to that method. Simply move the variable declaration to the class-level.

i.e. Put:

    DataSet myData;

Outside of any method in your page. Then in the Repeater1_ItemDataBound method change:

    DataSet myData = new DataSet();

To:

    myData = new DataSet();
     
0
 

Author Closing Comment

by:vcbertini
ID: 34242466
I didn't know you could do that! Thanks!
0

Featured Post

Technology Partners: 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!

Question has a verified solution.

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

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Suggested Courses
Course of the Month8 days, 18 hours left to enroll

764 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