Click here to Skip to main content
15,906,624 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
List<Category> add = modle1.GetCatgorymenu();

Here how to add the relation please check i have commented it
    //add.Relations.Add(new DataRelation("CategoriesRelation", add.Tables[0].Columns["CategoryID"],
    //    add.Tables[1].Columns["CategoryID"]));
    outerDataList.DataSource = add;

    outerDataList.DataBind();
}


 protected void outerDataList_ItemDataBound(object sender, DataListItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item)
            {

                DataRowView drv = e.Item.DataItem as DataRowView;

                DataList innerDataList = e.Item.FindControl("innerDataList") as DataList;

                innerDataList.DataSource = drv.CreateChildView("CategoriesRelation");

                innerDataList.DataBind();

            }
        }


This is the properties

C#
public class Category : OnlineBookBase
   {
       private Decimal? _CategoryId;

       public Decimal? CategoryId
       {
           get { return _CategoryId; }
           set { _CategoryId = value; }
       }
       private String _category;

       public String category
       {
           get { return _category; }
           set { _category = value; }
       }
       private String _Description;

       public String Description
       {
           get { return _Description; }
           set { _Description = value; }
       }
       public Category(SqlDataReader reader)
       {
           if (string.IsNullOrEmpty(reader["category_id"].ToString()))
           {
               _CategoryId = null;
           }
           else
           {
               _CategoryId = Decimal.Parse(reader["category_id"].ToString());
           }
           _category = reader["category"].ToString();
           _Description = reader["description"].ToString();
       }
       public Category()
       {

       }
   }
   public class SubCategory : OnlineBookBase
   {

       private Category _cate = new Category();

       public Category cate
       {
           get { return _cate; }
           set { _cate = value; }
       }


       private Decimal? _SubCategoryID;

       public Decimal? SubCategoryID
       {
           get { return _SubCategoryID; }
           set { _SubCategoryID = value; }
       }
       private Decimal? _CategoryId;

       public Decimal? CategoryId
       {
           get { return _CategoryId; }
           set { _CategoryId = value; }
       }
       private String _SCategory;

       public String SCategory
       {
           get { return _SCategory; }
           set { _SCategory = value; }
       }
       private String _SubCategoryDescription;

       public String SubCategoryDescription
       {
           get { return _SubCategoryDescription; }
           set { _SubCategoryDescription = value; }
       }
       public SubCategory(SqlDataReader reader)
       {
           if (string.IsNullOrEmpty(reader["category_id"].ToString()))
           {
               _CategoryId = null;
           }
           else
           {
               _CategoryId = Decimal.Parse(reader["category_id"].ToString());
           }
           if (string.IsNullOrEmpty(reader["sub_category_id"].ToString()))
           {
               _SubCategoryID = null;
           }
           else
           {
               _SubCategoryID = Decimal.Parse(reader["sub_category_id"].ToString());
           }
          // _cate.category = reader["category"].ToString();
           _SCategory = reader["sub_category"].ToString();
           _SubCategoryDescription = reader["sub_category_desc"].ToString();
       }
       public SubCategory()
       {

       }

   }



and the procedure

SQL
ALTER PROCEDURE [dbo].[usp_GetProductsForCategories]

AS

SELECT * FROM Category WHERE category_id IN

( SELECT category_id FROM SubCategory )

SELECT p.sub_category_id , p.sub_category ,p.sub_category_id FROM dbo.SubCategory p



ASPX

XML
<asp:DataList ID="outerDataList" runat="server"
                     OnItemDataBound="outerDataList_ItemDataBound">

                <ItemTemplate>

                    <li>

                        <asp:Label Font-Size="Large" Font-Bold="true" ID="lblCategoryName" runat="server"

                            Text='<%# Eval("category") %>' />

                    </li>

                    <ul>

                        <asp:DataList ID="innerDataList" runat="server">

                            <ItemTemplate>

                                <li style="background-color:AliceBlue">

                                    <asp:HyperLink ID="hlProductName" runat="server" Text='<%# Eval("SCategory")%>' />

                                </li>

                            </ItemTemplate>

                        </asp:DataList>

                    </ul>

                </ItemTemplate>

            </asp:DataList>




the error is
Error 3 'System.Collections.Generic.List<onlinebookstore.data.entities.category>' does not contain a definition for 'Relations' and no extension method 'Relations' accepting a first argument of type 'System.Collections.Generic.List<onlinebookstore.data.entities.category>' could be found (are you missing a using directive or an assembly reference?) E:\Projects\Online books\codes\Online book\Online book\User\Boooksdisp.aspx.cs 60 17 Online book
Posted
Updated 26-Aug-13 22:33pm
v4

1 solution

That's what the error message says: add is a generic List that holds elements of type Category.
In your first commented code line, you're accessing add's property Relations. But since add is a list, it doesn't have a property of that name.

1) "add" is a very very very bad name for a variable. Better use nouns for variable names, verbs for method names. Especially since "add" is a verb you're going to use frequently in conjuction with generic lists.

2) Since I don't know anything about the Category class, I presume that it has properties "Relations" and "Tables". Given that, you could access them this way:
C#
add[correctIndex].Relations.Add(new DataRelation());
But that's merely guesswork. You should use the "Improve question" link above and provide some more insight into the classes you use.
List<t>[^] belongs to the framework. We have MSDN for details.
The interesting parts are "Category", "CategoriesRelation" and maybe the types of "Relations", "Tables" and "Columns".
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900