Order and OrderDetails Information (Need Help)

Jan 15, 2011 at 6:04 PM

Hi, 

First of all, thx for the best tutorial for creating shopping cart experience. I followed everything and managed 

to finish everything without any errors. 

I still have one question. When you submit order it is written in Order table. But if I was a person who is taking care

of the orders in imaginary company that uses this online shop, I can't see the details of the order in the Order Details table.

Only when i submit order with just one product then I can see details of that order in Order_Details table.

But when I submit order with more than one product, I can't see anything in Order_Details table.

Is there some solution to that problem, because someone couldn't ship order if he can't see what products have been ordered. 

 

I need your help, it's urgent.... 

 

Thanks...

Jan 17, 2011 at 6:32 AM

I ran the app again here on my end and everything shows up fine, one product or more shows up in order details.

Maybe you should check your code.

 

 

Jan 17, 2011 at 1:12 PM
Edited Jan 17, 2011 at 2:11 PM

Ok, thanks for your reply...

I've checked my code and everything seems to be ok...

I've been following video instructions http://www.asp.net/aspnet-4/videos and there is one thing different 

from this e-book.

 

In the ebook the code is:

 

 foreach (View_ShoppingCart item in mojaKosarica)
                    {
                          
                            int i = 0;
                            if (i < 1)
                            {
                                NarudzbaDetalji nd = new NarudzbaDetalji();
                                nd.Narudzba_ID = novaNarudzba.Narudzba_ID;
                                nd.Proizvod_ID = item.Proizvod_ID;
                                nd.Kolicina = item.Kolicina;
                                nd.Cijena = item.Cijena;
                                db.NarudzbaDetaljis.AddObject(nd);
                                i++;
                            }

                            var myItem = (from c in db.ShoppingCarts where c.Cart_ID == item.Cart_ID && c.Proizvod_ID == item.Proizvod_ID select c).FirstOrDefault();
                            if (myItem != null)
                            {
                                db.DeleteObject(myItem);
                            }

                    }

                    db.SaveChanges();

And in the video is a bit different...

Could you please send me SubmitOrder piece of code so I can compare, and maybe see what's the problem...
Thanks in advance...

P.S
some of the variables are on my language (Croatian), i don't think that should be any problems...

 

Feb 4, 2011 at 10:39 PM

Hello dlazendi

Sorry about the belated response, I just don't visit the site much because of other things I'm doing. 

Anyway, I looked at your submitted code and I do not see the necessity for declaring the variable i or for doubling up on the loop.  The foreach statement in itself is a well ordered loop.

Get rid of the "i" variable and the if statement after it and don't forget omitting the "i++" too. then run it again.

Your code should look like this and it should do the job

foreach (View_ShoppingCart item in mojaKosarica)
{

NarudzbaDetalji nd = new NarudzbaDetalji();
nd.Narudzba_ID = novaNarudzba.Narudzba_ID;
nd.Proizvod_ID = item.Proizvod_ID;
nd.Kolicina = item.Kolicina;
nd.Cijena = item.Cijena;
db.NarudzbaDetaljis.AddObject(nd);


var myItem = (from c in db.ShoppingCarts where c.Cart_ID == item.Cart_ID && c.Proizvod_ID == item.Proizvod_ID select c).FirstOrDefault();
if (myItem != null)
{
db.DeleteObject(myItem);
}

}

db.SaveChanges();

Let me know if there's anything else,,, Croatian huh? it was interesting to read but no problem.
Feb 5, 2011 at 1:14 PM

Hi, 

I've tried but still the same problem... 

The code you wrote is the same as in the tailspinspyworks video... But I'm still having the same problem... Don't where to look for the problem except in the MyShoppingCart.cs

Thx anyway...

Feb 5, 2011 at 1:20 PM

Hey, 

I've changed the code but it still doesn't work.

The code you posted is the same as in the tailspinspyworks video tutorials. I just don't get it, where is the problem... I've checked MyShoppingCart.cs and don't know where else to look for the problem....

Thx anyway...

 

Feb 6, 2011 at 5:27 PM

Ok man,

why don't you post your the whole code from MyShoppingCart class file here and let me look at it.

