Saturday, February 19, 2011

Nested Datalists using stored procedure parameters

Hi,

I'm working with some nested datalist controls and cannot get the SP parameters for my nested stored procedure to work.

In debug I can see that SqlDataSource2.SelectParameters.Add("Section",oLabel.Text.ToString()); is getting the correct value from the label but when the results display I always get the results for the last parameter added?

I'm guessing I need to clear the parameters in some way each time the nested datalist is bound but if I add code to do that it results in an error that I have not specified the parameters?

My code is below, you'll see that eventually I will have 3 nested datalists inside each other - or that's the plan.

Thanks for any suggestions

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="nhscsharprepeater._Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


<%@ Import Namespace="System.Web" %>
<%@ Import Namespace="System.Web.UI" %>
<%@ Import Namespace="System.Data" %>

<script type="text/C#" runat="server">





    protected void DataList1_ItemDataBound(object sender, DataListItemEventArgs e)
    {

        DataList oList = (DataList)e.Item.FindControl("Datalist2");
        Label oLabel = (Label)e.Item.FindControl("lblSection");

        DataList oList2 = (DataList)oList.FindControl("Datalist3");


        SqlDataSource2.SelectParameters.Clear();        
        SqlDataSource2.SelectCommand = "report_DistinctSubSections";
        SqlDataSource2.SelectParameters.Add("Section",oLabel.Text.ToString());



        oList.DataBind();

    }



</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:productfolioConnectionString %>"
            SelectCommand="report_DistinctSection" SelectCommandType="StoredProcedure"></asp:SqlDataSource>
        <asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:productfolioConnectionString %>"
             SelectCommandType="StoredProcedure">

             <SelectParameters>
                <asp:Parameter Name="Section" Type="String" />
             </SelectParameters>

             </asp:SqlDataSource>
        <asp:SqlDataSource ID="SqlDataSource3" runat="server" ConnectionString="<%$ ConnectionStrings:productfolioConnectionString %>"
            SelectCommand="report_getReports" SelectCommandType="StoredProcedure">
            <SelectParameters>
                <asp:Parameter Name="SubSection" Type="String" />
            </SelectParameters>
        </asp:SqlDataSource>
            <asp:DataList ID="DataList1" runat="server" DataSourceID="SqlDataSource1" RepeatColumns="2" RepeatDirection="Horizontal" OnItemDataBound="DataList1_ItemDataBound">
                   <ItemTemplate>

                   <asp:Label Runat="server" text='<%# DataBinder.Eval(Container.DataItem, "Section") %>' ID="lblSection">
                   </asp:Label>

                            <br />

                            <asp:datalist id="Datalist2" runat="server" DataSourceID="SqlDataSource2">        
            <ItemTemplate>

                                       <asp:Label Runat="server" text='<%# DataBinder.Eval(Container.DataItem, "SubSection") %>' ID="lblSection">
                                       </asp:Label>         

                                                <br />

                                                   <asp:datalist id="Datalist3" runat="server" DataSourceID="SqlDataSource3">         
                                    <ItemTemplate>

                                                               <!--<asp:Label Runat="server" text='<%# DataBinder.Eval(Container.DataItem, "Report") %>' ID="lblSection">
                                                               </asp:Label>-->                                                                 

                                    </ItemTemplate>
                                  </asp:datalist>                                                


            </ItemTemplate>
          </asp:datalist>



                </ItemTemplate>
           </asp:DataList> 
    </div>
    </form>
</body>
</html>
From stackoverflow
  • One might guess that calling Clear on SqlDataSource2.SelectParameters before calling Add might do the trick.

0 comments:

Post a Comment