Embedded data using stored procedure parameters

I am working with some nested datalist controls and cant get the SP parameters for my nested stored procedure to work.

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

I'm guessing that I need to somehow clear the parameters every time the nested datalist is bound, but if I add code to do this, it throws an error that I didn't specify the parameters?

My code is below, you will see that I end up with 3 nested datalists inside each other - or whatever 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>

      

+1


source to share


1 answer


One might assume that calling Clear

in SqlDataSource2.SelectParameters

before calling Add

might do the trick.



+1


source







All Articles