Otherwise here is the submit order section from mine.

 

 public bool SubmitOrder(string UserName)
        {
            using (CommerceEntities db = new CommerceEntities())
            {
                try
                {
                    Order newOrder = new Order();
                    newOrder.CustomerName = UserName;
                    newOrder.OrderDate = DateTime.UtcNow;
                    newOrder.ShipDate = CalculateShippingDate();
                    db.Orders.AddObject(newOrder);
                    db.SaveChanges();

                    // for multiple users and a bigger site use a more transitional save

                  //  int NewOrderKey = (from p in db.Orders select p.OrderID).Max();
                    String CartId = GetShoppingCartId();
                    var myCart = (from c in db.ViewCarts where c.CartID == CartId select c);

                    foreach (ViewCart item in myCart)
                    {
                        OrderDetail  od = new OrderDetail();
                        od.OrderID = newOrder.OrderID;
                        od.ProductID = item.ProductID;
                        od.Quantity = item.Quantity;
                        od.UnitCost = item.UnitCost;
                      // error here with object
                        db.OrderDetails.AddObject(od);
                  
                    var myItem = (from c in db.ShoppingCarts where c.CartID == item.CartID && c.ProductID == item.ProductID select c).FirstOrDefault();
                        if (myItem != null)
                        {
                            db.DeleteObject(myItem);
                        }  
                    }
                    db.SaveChanges();
                }
                catch (Exception exp)
                {
                    // exception handler
                    throw new Exception("Error: Unable to Migrate Cart - " + exp.Message.ToString(), exp);
                }
            }
            return (true);
        }

 

U'r welcome.

Feb 6, 2011 at 8:04 PM

Hi, 

here is MyShoppingCart.cs code:

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using OnlineShop.Data_Access;

namespace OnlineShop
{

    public struct ShoppingCartUpdates
    {
        public int ProductId;
        public int PurchaseQuantity;
        public bool RemoveItem;
    }

    public partial class MyShoppingCart
    {
        public const string CartId = "OnlineShop_CartID";

        public String GetShoppingCartId()
        {
            System.Web.HttpContext context = System.Web.HttpContext.Current;
            if (Session[CartId] == null)
            {
                if (context.User.Identity.Name != "")
                {
                    Session[CartId] = context.User.Identity.Name;
                }
                else
                {
                    Guid tempCartId = Guid.NewGuid();
                    Session[CartId] = tempCartId.ToString();
                }
            }
            
            return Session[CartId].ToString();
        }


        public decimal GetTotal(string cartID)
        {
            using (OnlineShopEntities db = new OnlineShopEntities())
            {
                decimal cartTotal = 0;
                try
                {
                    var myCart = (from c in db.View_ShoppingCart where c.Cart_ID == cartID select c);
                    if (myCart.Count() > 0)
                    {
                        cartTotal = myCart.Sum(od => (decimal)od.Kolicina * (decimal)od.Cijena);
                    }
                }
                catch (Exception exp)
                {
                    throw new Exception("Error: Nastala je greska u racunanju" + exp.Message.ToString(), exp);
                }
                return (cartTotal);
            }
        }


        public void AddItem(string cartID, int ProductID, int quantity)
        {
            using (OnlineShopEntities db = new OnlineShopEntities())
            {
                try
                {
                    var myItem = (from c in db.ShoppingCarts where c.Cart_ID == cartID && c.Proizvod_ID == ProductID select c).FirstOrDefault();
                    if (myItem == null)
                    {
                        ShoppingCart cartadd = new ShoppingCart();
                        cartadd.Cart_ID = cartID;
                        cartadd.Proizvod_ID = ProductID;
                        cartadd.Kolicina = quantity;
                        cartadd.Datum_Kreirano = DateTime.Now;
                        db.ShoppingCarts.AddObject(cartadd);
                    }
                    else
                    {
                        myItem.Kolicina += quantity;
                    }
                    db.SaveChanges();

                }
                catch (Exception exp)
                {
                    // TOD: Implement Exception Handling.
                }
            }
        }

