Solved

Where to databind and how to create the relationship between nested repeaters

Posted on 2007-04-03
6
233 Views
Last Modified: 2010-05-18
This is a nesting repeater problem. I don't understand where I indicate the relation between the inner and outer repeaters, and I also can't figure out when/where to instantiate the datasource for each repeater.

I would like to call two separate stored procedures for each repeater. I would like to pass the Collection_ID from the parent repeater to the InnerRepeater.

I'm using vb.net.

My aspx page looks like this:
        <asp:Repeater ID="ParentRepeater" runat="server" OnItemDataBound="rParent_ItemDataBound">
        <HeaderTemplate>
        <table style="LEFT: 178px; WIDTH: 980px; POSITION: absolute; TOP: 195px" id="DataTable">
        <tr>
        <th class="intraContent"><b>Action</b></th>
        <th class="intraContent"><b>Collection Name</b></th>
        <th class="intraContent"><b>Current Version</b></th>
        <th class="intraContent"><b>Description</b></th>
        <th class="intraContent"><b>Lifecycle </b></th>
        <th class="intraContent"><b>Environment</b></th>
        <th class="intraContent"><b>Released On</b></th>
        <th class="intraContent"><b>Published On </b></th>
        <th class="intraContent"><b>Packages</b></th>
        <th class="intraContent"><b>Queries</b></th>
        <th class="intraContent"><b>Hosts</b></th>
        </tr>
       
        </headertemplate>
        <ItemTemplate>
        <tr>
        <td class="intraContent">

        <asp:repeater ID="InnerRepeater" runat="Server">
        <ItemTemplate>
        <%# DataBinder.Eval(Container.DataItem, "OBJECT_ACTION") %>

        </ItemTemplate>
        </asp:repeater>
        </td>
        <td class="intraContent" onmouseover="this.className='intraContentHover'" onmouseout="this.className='intraContent'"><%#DataBinder.Eval(Container.DataItem, "COLLECTION_NAME")%></td>
       
        <td class="intraContent" align="center" style="border-bottom: 2px solid #D3D3D3" onmouseover="this.className='intraContentHover'" onmouseout="this.className='intraContent'"><%#DataBinder.Eval(Container.DataItem, "Current_Version")%></td>
        <td class="intraContent" align="center" style="border-bottom: 2px solid #D3D3D3" onmouseover="this.className='intraContentHover'" onmouseout="this.className='intraContent'"><%#DataBinder.Eval(Container.DataItem, "DESCRIPTION")%></td>
        <td class="intraContent" align="center" style="border-bottom: 2px solid #D3D3D3" onmouseover="this.className='intraContentHover'" onmouseout="this.className='intraContent'"><%#DataBinder.Eval(Container.DataItem, "LIFECYCLE")%></td>
        <td class="intraContent" align="center" style="border-bottom: 2px solid #D3D3D3" onmouseover="this.className='intraContentHover'" onmouseout="this.className='intraContent'"><%#DataBinder.Eval(Container.DataItem, "ENVIRONMENT")%></td>
        <td class="intraContent" align="center" style="border-bottom: 2px solid #D3D3D3" onmouseover="this.className='intraContentHover'" onmouseout="this.className='intraContent'"><% Response.Write("????")%></td>
        <td class="intraContent" align="center" style="border-bottom: 2px solid #D3D3D3" onmouseover="this.className='intraContentHover'" onmouseout="this.className='intraContent'"><%#DataBinder.Eval(Container.DataItem, "PUBLISHED_ON")%></td>
        <td class="intraContent" align="center" style="border-bottom: 2px solid #D3D3D3" onmouseover="this.className='intraContentHover'" onmouseout="this.className='intraContent'"><asp:linkbutton runat="server" ID="linkButtonc1" CssClass="lnkbutton" CommandName="PACKAGES_COUNT" CommandArgument='<%# DataBinder.Eval(Container.DataItem,"COLLECTION_ID") %>'><%#DataBinder.Eval(Container.DataItem, "PACKAGES")%></asp:linkbutton></td>
        <td class="intraContent" align="center" style="border-bottom: 2px solid #D3D3D3" onmouseover="this.className='intraContentHover'" onmouseout="this.className='intraContent'"><asp:linkbutton runat="server" ID="linkButtonc2" CssClass="lnkbutton" CommandName="QUERIES_COUNT" CommandArgument='<%# DataBinder.Eval(Container.DataItem,"COLLECTION_ID") %>'><%#DataBinder.Eval(Container.DataItem, "QUERIES")%></asp:linkbutton></td>
        <td class="intraContent" align="center" style="border-bottom: 2px solid #D3D3D3" onmouseover="this.className='intraContentHover'" onmouseout="this.className='intraContent'"><asp:linkbutton runat="server" ID="linkButtonc3" CssClass="lnkbutton" CommandName="HOSTS_COUNT" CommandArgument='<%# DataBinder.Eval(Container.DataItem,"COLLECTION_ID") %>'><%#DataBinder.Eval(Container.DataItem, "HOSTS") %></asp:linkbutton></td>
        </tr>
        </ItemTemplate>
        <FooterTemplate>
       
        </table>
        </FooterTemplate>
        </asp:Repeater>


