cannot do add to cart function
-
first, i'm sorry i'm newbie here and newbie in aspnet. i learn some code from ebook but it's not working. and i'm really sorry too because my english is bad. when i debug my project its working fine, and i try to add item to my shopping cart its not show any error but the button do not work, its ike the product page only reload. i hope i can find the answer here. thanks for help. here the code CartAccess.cs
using System;
using System.Web;
using System.Data;
using System.Data.Common;/// <summary>
/// Supports Shopping Cart functionality
/// </summary>
public class CartAccess
{
public CartAccess()
{
//
// TODO: Add constructor logic here
//
}// returns the shopping cart ID for the current user private static string shoppingCartId { get { HttpContext context = HttpContext.Current; string cartId; { // check if the cart ID exists as a cookie if (context.Request.Cookies\["Delta\_CartID"\] != null) { // return the id //return context.Request.Cookies\["Delta\_CartID"\].Value; cartId = context.Request.Cookies\["Delta\_CartID"\].Value; return cartId; } else // if the cart ID doesn't exist in the cookie as well, generate a new ID { // generate a new GUID //Guid tempGuid = Guid.NewGuid(); //context.Response.Cookies\["Delta\_CartID"\].Value = tempGuid.ToString(); cartId = Guid.NewGuid().ToString(); // create the cookie object and set its value HttpCookie cookie = new HttpCookie("Delta\_CartID", cartId); // set the cookie's expiration int howManyDays = deltaconfig.CartExpireDays; DateTime currentDate = DateTime.Now; TimeSpan timeSpan = new TimeSpan(howManyDays, 0, 0, 0); DateTime expirationDate = currentDate.Add(timeSpan); cookie.Expires = expirationDate; // set the cookie on the client's browser context.Response.Cookies.Add(cookie); // return the CartID return cartId.ToString(); } } } } // Add a new s
-
first, i'm sorry i'm newbie here and newbie in aspnet. i learn some code from ebook but it's not working. and i'm really sorry too because my english is bad. when i debug my project its working fine, and i try to add item to my shopping cart its not show any error but the button do not work, its ike the product page only reload. i hope i can find the answer here. thanks for help. here the code CartAccess.cs
using System;
using System.Web;
using System.Data;
using System.Data.Common;/// <summary>
/// Supports Shopping Cart functionality
/// </summary>
public class CartAccess
{
public CartAccess()
{
//
// TODO: Add constructor logic here
//
}// returns the shopping cart ID for the current user private static string shoppingCartId { get { HttpContext context = HttpContext.Current; string cartId; { // check if the cart ID exists as a cookie if (context.Request.Cookies\["Delta\_CartID"\] != null) { // return the id //return context.Request.Cookies\["Delta\_CartID"\].Value; cartId = context.Request.Cookies\["Delta\_CartID"\].Value; return cartId; } else // if the cart ID doesn't exist in the cookie as well, generate a new ID { // generate a new GUID //Guid tempGuid = Guid.NewGuid(); //context.Response.Cookies\["Delta\_CartID"\].Value = tempGuid.ToString(); cartId = Guid.NewGuid().ToString(); // create the cookie object and set its value HttpCookie cookie = new HttpCookie("Delta\_CartID", cartId); // set the cookie's expiration int howManyDays = deltaconfig.CartExpireDays; DateTime currentDate = DateTime.Now; TimeSpan timeSpan = new TimeSpan(howManyDays, 0, 0, 0); DateTime expirationDate = currentDate.Add(timeSpan); cookie.Expires = expirationDate; // set the cookie on the client's browser context.Response.Cookies.Add(cookie); // return the CartID return cartId.ToString(); } } } } // Add a new s
Having all of the methods of the cart as static is a bad idea. You are also throwing away the return values. If CartAccess.AddItem returns a bool you should be doing something with it. It could be as simple as setting the text of a label with "added to cart" or "oops something bad happened". I am guessing you would be getting a false and that it has something to do with your database connection. To get detailed info about what went wrong, either remove the try/catch around the DataAccess calls, or in the catch, change it to catch(Exception ex) and set a label with ex.Message. Your English is not bad at all.
-
Having all of the methods of the cart as static is a bad idea. You are also throwing away the return values. If CartAccess.AddItem returns a bool you should be doing something with it. It could be as simple as setting the text of a label with "added to cart" or "oops something bad happened". I am guessing you would be getting a false and that it has something to do with your database connection. To get detailed info about what went wrong, either remove the try/catch around the DataAccess calls, or in the catch, change it to catch(Exception ex) and set a label with ex.Message. Your English is not bad at all.