        public void UpdateShoppingCartDatabase(String cartId, ShoppingCartUpdates[] CartItemUpdates)
        {
            try
            {
                using (OnlineShopEntities db = new OnlineShopEntities())
                {
                    int CartItemCount = CartItemUpdates.Count();
                    var myCart = (from c in db.View_ShoppingCart where c.Cart_ID == cartId select c);
                    foreach (var cartItem in myCart)
                    {
                        for (int i = 0; i < CartItemCount; i++)
                        {
                            if (cartItem.Proizvod_ID == CartItemUpdates[i].ProductId)
                            {
                                if (CartItemUpdates[i].PurchaseQuantity < 1 || CartItemUpdates[i].RemoveItem == true)
                                {
                                    RemoveItem(cartId, cartItem.Proizvod_ID);
                                }
                                else
                                {
                                    UpdateItem(cartId, cartItem.Proizvod_ID, CartItemUpdates[i].PurchaseQuantity);
                                }
                            }
                        }
                    }
                }
            }
            catch (Exception exp)
            {
                // TODO: Implementirati exception
            }
        }

        public void RemoveItem(string cartID, int productID)
        {
            using (OnlineShopEntities db = new OnlineShopEntities())
            {
                try
                {
                    var myItem = (from c in db.ShoppingCarts where c.Cart_ID == cartID && c.Proizvod_ID == productID select c).FirstOrDefault();
                    if (myItem != null)
                    {
                        db.DeleteObject(myItem);
                        db.SaveChanges();
                    }
                }
                catch (Exception exp)
                {
                    // TODO : Implement exception
                }
            }
        }

        public void UpdateItem(string cartID, int productID, int quantity)
        {
            using (OnlineShopEntities db = new OnlineShopEntities())
            {
                try
                {
                    var myItem = (from c in db.ShoppingCarts where c.Cart_ID == cartID && c.Proizvod_ID == productID select c).FirstOrDefault();
                    if (myItem != null)
                    {
                        myItem.Kolicina = quantity;
                        db.SaveChanges();
                    }
                }
                catch (Exception exp)
                {
                    // TODO : Implement exception
                }
            }
        }

        public void MigrateCart(String oldCartId, String UserName)
        {
            using (OnlineShopEntities db = new OnlineShopEntities())
            {
                try
                {
                    var myShopingCart = from cart in db.ShoppingCarts
                                         where cart.Cart_ID == oldCartId
                                         select cart;
                    foreach (ShoppingCart item in myShopingCart)
                    {
                        item.Cart_ID = UserName;
                    }
                    db.SaveChanges();
                    Session[CartId] = UserName;
                }
                catch (Exception exp)
                {
                    // TODO Exception
                }
            }
        }

        public bool SubmitOrder(string UserName)
        {
            using (OnlineShopEntities db = new OnlineShopEntities())
            {
                try
                {
                    Narudzbe novaNarudzba = new Narudzbe();
                    novaNarudzba.Korisnik_Ime = UserName;
                    novaNarudzba.Datum_Narudzbe = DateTime.Now;
                    novaNarudzba.Datum_Isporuke = CalculateShipDate();
                    db.Narudzbes.AddObject(novaNarudzba);
                    db.SaveChanges();

                    int NewOrderKey = (from p in db.Narudzbes select p.Narudzba_ID).Max();
                    String cartId = GetShoppingCartId();
                    var mojaKosarica = (from c in db.View_ShoppingCart where c.Cart_ID == cartId select c);

                    foreach (View_ShoppingCart item in mojaKosarica)
                    {                    
                            
                                NarudzbaDetalji nd = new NarudzbaDetalji();
                                nd.Narudzba_ID = novaNarudzba.Narudzba_ID;
                                nd.Proizvod_ID = item.Proizvod_ID;
                                nd.Kolicina = item.Kolicina;
                                nd.Cijena = item.Cijena;
                                db.NarudzbaDetaljis.AddObject(nd);
                               
                            
                            var myItem = (from c in db.ShoppingCarts where c.Cart_ID == item.Cart_ID && c.Proizvod_ID == item.Proizvod_ID select c).FirstOrDefault();
                            if (myItem != null)
                            {
                                db.DeleteObject(myItem);
                            }

                    }

                    db.SaveChanges();
                }
                catch (Exception exp)
                {
                    //TODO : excepton handling
                }
            }


            return (true);
        }

        DateTime CalculateShipDate()
        {
            DateTime shipDate = DateTime.Now.AddDays(3);
            return (shipDate);
        }

    }
}

