Issues with MyShoppingCart.cs (Partial Class)

May 23, 2010 at 8:03 AM

Hi,

I have a problem that I cant seem to find a solution, any help will be much appreciated. I am at the stage where I have created the MyShoppingCart.cs Class File (Inside the Class folder). I have added the partial keyword and two of the methods so far.  as follows:

 

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

namespace TailspinSpyworks.Classes
{
    public partial class MyShoppingCart
    {
        public const string CartId = "TailSpinSpyWorks_CartID";

        //------------------------------------------------------------------------------------------------------------------------------------------+
        public String GetShoppingCartId()
        {
            if ( HttpContext.Current.Session[CartId] == null)
            {
                HttpContext.Current.Session[CartId] = System.Web.HttpContext.Current.Request.IsAuthenticated ? HttpContext.Current.User.Identity.Name : Guid.NewGuid().ToString();
		(NOTE I ALSO HAD TO CHANGE THE CODE IN RED AS USING ('SESSION' AND 'USER.IDENTITY.NAME') THEM WITHOUT HTTPCONTEXT NAMESPACE WAS CAUSING BUILD ERRORS)
            }
            return HttpContext.Current.Session[CartId].ToString();
        }
        //------------------------------------------------------------------------------------------------------------------------------------------+
        public void AddItem(string cartID, int productID, int quantity)
        {
            using (CommerceEntities db = new CommerceEntities())
            {
                try
                {
                    var myItem = (from c in db.ShoppingCarts where c.CartID == cartID && c.ProductID == productID select c).FirstOrDefault();
                    if (myItem == null)
                    {
                        ShoppingCart cartadd = new ShoppingCart();
                        cartadd.CartID = cartID;
                        cartadd.Quantity = quantity;
                        cartadd.ProductID = productID;
                        cartadd.DateCreated = DateTime.Now;
                        db.ShoppingCarts.AddObject(cartadd);
                    }
                    else
                    {
                        myItem.Quantity += quantity;
                    }

                    db.SaveChanges();
                }
                catch (Exception exp)
                {
                    throw new Exception("ERROR: Unable to Add Item to Cart - " + exp.Message.ToString(), exp);
                }
            }

        }
    }
}

The problems comes when I try and call these two methods from my AddToCart.aspx.cs. I am getting the same error:
1.'TailspinSpyworks.MyShoppingCart' does not contain a definition for 'GetShoppingCartId' and no extension method 'GetShoppingCartId' accepting a first argument of type 'TailspinSpyworks.MyShoppingCart' could be found (are you missing a using directive or an assembly reference?) 

2.TailspinSpyworks.MyShoppingCart' does not contain a definition for 'AddItem' and no extension method 'AddItem' accepting a first argument of type 'TailspinSpyworks.MyShoppingCart' could be found (are you missing a using directive or an assembly reference?)


Here's the code for my AddToCart.aspx.cs :

namespace TailspinSpyworks
{
    public partial class AddToCart : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string rawId = Request.QueryString["ProductID"];
            int productId;
            if (!String.IsNullOrEmpty(rawId) && Int32.TryParse(rawId, out productId))
            {
                MyShoppingCart usersShoppingCart = new MyShoppingCart();
                String cartId = usersShoppingCart.GetShoppingCartId();
                usersShoppingCart.AddItem(cartId, productId, 1);
            }
            else
            {
                Debug.Fail("ERROR : We should never get to AddToCart.aspx without a ProductId.");
                throw new Exception("ERROR : It is illegal to load AddToCart.aspx without setting a ProductId.");
            }
            Response.Redirect("MyShoppingCart.aspx");

        }
    }
}


Thanks in advance.


 

 

May 23, 2010 at 3:59 PM

Ok  Guys, Never mind..I just copied the MyShoppincart.cs File from the sample application and everything seems to be fine now. I guess there must have been something wrong with mine. Not quite sure what but it appears to be resolved now.

Jul 29, 2010 at 9:10 AM
I don't think that your problem has been solved! In this Application, MyShoppingCarts.cs is builded as a partical class of MyShoppingCarts.aspx, so your namespace should be changed. It should be same with your MyShoppingCarts.aspx. Thus,Session can be recognized becouse it is a property of the Page Class.