Issue with AddToCart.aspx.cs

Jul 24, 2012 at 7:20 PM
Edited Jul 24, 2012 at 7:22 PM

I am working on a project that incorporates elements of the Tailspin Spyworks application into it. For the most part, I am following the tutorial exactly. The only things I've changed are certain file paths ("/products/store/addtocart.aspx" instead of "addtocart.aspx"), the project name, and the master page.

In the page "AddToCart.aspx.cs", when I entered in the following code, it gave me the error:

"The type or namespace name "MyShoppingCart" could not be found (Are you missing a using directive or an assembly reference?)"

I am on the newer side of C# programming, but have done web development for a number of years. Any help would be greatly appreciated.

This is the entire code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Diagnostics;

namespace JumpStitch
{
    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("/products/store/MyShoppingCart.aspx");
        }
    }
}
Jul 25, 2012 at 12:04 PM

Hello Max, it should not be a problem unless you kept the inheritence path as is in the .aspx file, it needs the correct path for the class file .cs, I am sure that you can see that it couldn't find the class. check it and let me know. The code you presented seems fine.

Jul 27, 2012 at 8:20 PM

That did indeed fix the problem, but now the GetShoppingCartId and AddItem bits are flashing the same error.

Jul 30, 2012 at 5:18 PM

hello again Max, I'm kinda a little busy fixing my car, sorry for the belated reply. I have made a VB version of Joe's C#, so if you liked you can download my version which is fully functional, many have used it. But it is good that you fixed the problem, I will need to look at your class code and check it out before I can give you my opinion regarding the new error codes.

Aug 1, 2012 at 2:38 AM

I've fixed that issue, but, once again, have another. Basically, if I go to a product page (ProductDetails.aspx), and then click "add to cart", it passes to the MyShoppingCart.aspx page, but doesn't display anything in an actual "Shopping Cart", let alone display a table.

I think it is an issue with the AddToCart.aspx.cs page. Here is the code for it:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Diagnostics;

namespace JumpStitch
{
    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))
            {
                JumpStitch.Classes.MyShoppingCart usersShoppingCart = new JumpStitch.Classes.MyShoppingCart();
                String cartId = usersShoppingCart.GetShoppingCartId();
                usersShoppingCart.AddItem(cartId, productId, 1);
            }
            else
            {
                Debug.Fail("ERROR: We should never get to AddToCart.aspx without a product Id.");
                throw new Exception("ERROR: It is illegal to load AddToCart.aspx without setting a Product Id.");
            }
            Response.Redirect("MyShoppingCart.aspx");
        }
    }
}

 

Aug 3, 2012 at 7:34 AM
Edited Aug 3, 2012 at 7:37 AM

Ok Max,try this

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Diagnostics;

namespace JumpStitch
{
public partial class AddToCart : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (Request.Parms["productId"] != null)
{
JumpStitch.Classes.MyShoppingCart usersShoppingCart = new JumpStitch.Classes.MyShoppingCart();
String cartId = usersShoppingCart.GetShoppingCartId();
usersShoppingCart.AddItem(cartId, Int32.Parse(Request.Parms["productId"]) , 1);
}
else
{
Debug.Fail("ERROR: We should never get to AddToCart.aspx without a product Id.");
throw new Exception("ERROR: It is illegal to load AddToCart.aspx without setting a Product Id.");
}
Response.Redirect("MyShoppingCart.aspx");
}
}
}