Wrox Programmer Forums
|
BOOK: Beginning ASP.NET 4 : in C# and VB
This is the forum to discuss the Wrox book Beginning ASP.NET 4: in C# and VB by Imar Spaanjaars; ISBN: 9780470502211
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning ASP.NET 4 : in C# and VB section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old August 16th, 2011, 04:34 AM
Authorized User
 
Join Date: Jul 2011
Posts: 13
Thanks: 7
Thanked 0 Times in 0 Posts
Default Linq and repeater not working

Hi Imar,

I receive an error message in the browser for my work. The work is sismilar to the try it out on your book at page 493.

The error message I am receiving says: DataBinding: does not contain a property with the name "Products"

Here is the table I am using

table name : Product
fields name: ProductId, ProductName, Category, Price
---------------------------------
code on the Repeater.aspx page:
Code:
<%@ Page Title="" Language="VB" MasterPageFile="~/MasterPages/ProdCustomer.master" AutoEventWireup="false" CodeFile="Repeater.aspx.vb" Inherits="CUS_Products_Repeater" %>

<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="cpMainContent" Runat="Server">
    <asp:Repeater ID="Repeater1" runat="server">
    <ItemTemplate>
        <h3>
            <asp:BulletedList ID="ProductList" runat="server" 
            DataSource='<%#Eval("Products")%>' 
            DataTextField="ProductName" 
            DisplayMode="Text">
            </asp:BulletedList>
        <asp:Literal ID="Literal1" runat="server" >
        
        </asp:Literal>
        </h3>
    </ItemTemplate>
    </asp:Repeater>
</asp:Content>
---------------------------
Code in vb page:

Code:
Imports ProductsModel
Imports System.Data

Partial Class CUS_Products_Repeater
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Using myEntities As New ProductsEntities1()
            Dim allProduct = From Product In myEntities.Products
                           Order By Product.Price
                           Select New With {Product.Price, Product.ProductName}
            Repeater1.DataSource = allProduct
            Repeater1.DataBind()
        End Using
    End Sub
End Class
-------------------

I checked the forum for an answer i found one, but I am new to asp.net so I couldn`t understand the answer in there.

Linq: DataBinding: 'System.Char' does not contain a property with the name 'X'.

I will be thankfull if you may help me to find where is my mistake.

Thank you
Oruc

Last edited by oruckenan; October 10th, 2011 at 03:24 PM.. Reason: wrap code inside code tag
 
Old August 16th, 2011, 05:48 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Hi there,

The example in the book you used for your implementation was designed to display hierarchical data. You could use that example if you also had a Category table and wanted to display a list of all categories, with another list of products that belong to each category. E.g.:

Code:
 
Category 1
  Product 1 
  Product 2
  Product 3
Category 2
  Product 4 
  Product 5
  Product 6
For this to work you query needs to be hierarchical as well. Something like

SELECT category.Name, category.Products

In your case, you seem to be selecting a flat list (just the product's price and name) so you can't use a nested control (the BulletedList) to display your data.
Instead, you can simply use Literals or other controls to output the price and name in the Repeater directly. Something like this:

Code:
 
<asp:Repeater ID="Repeater1" runat="server">
<ItemTemplate>
<asp:Literal ID="Literal1" runat="server" Text='<%#Eval("ProductName")%>' />
-
<asp:Literal ID="Literal2" runat="server" Text='<%#Eval("Price")%>' />
</ItemTemplate>
</asp:Repeater>
Hope this helps,

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
The Following User Says Thank You to Imar For This Useful Post:
oruckenan (August 16th, 2011)
 
Old August 16th, 2011, 06:59 AM
Authorized User
 
Join Date: Jul 2011
Posts: 13
Thanks: 7
Thanked 0 Times in 0 Posts
Default

Hi Imar,

Thank you for the answer. I learned and realised somethings This helped me to understand somethings in the exercise at page 494 a little bit more. As I am trying to copy the logic of this exercise to my work so let me give you more details:

I have another table named "Category" linked with one to many links to table "Product"

Here is the description of the table:

Quote:
Table name: Category
Fields name:
Category - nvarchar(50) - primary key field
CategoryId - int - is identity field
Note: The tables Category and Product are connected by one to many relationship.
The fields connected are Category (Category) - Category (Product)

Also here is the description of the Product field

Quote:
Table name: Product

Fields name:
ProductId - int - primary key field
ProductName - nvarchar(150)
Category - nvarchar(50)
maybe this can help you to correct my mistake.

Thank you again, I am looking forward for your answer.

Oruc

Last edited by oruckenan; October 10th, 2011 at 03:26 PM.. Reason: wrap some text inside quote tag
 
Old August 16th, 2011, 07:03 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Quote:
maybe this can help you to correct my mistake.
I guess that depends on what you're trying to accomplish. What's the final goal for this page?

If it's just to learn how this works, you can take the book's example and substitute Genre for Category and Review for Product. You would then be able to display a list of categories with their associated products in a BulletedList.

Otherwise, it would help to understand what you would like to display on the screen.

Cheers,

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
The Following User Says Thank You to Imar For This Useful Post:
oruckenan (August 16th, 2011)
 
Old August 16th, 2011, 07:43 AM
Authorized User
 
Join Date: Jul 2011
Posts: 13
Thanks: 7
Thanked 0 Times in 0 Posts
Default

Hi Imar,

Thank you for the answer.
The truth is when I did the exercise page 493 on the book I was not successfull on getting the page without errors, and then after many attempts I skipped the exercise.

Now I am trying to make this work with my own database, so the objective is to make it work with my own database because as the fields are in my mind I may memorize and understand better. Just with practice I think I can understand things better.
By being successfull in this exercise I may complexify the code. For now I am trying to fully understand this exercise.

So now I changed the code according your last posting:
--------------------
Code:
Imports ProductsModel
Imports System.Data

Partial Class CUS_Products_Repeater
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Using myEntities As New ProductsEntities1()
            Dim allProduct = From Category In myEntities.Categories
                           Order By Category.Id
                           Select New With {Category.Category1, Category.Products}
            Repeater1.DataSource = allProduct
            Repeater1.DataBind()
        End Using
    End Sub
End Class
--------------

This code is working but apparently it just give me the category table content, and doesn`t give me the product table content inside of each category group.

