0

I am creating a shopping cart. Users would go to individual pages of products and click "add to cart" button. When that button is clicked, the ProductId is stored in an array list in Session["Cart"]. When they go to Cart.aspx, the repeater will display all the items in the array list. I am not sure how to use the array list with an Entity Model properly.

Here is what I have so far for the code-behind:

if (Session["Cart"] != null)
        {
            using (ProjectEntities myEntities = new ProjectEntities())
            {
                ArrayList alProduct = new ArrayList();
                alProduct = (ArrayList)Session["Cart"];

                var product = (from p in myEntities.Products
                               where p.ProductId == Convert.ToInt32(alProduct)
                               select new { p.ProductName });

                Repeater1.DataSource = product.ToList();
                Repeater1.DataBind();
            }
        }

Here is the markup for Cart.aspx:

<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
    <asp:Repeater ID="Repeater1" runat="server">
        <ItemTemplate>
            <table>
                <tr>
                    <td>
                        <asp:Label ID="lblProductName" runat="server" Text='<%# Eval("ProductName") %>'></asp:Label>
                </tr>
            </table>
        </ItemTemplate>
    </asp:Repeater>
</asp:Content>
Ron T
  • 397
  • 1
  • 4
  • 22
  • Can you try this var product = (from p in myEntities.Products where p.ProductId == Convert.ToInt32(alProduct) select new { p.ProductName }).ToList(); Repeater1.DataSource = product; Repeater1.DataBind(); – Francis Saul Nov 13 '15 at 05:27
  • I tried that but it didn't work. It gave me this error message "LINQ to Entities does not recognize the method 'Int32 ToInt32(System.Object)' method, and this method cannot be translated into a store expression." @Francis Saul – Ron T Nov 13 '15 at 05:36
  • try to create a new arrayList and bind that new arraylist as datasource for the repeater. ArrayList productList = new ArrayList(product.ToList()); – Francis Saul Nov 13 '15 at 05:45
  • Why are you using an `ArrayList`? They're so 10 years ago. Why not use `List`? – Enigmativity Nov 13 '15 at 07:39
  • That was what I learned in class but I'll look into the List @Enigmativity – Ron T Nov 13 '15 at 08:26
  • @Enigmativity what's the difference of ArrayList and List? Care to share your knowledge? thanks – Francis Saul Nov 14 '15 at 14:36
  • @FrancisSaul - An `ArrayList` is not strongly-typed. You can add any type of object and you must cast when accessing items. There is always the danger of getting illegal casts. A `List` is strongly-typed and avoids all of these issues. It's just much easier to write correct programs with `List`. – Enigmativity Nov 15 '15 at 01:23

1 Answers1

0

First problem with your code is here: p.ProductId == Convert.ToInt32(alProduct). Since alProduct is an ArrayList it will never convert into an integer and will throw a run time exception. Change your query like this and use Contains:-

var product = (from p in myEntities.Products
               where alProduct.Contains(p.ProductId)
               select p.ProductName).ToList();

Since you are selecting just one item i.e. ProductName there is no need to project an anonymous type.

Also, there is no need to instantiate the ArrayList before assigning the value from session, although you should check for null before running the product query. I would suggest you to use a generic version of ArrayList i.e. List<int> instead of ArrayList.

Rahul Singh
  • 21,585
  • 6
  • 41
  • 56
  • I'm using ArrayList because I just learn this from class but i will look into the other alternatives. I type that above but it says something is wrong with the last line "Repeater1.DataBind();" and it gives this error message: "Data binding directly to a store query (DbSet, DbQuery, DbSqlQuery, DbRawSqlQuery) is not supported." @Rahul Singh – Ron T Nov 13 '15 at 08:24
  • @RonT - Okay I forgot you are using entity framework, you can apply `ToList()` at the end to convert that to List. Check my update. – Rahul Singh Nov 13 '15 at 08:27
  • I'm still using the ArrayList, would it affect it if I'm still using that since I'm not too sure how to use List yet. I typed in your updated solution, and this time the error is in "var product = (from p in myEntities.Products" with the error message: "LINQ to Entities does not recognize the method 'Boolean Contains(System.Object)' method, and this method cannot be translated into a store expression." @Rahul Singh – Ron T Nov 13 '15 at 08:36
  • @RonT - Okay that means you are using entity framework version < 4. Any cance you can update the version? otherwise you will have to do it using manual expression as specified in this answer - http://stackoverflow.com/a/17940510/1529657 – Rahul Singh Nov 13 '15 at 08:42
  • Oh really? I believe I am using EF version 6, it also says in my web.config @Rahul Singh – Ron T Nov 13 '15 at 08:46
  • @RonT - Nope I don't think so, check this answer on how to check the version - http://stackoverflow.com/questions/3377821/entity-framework-how-can-i-tell-what-version-i-am-using – Rahul Singh Nov 13 '15 at 08:49
  • 1
    I see, the System.Data.Entity.Design, does say Version=4.0.0.0. I'm going to figure out how to update the version. Thanks for all the help this far @Rahul Singh – Ron T Nov 13 '15 at 08:52
  • @RonT - So were you able to figure out what was the issue? – Rahul Singh Nov 17 '15 at 07:02
  • Not yet, I will have to ask my professor tomorrow to see @Rahul Singh I tried to manually type it with the alternative method but I couldn't type "BuildContainsExpression" – Ron T Nov 18 '15 at 09:06