Solved

Nested Repeaters - One Datasource depends on another

Posted on 2010-11-15
6
403 Views
Last Modified: 2012-08-13
I have a dataset as follows:

Degree category  (dataset: degrees)  : ID = 1
-- degree details 1
-- degree details 2

Course category (dataset: courses)  --> parameter ID = 1
-- course details 1
-- course details 2

Degree category2  (dataset: degrees)  : ID = 2
-- degree details 1
-- degree details 2

Course category (dataset: courses)  --> parameter ID = 2
-- course details 1
-- course details 2

As the page iterates through the first record in the degree dataset, I need to grab the ID value and use it as a parameter in my second dataset (courses).

I tried passing the value into a hidden field then grabbing the value from that in the code behind, but each time I test, I'm getting a null value in the hidden field, which is causing the application to terminate. Could it be that I'm doing this in the page load and the hidden field isn't populated yet? If so, how can I make sure I grab that value at the right moment?
<asp:SqlDataSource ID="dsDegree" runat="server" 

        ConnectionString="" 

        SelectCommand="sp_degreeRecord" SelectCommandType="StoredProcedure">       

    </asp:SqlDataSource>

        

    <asp:SqlDataSource ID="dsCourses" runat="server" 

        ConnectionString="" 

        SelectCommand="sp_courseRecords" SelectCommandType="StoredProcedure">     

    </asp:SqlDataSource>



<asp:Repeater ID="degreeDetails" runat="server" DataSourceID="dsDegree"> 

<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="rptCourseInfo" runat="server" DataSourceID="dsCourses">

        <HeaderTemplate><table width="95%" cellpadding="3" cellspacing="0" border="0" align="center"></HeaderTemplate>

        <ItemTemplate>

        <tr>

        <td width="5%">&nbsp;</td>

        <td width="15%"><%# Eval("subjectID") %> <%# Eval("courseNumber") %></td>

        <td align="left" valign="top" width="70%"><%# Eval("courseName") %></td>

        <td align="left" valign="top" width="10%">.....<%# Eval("credits") %></td>

        </ItemTemplate>

        <FooterTemplate></table></FooterTemplate>

        </asp:Repeater>

        

    <p><asp:Literal ID="litOther" runat="server" Text='<%# Eval("otherInfo") %>'></asp:Literal></p>

    <h2><asp:Label ID="lblCertificationHdr" runat="server" Text="Certification"></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 Page_Load(object sender, EventArgs e)

        {

         

            string id = Request.QueryString["ID"];



            dsDegree.SelectParameters.Add(new Parameter("ID", TypeCode.Int32)); 

            dsDegree.SelectParameters["ID"].DefaultValue = id;          



            String subcatID = ((HiddenField)Page.FindControl("hdnID")).Value;



            dsCourses.SelectParameters.Add(new Parameter("ID", TypeCode.Int32));

            dsCourses.SelectParameters["ID"].DefaultValue = subcatID;



        }

Open in new window

0
Comment
Question by:vcbertini
  • 3
  • 3
6 Comments
 
LVL 12

Accepted Solution

by:
jagssidurala earned 500 total points
Comment Utility
0
 

Author Comment

by:vcbertini
Comment Utility
I have tried a multitude of what was suggested on those pages and here is what I am getting...

"c" is being returned as Null even though the data source clearly exists (see 2nd code attachment).  

and it won't add a parameter to a datasource it isn't finding, thus the error.
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="CatalogDegree.ascx.cs" Inherits="RooseveltCMS.layouts.catalogDegreeSublayout" %> 

 

    <asp:SqlDataSource ID="dsDegree" runat="server" 

        ConnectionString="" 

        SelectCommand="sp_degreeRecord" SelectCommandType="StoredProcedure">       

    </asp:SqlDataSource>

    

    <asp:SqlDataSource ID="dsCourses" runat="server" 

        ConnectionString="" 

        SelectCommand="sp_courseRecords" SelectCommandType="StoredProcedure">     

    </asp:SqlDataSource> 

       

<div id="CenterColumn">

<form runat="server">



<asp:Repeater ID="degreeDetail" runat="server" DataSourceID="dsDegree" OnItemDataBound="R1_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" DataSourceID="dsCourses">

        <HeaderTemplate><table width="95%" cellpadding="3" cellspacing="0" border="0" align="center"></HeaderTemplate>

        <ItemTemplate>

        <tr>

        <td width="5%">&nbsp;</td>

        <td width="15%"><%# Eval("subjectID") %> <%# Eval("courseNumber") %></td>

        <td align="left" valign="top" width="70%"><%# Eval("courseName") %></td>

        <td align="left" valign="top" width="10%">.....<%# Eval("credits") %></td>

        </ItemTemplate>

        <FooterTemplate></table></FooterTemplate>

        </asp:Repeater>

        

    <p><asp:Literal ID="litOther" runat="server" Text='<%# Eval("otherInfo") %>'></asp:Literal></p>

    <h2><asp:Label ID="lblCertificationHdr" runat="server" Text="Certification"></asp:Label></h2>

    <p><asp:Literal ID="litCertification" runat="server" Text='<%# Eval("certification") %>'></asp:Literal></p>

    </ItemTemplate>

    </asp:Repeater>

</form>

    </div>

Open in new window

protected void R1_ItemDataBound(Object Sender, RepeaterItemEventArgs e)

        {

            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem) 

            {

               // Find the Inner DataSource control in this Row. 

               SqlDataSource c = (SqlDataSource)e.Item.FindControl("dsCourses");



                // Create the parameter for the child datasource

                c.SelectParameters.Add(new Parameter("ID", TypeCode.String));



                // Set the SelectParameter for this DataSource control by re-evaluating the field that is to be passed.

                c.SelectParameters["ID"].DefaultValue = DataBinder.Eval(e.Item.DataItem, "ID").ToString();

            }

        }

    }

Open in new window

0
 
LVL 12

Assisted Solution

by:jagssidurala
jagssidurala earned 500 total points
Comment Utility
Place "dsCourses" datasource in main repeater.

and third i have given above will satisfy your needs, try once like that
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

by:vcbertini
Comment Utility
I tried that, the dscourses entry disappears from the designer page, and then I get an error that the object reference cannot be found.
0
 
LVL 12

Expert Comment

by:jagssidurala
Comment Utility
you need to find from main control after placing inside
0
 

Author Closing Comment

by:vcbertini
Comment Utility
Not sure what changed in my development environment, but when I moved the dataset into the main Repeater, today it didn't disappear - it worked. Thanks for pointing me in the right direction.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

744 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now