...and the code behind file looks like this:
    Protected Sub LoadData()
        Dim conn As New SqlConnection
        Dim cmd As New SqlCommand
        Dim connstring As String
        Dim da As New SqlDataAdapter
        Dim dt As New DataTable

        connstring = Connector.ConnectStringBuild
        conn.ConnectionString = connstring
        cmd.Connection = conn
        cmd.CommandText = "ACT_GET_COLLECTION_DETAILS"
        cmd.CommandType = CommandType.StoredProcedure


        cmd.Parameters.Add("@SERVICE_ID", SqlDbType.Int).Value = SetVars.Service_ID
        cmd.Parameters.Add("@LOGON_NAME", SqlDbType.VarChar).Value = SetVars.UserName
        cmd.Parameters.Add("@INDICATOR", SqlDbType.VarChar).Value = SetVars.Indicator

        conn.Open()
        da.SelectCommand = cmd
        da.Fill(dt)

        Me.ParentRepeater.DataSource = dt
        Me.ParentRepeater.DataBind()
        conn.Close()




    End Sub
    Protected Function ChildData() As DataTable
        Dim conn As New SqlConnection
        Dim cmd As New SqlCommand
        Dim connstring As String
        Dim da As New SqlDataAdapter
        Dim dt As New DataTable

        Dim childa As New SqlDataAdapter
        connstring = Connector.ConnectStringBuild
        conn.ConnectionString = connstring
        cmd.Connection = conn
        cmd.CommandText = "ACT_GET_FUNCTION_SERVICE_DETAILS"
        cmd.CommandType = CommandType.StoredProcedure

        cmd.Parameters.Add("@GROUP_NAME", SqlDbType.VarChar).Value = SetVars.GroupName
        cmd.Parameters.Add("@SERVICE_ID", SqlDbType.Int).Value = SetVars.Service_ID
        cmd.Parameters.Add("@OBJECT_NAME", SqlDbType.VarChar).Value = "COLLECTION"
        cmd.Parameters.Add("@COLLECTION_ID", SqlDbType.Int).Value = 7
        conn.Open()
        childa.SelectCommand = cmd
        childa.Fill(dt)

        Return dt

        conn.Close()
        conn = Nothing
        cmd = Nothing

    End Function
    Protected Sub rParent_ItemDataBound(ByVal sender As Object, ByVal e As RepeaterItemEventArgs)
        Dim conn As New SqlConnection
        Dim cmd As New SqlCommand
        Dim connstring As String
        Dim da As New SqlDataAdapter
        Dim dt As New DataTable

        connstring = Connector.ConnectStringBuild
        conn.ConnectionString = connstring
        cmd.Connection = conn
        cmd.CommandText = "ACT_GET_COLLECTION_DETAILS"
        cmd.CommandType = CommandType.StoredProcedure


        cmd.Parameters.Add("@SERVICE_ID", SqlDbType.Int).Value = SetVars.Service_ID
        cmd.Parameters.Add("@LOGON_NAME", SqlDbType.VarChar).Value = SetVars.UserName
        cmd.Parameters.Add("@INDICATOR", SqlDbType.VarChar).Value = SetVars.Indicator

        conn.Open()
        da.SelectCommand = cmd
        da.Fill(dt)
        'Me.ParentRepeater.DataSource = dt
        'Me.ParentRepeater.DataBind()
        conn.Close()

        If (e.Item.ItemType = ListItemType.Item) Or (e.Item.ItemType = ListItemType.AlternatingItem) Then
            Dim r As Repeater = CType(e.Item.FindControl("InnerRepeater"), Repeater)
            r.DataSource = ChildData()
            r.DataBind()

        End If
    End Sub