Feb 7, 2011 at 5:13 PM

Hello Zendi;

Well beside all that language barriers I think I have managed to clean up your code and hopefully it will work for you, but you do have to remember once you change names in the entity database, views, methods and classes and even in your aspx and the aspx.cs files, you really have to keep them in check, and I am sure that you are very well aware that C# (well all C and Java variables) are case sensitive.

Anyway, try this one and if it didn't work then then do write the codes for the exceptions and ten see what kind of errors you are getting, then you might want to try debugging the code step by step using the break points method, finally if all this did not work, you could either upload the whole project so I can check it myself, or re-write the code to suite your language with out using the originals definitions.

 

Good luck.

 

here is the cs file code copy it and paste it on yours.

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using OnlineShop.Data_Access;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;

namespace OnlineShop
{

    public struct ShoppingCartUpdates
    {
        public int ProductId;
        public int PurchaseQuantity;
        public bool RemoveItem;
    }

    public partial class MyShoppingCart
    {
        public const string CartId = "OnlineShop_CartID";

        public String GetShoppingCartId()
        {
            System.Web.HttpContext context = System.Web.HttpContext.Current;
            if (Session[CartId] == null)
            {
                if (context.User.Identity.Name != "")
                {
                    Session[CartId] = context.User.Identity.Name;
                }
                else
                {
                    Guid tempCartId = Guid.NewGuid();
                    Session[CartId] = tempCartId.ToString();
                }
            }
           
            return Session[CartId].ToString();
        }


        public decimal GetTotal(string CartID)
        {
            using (OnlineShopEntities db = new OnlineShopEntities())
            {
                decimal CartTotal = 0;
                try
                {
                    var myCart = (from c in db.View_ShoppingCart where c.Cart_ID == CartID select c);
                    if (myCart.Count() > 0)
                    {
                        CartTotal = myCart.Sum(od => (decimal)od.Kolicina * (decimal)od.Cijena);
                    }
                }
                catch (Exception exp)
                {
                    throw new Exception("Error: Nastala je greska u racunanju" + exp.Message.ToString(), exp);
                }
                return (CartTotal);
            }
        }


        public void AddItem(string CartID, int ProductID, int quantity)
        {
            using (OnlineShopEntities db = new OnlineShopEntities())
            {
                try
                {
                    var myItem = (from c in db.ShoppingCarts where c.Cart_ID == CartID && c.Proizvod_ID == ProductID select

c).FirstOrDefault();
                    if (myItem == null)
                    {
                        ShoppingCart cartadd = new ShoppingCart();
                        cartadd.Cart_ID = cartID;
                        cartadd.Proizvod_ID = ProductID;
                        cartadd.Kolicina = quantity;
                        cartadd.Datum_Kreirano = DateTime.Now;
                        db.ShoppingCarts.AddObject(cartadd);
                    }
                    else
                    {
                        myItem.Kolicina += quantity;
                    }
                    db.SaveChanges();

                }
                catch (Exception exp)
                {
                    // TOD: Implement Exception Handling.
                }
            }
        }

        public void UpdateShoppingCartDatabase(String CartId, ShoppingCartUpdates[] CartItemUpdates)
        {
    using (OnlineShopEntities db = new OnlineShopEntities())
           
            {
               try
                {
                    int CartItemCount = CartItemUpdates.Count();
                    var myCart = (from c in db.View_ShoppingCart where c.Cart_ID == CartId select c);
                    foreach (var CartItem in myCart)
                    {
                        for (int i = 0; i < CartItemCount; i++)
                        {
                            if (CartItem.Proizvod_ID == CartItemUpdates[i].ProductId)
                            {
                                if (CartItemUpdates[i].PurchaseQuantity < 1 || CartItemUpdates[i].RemoveItem == true)
                                {
                                    RemoveItem(CartId, CartItem.Proizvod_ID);
                                }
                                else
                                {
                                    UpdateItem(CartId, CartItem.Proizvod_ID, CartItemUpdates[i].PurchaseQuantity);
                                }
                            }
                        }
                    }
                }
            }
            catch (Exception exp)
            {
                // TODO: Implementirati exception
            }
        }