I am trying to make as same as your work but I am not getting the same result on the screen. Please help.



Thank you.

Last edited by oruckenan; October 10th, 2011 at 03:28 PM.. Reason: wrap code inside code tag
 
Old August 16th, 2011, 07:55 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Are you sure the relationships between the Category and Product table are set up correctly? You have an identity field in Category, yet the Category column is the primary key? Instead of your current schema, I'd recommend something like this:

Table name: Category
Fields name:
CategoryId - int - is identity field, primary key field
Name - nvarchar(50)

Table name: Product
Fields name:
ProductId - int - is identity field, primary key field
ProductName - nvarchar(150)
CategoryId - int

You then create a relationship between Category.CategoryId and Product.CategoryId

While your current setup might work, it feels odd to have an auto number field (the identity) in Category but use the name as the primary key.

Another thing to check: are the products really related to your categories? E.g. what does the Category column in the Product table contain? Do these values link back to the Category table?

Two other things:

1. Try debugging your code. Put a breakpoint on:

Repeater1.DataSource = allProduct

Hit F5 and then look at the allProduct variable. You can expand it to see what it contains

2. What's the code for the Repeater you're currently using? Maybe it all works fine and you do get products but you're simply not displaying them?

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
The Following User Says Thank You to Imar For This Useful Post:
oruckenan (August 16th, 2011)
 
Old August 16th, 2011, 05:05 PM
Authorized User
 
Join Date: Jul 2011
Posts: 13
Thanks: 7
Thanked 0 Times in 0 Posts
Default

Dear Imar,

I noticed that with the code I get the productname list from products table and that they are separated with an empty space for each group that belongs to different categories. Strangely the category name doesn`t display on the page, I checked the source from the browser in case it is due to the css color formating of the text, but the category name was no even in the source.

So I changed from repeater.aspx the "product" to "category" and then it was giving me for each of product just the category name repeatingly for each. This means if I have for example 4 product in mt product table, then the category "food" was displaying 4 times.
So now I don`t have any mistake but the problem is I don`t have the group category name displaying for products.
I am doing something wrong or missing something in the code but I don`t know what.

the code in repeater.aspx is as follows:

---------------------------------
Code:
<%@ Page Title="" Language="VB" MasterPageFile="~/MasterPages/ProdCustomer.master" AutoEventWireup="false" CodeFile="Repeater.aspx.vb" Inherits="CUS_Products_Repeater" %>

<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="cpMainContent" Runat="Server">
<asp:Repeater ID="Repeater1" runat="server">
<ItemTemplate>
<h3>
<asp:BulletedList ID="ProductList" runat="server" 
DataSource='<%#Eval("Products")%>' 
DataTextField="ProductName" 
DisplayMode="Text">
</asp:BulletedList>
<asp:Literal ID="Literal1" runat="server" >

</asp:Literal>
</h3>
</ItemTemplate>
</asp:Repeater>
</asp:Content>
---------------------------

please continue helping me

thank you
Oruc

Last edited by oruckenan; October 10th, 2011 at 03:30 PM.. Reason: wrap code inside code tag
 
Old August 16th, 2011, 06:20 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Code:
Strangely the category name doesn`t display on the page
That's not so strange, as you're not outputting its name. You're only outputting the products using a nested BulletedList control.

Above the BulletedList, but still in the ItemTemplate add something like this:

<asp:Literal ID="Literal1" runat="server" Text='<%#Eval("Category1")%>' />

where Category1 refers to the name property of the Category.

When you post code, can you wrap it in Code tags using the code (#) button on the toolbar? You need to copy and paste it to Notepad first to remove color coding. This way the code becomes a lot easier to read.

Cheers,

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
The Following User Says Thank You to Imar For This Useful Post:
oruckenan (August 17th, 2011)
 
Old August 17th, 2011, 03:29 AM
Authorized User
 
Join Date: Jul 2011
Posts: 13
Thanks: 7
Thanked 0 Times in 0 Posts
Default Thank you finally resolved

You are a Star Imar!
This was missing part in the code. It was written in the book very clearly but and I just didn`t see it!
Now all is fine.
I also made the changes as you described about the primary key, all is much better now.
Thank you so much!

Best Wishes
Oruc





Similar Threads
Thread Thread Starter Forum Replies Last Post
New error in working LINQ code chroniclemaster1 ASP.NET 4 General Discussion 0 June 3rd, 2011 02:17 PM
Question about LINQ maratin BOOK: ASP.NET MVC Website Programming Problem Design Solution ISBN: 9780470410950 3 January 24th, 2010 10:49 AM
LINQ vaibhavgoe LINQ 4 December 21st, 2009 03:49 PM
Need your Help for LINQ urgently embarus BOOK: Beginning Microsoft Visual C# 2008 ISBN: 978-0-470-19135-4 0 January 16th, 2009 12:13 AM
Local COM working , but not working at Web Serv nagen111 .NET Web Services 3 February 19th, 2005 04:22 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.