NB: ChildData() is a class that returns a datatable. It is executing another stored procedure separate from the one for the parent repeater.
0
Comment
Question by:rss2
  • 3
  • 3
6 Comments
 
LVL 37

Expert Comment

by:samtran0331
ID: 18842799
would this help?
http://support.microsoft.com/kb/326338

I'm not sure if it meets your data requirement:
"I would like to pass the Collection_ID from the parent repeater to the InnerRepeater."

It does the relation using an ado datarelation and then binds the child to the parent using:
Container.DataItem.Row.GetChildRows("myrelation")
0
 

Author Comment

by:rss2
ID: 18842926
No that doesn't really help me because the problem I'm having is creating the relation between the two datasets, but using stored procedure calls instead of embedded SQL.

I would like to see how to create the two necessary datatsets using stored procedures, then creating the relationship between the two datasets (i.e. without the reference to a table..perhaps with the use of a table index?), and then binding the data.

Thanks anyway!
0
 
LVL 37

Expert Comment

by:samtran0331
ID: 18846305
no I wouldn't recommend using a table/row index...if your primary key isn't always inline with the number of rows...all kinds of bad things happen...

what is the primary key field for the child repeater/datataset?
is it Service_ID? (hope so)

because I think you almost have it, but ChildData would need to receive a parameter like:
ChildData(SetVars.Service_ID)

and it would stay in your rParent_ItemDataBound event

keep in mind that calling a stored procedure for all the children mean multiple calls to the db (one call for each child)...where if you do it like the MSDN article, you have a total of 2 calls to the db...
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 

Author Comment

by:rss2
ID: 18851610
It's working now..sort of.

I'll post the code in a sec for others but could you help me with display issues I'm having..

The inner repeater is supposed to populate a dropdownlist. At the moment I have the inner repeater returning the correct data but only in a list. How do I get that information into a drop down list?

Here's what my aspx page has for the inner repeater:
        <asp:Repeater ID="InnerRepeater" runat="Server">
        <ItemTemplate>
        <tr>
        <td class="intraContent">
<%#DataBinder.Eval(Container.DataItem, "OBJECT_ACTION")%>
        </td>
</itemtemplate>
</asp:Repeater>

...so it is the OBJECT_ACTION values which I would like to put into <asp:DropDownList> I have tried everything and can't get it to work!

Thanks,
rss2
0
 
LVL 37

Accepted Solution

by:
samtran0331 earned 500 total points
ID: 18852003
what have you tried?

First replace:
<%#DataBinder.Eval(Container.DataItem, "OBJECT_ACTION")%>
with a dropdownlist...then...

in your rParent_ItemDataBound, you use findcontrol to get the child repeater...you need to do the same thing to "findcontrol" the dropdownlist inside the child repeater you dimmed as "r"...
then bind the dropdownlist and set the correct value

0
 

Author Comment

by:rss2
ID: 18856698
Ok.. now it's complaining about using the "New" keyword?

Here is my code:
    Protected Sub ParentRepeater_ItemDataBound(ByVal sender As Object, ByVal e As RepeaterItemEventArgs)

        If (e.Item.ItemType = ListItemType.Item) Or (e.Item.ItemType = ListItemType.AlternatingItem) Then
            Dim r As Repeater = CType(e.Item.FindControl("InnerRepeater"), Repeater)
            r.DataSource = ChildData()
            r.DataBind()
            Dim theDropDownList As New DropDownList
            theDropDownList = CType(FindControl("ActionDropDownList"), DropDownList)
            theDropDownList.DataSource = r.DataSource
            theDropDownList.DataBind()

        End If

    End Sub

It doesn't like this line:
theDropDownList.DataSource = r.DataSource
It says "Object reference not set to an instance of an object"
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

863 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

28 Experts available now in Live!

Get 1:1 Help Now