Search code examples
asp.netnested-datalist

nested datalist


I had nested datalist I had two datalist the first for getting gategories and the second subcategory for category I did my code well and first data list get the categories well but the second datalist didnot get any data and no error found so please any one help me.

ASP

 <div>
    <asp:DataList ID="dlCategory" runat="server">
        <EditItemStyle ForeColor="#CC3300" />
        <AlternatingItemStyle ForeColor="#CC3300" />
        <ItemStyle ForeColor="#CC3300" />
        <SelectedItemStyle ForeColor="#CC3300" />
        <HeaderTemplate>
            <div class="buttn_hed_red">
                &nbsp;</div>
            <div class="buttn_hed_bg">
                <div class="lm7 tm1 buttn_hed_txt">
                    Projectors</div>
            </div>
        </HeaderTemplate>
        <ItemTemplate>
            <div class="buttn_div">
                <div class="buttn_red_sqr">
                    &nbsp;</div>
                <div class="lm5 tm2 buttn_txt">
                    <a href='<%#Eval("ID","Category.aspx?ID={0}") %>' class="buttn_txt">
                        <asp:Label ID="LblCat" runat="server" Text='<%#DataBinder.Eval(Container.DataItem,"Category") %>'></asp:Label>
                    </a>
                </div>
            </div>
            <asp:DataList ID="dlSubCategory" runat="server" DataSource='<%# GetSubByCategory(Convert.ToString(Eval("ID")))%>'>
                <EditItemStyle ForeColor="#CC3300" />
                <AlternatingItemStyle ForeColor="#CC3300" />
                <SelectedItemStyle ForeColor="#CC3300" />
                <ItemTemplate>
                    <div class="buttn_div_sub">
                        <div class="lm40 tm2 buttn_txt">

                            <asp:Label ID="Label1" runat="server" Text='<%#DataBinder.Eval(Container.DataItem,"SubCategory") %>'></asp:Label>

                        </div>
                    </div>
                </ItemTemplate>
            </asp:DataList>
        </ItemTemplate>
    </asp:DataList>
</div>

CS CODE

public  DataTable   GetSubByCategory(string   ID)
{
    DataTable dt = new DataTable();

    cls.GetSubCategory(ID);
    return dt;

}

METHOD

public DataTable GetSubCategory(string   Category_Id)
{
    using
    (SqlConnection conn = Connection.GetConnection())
    {
        SqlCommand cmd = new SqlCommand();
        cmd.Connection = conn;
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.CommandText = "SP_GetParentByCategoryID";
        SqlParameter ParentID_Param = cmd.Parameters.Add("@CategoryID", SqlDbType.Int);
        ParentID_Param.Value = Category_Id;
        DataTable dt = new DataTable();
        SqlDataAdapter da = new SqlDataAdapter();
        da.SelectCommand = cmd;
        da.Fill(dt);
        return dt;
    }
}

PROC

ALTER proc [dbo].[SP_GetParentByCategoryID]
(
@CategoryID int
)

as
select  Cat2.[Name] as "SubCategory" ,Cat2.ParentID
from Categories Cat1
  inner join  Categories Cat2
ON Cat1.ID=Cat2.ParentID
where Cat2.ParentID=@CategoryID

Solution

  • I think you are just returning a blank DataTable.

    Try

    public DataTable GetSubByCategory(string ID)
    {
        return cls.GetSubCategory(ID);
    }