        public void RemoveItem(string CartID, int productID)
        {
            using (OnlineShopEntities db = new OnlineShopEntities())
            {
                try
                {
                    var myItem = (from c in db.ShoppingCarts where c.Cart_ID == CartID && c.Proizvod_ID == productID select

c).FirstOrDefault();
                    if (myItem != null)
                    {
                        db.DeleteObject(myItem);
                        db.SaveChanges();
                    }
                }
                catch (Exception exp)
                {
                    // TODO : Implement exception
                }
            }
        }

        public void UpdateItem(string CartID, int productID, int quantity)
        {
            using (OnlineShopEntities db = new OnlineShopEntities())
            {
                try
                {
                    var myItem = (from c in db.ShoppingCarts where c.Cart_ID == CartID && c.Proizvod_ID == productID select

c).FirstOrDefault();
                    if (myItem != null)
                    {
                        myItem.Kolicina = quantity;
                        db.SaveChanges();
                    }
                }
                catch (Exception exp)
                {
                    // TODO : Implement exception
                }
            }
        }

        public void MigrateCart(String oldCartId, String UserName)
        {
            using (OnlineShopEntities db = new OnlineShopEntities())
            {
                try
                {
                    var myShopingCart = (from cart in db.ShoppingCarts
                                         where cart.Cart_ID == oldCartId
                                         select cart);
                    foreach (ShoppingCart item in myShopingCart)
                    {
                      item.CartID = UserName;
                        db.SaveChanges();
                        Session[CartId] = UserName;
                    }
                   
                }
                catch (Exception exp)
                {
                    // TODO Exception
                }
            }
        }

        public bool SubmitOrder(string UserName)
        {
            using (OnlineShopEntities db = new OnlineShopEntities())
            {
                try
                {
                    Narudzbe novaNarudzba = new Narudzbe();
                    novaNarudzba.Korisnik_Ime = UserName;
                    novaNarudzba.Datum_Narudzbe = DateTime.Now;
                    novaNarudzba.Datum_Isporuke = CalculateShipDate();
                    db.Narudzbes.AddObject(novaNarudzba);
                    db.SaveChanges();

                   // int NewOrderKey = (from p in db.Narudzbes select p.Narudzba_ID).Max();
                    String CartId = GetShoppingCartId();
                    var mojaKosarica = (from c in db.View_ShoppingCart where c.Cart_ID == CartId select c);

                    foreach (View_ShoppingCart item in mojaKosarica)
                    {                   
                           
                                NarudzbaDetalji nd = new NarudzbaDetalji();
                                nd.Narudzba_ID = novaNarudzba.Narudzba_ID;
                                nd.Proizvod_ID = item.Proizvod_ID;
                                nd.Kolicina = item.Kolicina;
                                nd.Cijena = item.Cijena;
                                db.NarudzbaDetaljis.AddObject(nd);
                              
                           
                            var myItem = (from c in db.ShoppingCarts where c.Cart_ID == item.Cart_ID && c.Proizvod_ID ==

item.Proizvod_ID select c).FirstOrDefault();
                            if (myItem != null)
                            {
                                db.DeleteObject(myItem);
                            }

                    }

                    db.SaveChanges();
                }
                catch (Exception exp)
                {
                    //TODO : excepton handling
                }
            }


            return (true);
        }

        DateTime CalculateShipDate()
        {
            DateTime shipDate = DateTime.Now.AddDays(3);
            return (shipDate);
        }

    }
}

Feb 7, 2011 at 6:24 PM
Edited Feb 7, 2011 at 6:25 PM

Hi, 

I've tried pasting your code but everything is the same...

I never got any errors when I ran the app "Start Debugging (F5)" , everything seems to be fine...

The thing that was weird when I made this app a month ago, if I put just one product and submit order, it would show up in the OrderDetails table, but when there

were more than one product it wouldn't show anything in OrderDetails table.(Croatian: NarudzbaDetalji).

I really can't figure it out, did everything 2-3 times from scratch.

I'm really sorry all your time lost in this but if you want I can e-mail you whole project in RAR... If you find something it would be great , if not it's fine...

I'm not expert in ASP.net and C# this is for my school project and maybe you can see something wrong better than me...

Feb 7, 2011 at 10:31 PM

ya sure, I have some time to waste, since I am on a little vacation and no where to go. just send it as a zip file to my email.