Click here to Skip to main content
15,888,610 members
Home / Discussions / ASP.NET
   

ASP.NET

 
QuestionShowing error message Pin
future38391-Nov-10 15:43
future38391-Nov-10 15:43 
AnswerRe: Showing error message Pin
benams1-Nov-10 20:23
benams1-Nov-10 20:23 
AnswerRe: Showing error message Pin
RaviRanjanKr1-Nov-10 20:39
professionalRaviRanjanKr1-Nov-10 20:39 
AnswerRe: Showing error message Pin
Brij1-Nov-10 20:44
mentorBrij1-Nov-10 20:44 
GeneralRe: Showing error message Pin
RaviRanjanKr1-Nov-10 20:53
professionalRaviRanjanKr1-Nov-10 20:53 
GeneralRe: Showing error message Pin
Brij2-Nov-10 2:36
mentorBrij2-Nov-10 2:36 
GeneralRe: Showing error message Pin
RaviRanjanKr2-Nov-10 3:06
professionalRaviRanjanKr2-Nov-10 3:06 
Questioncannot do add to cart function Pin
setengah1-Nov-10 14:53
setengah1-Nov-10 14:53 
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 shopping cart item
    public static bool AddItem(string bookId)
    {
        // get a configured DbCommand object
        DbCommand comm = DataAccess.CreateCommand();
        // set the stored procedure name
        comm.CommandText = "AddItem";
        // create a new parameter
        DbParameter param = comm.CreateParameter();
        param.ParameterName = "@CartID";
        param.Value = shoppingCartId;
        param.DbType = DbType.String;
        param.Size = 36;
        comm.Parameters.Add(param);
        // create a new parameter
        param = comm.CreateParameter();
        param.ParameterName = "@BookID";
        param.Value = bookId;
        param.DbType = DbType.Int32;
        comm.Parameters.Add(param);
        // returns true in case of success or false in case of an error
        try
        {
            // execute the stored procedure and return true if it executes
            // successfully, or false otherwise
            return (DataAccess.ExecuteNonQuery(comm) != -1);
        }
        catch
        {
            // prevent the exception from propagating, but return false to
            // signal the error
            return false;
        }
    }

    // Update the quantity of a shopping cart item
    public static bool UpdateItem(string bookId, int cquantity)
    {
        // get a configured DbCommand object
        DbCommand comm = DataAccess.CreateCommand();
        // set the stored procedure name
        comm.CommandText = "UpdateItem";
        // create a new parameter
        DbParameter param = comm.CreateParameter();
        param.ParameterName = "@CartID";
        param.Value = shoppingCartId;
        param.DbType = DbType.String;
        param.Size = 36;
        comm.Parameters.Add(param);
        // create a new parameter
        param = comm.CreateParameter();
        param.ParameterName = "@BookID";
        param.Value = bookId;
        param.DbType = DbType.Int32;
        comm.Parameters.Add(param);
        // create a new parameter
        param = comm.CreateParameter();
        param.ParameterName = "@CQuantity";
        param.Value = cquantity;
        param.DbType = DbType.Int32;
        comm.Parameters.Add(param);
        // returns true in case of success or false in case of an error
        try
        {
            // execute the stored procedure and return true if it executes
            // successfully, or false otherwise
            return (DataAccess.ExecuteNonQuery(comm) != -1);
        }
        catch
        {
            // prevent the exception from propagating, but return false to
            // signal the error
            return false;
        }
    }

    // Remove a shopping cart item
    public static bool RemoveItem(string bookId)
    {
        // get a configured DbCommand object
        DbCommand comm = DataAccess.CreateCommand();
        // set the stored procedure name
        comm.CommandText = "RemoveItem";
        // create a new parameter
        DbParameter param = comm.CreateParameter();
        param.ParameterName = "@CartID";
        param.Value = shoppingCartId;
        param.DbType = DbType.String;
        param.Size = 36;
        comm.Parameters.Add(param);
        // create a new parameter
        param = comm.CreateParameter();
        param.ParameterName = "@BookID";
        param.Value = bookId;
        param.DbType = DbType.Int32;
        comm.Parameters.Add(param);
        // returns true in case of success or false in case of an error
        try
        {

            // execute the stored procedure and return true if it executes
            // successfully, or false otherwise
            return (DataAccess.ExecuteNonQuery(comm) != -1);
        }
        catch
        {
            // prevent the exception from propagating, but return false to
            // signal the error
            return false;
        }
    }

    // Retrieve shopping cart items
    public static DataTable GetItem()
    {
        // get a configured DbCommand object
        DbCommand comm = DataAccess.CreateCommand();
        // set the stored procedure name
        comm.CommandText = "GetItem";
        // create a new parameter
        DbParameter param = comm.CreateParameter();
        param.ParameterName = "@CartID";
        param.Value = shoppingCartId;
        param.DbType = DbType.String;
        param.Size = 36;
        comm.Parameters.Add(param);
        // return the result table
        DataTable table = DataAccess.ExecuteSelectCommand(comm);
        return table;
    }

    // Retrieve shopping cart items
    public static decimal GetTotalAmount()
    {
        // get a configured DbCommand object
        DbCommand comm = DataAccess.CreateCommand();
        // set the stored procedure name
        comm.CommandText = "GetTotalAmount";
        // create a new parameter
        DbParameter param = comm.CreateParameter();
        param.ParameterName = "@CartID";
        param.Value = shoppingCartId;
        param.DbType = DbType.String;
        param.Size = 36;
        comm.Parameters.Add(param);
        // return the result table
        return Decimal.Parse(DataAccess.ExecuteScalar(comm));
    }

    // Counts old shopping carts
    public static int CountOldCarts(byte days)
    {
        // get a configured DbCommand object
        DbCommand comm = DataAccess.CreateCommand();
        // set the stored procedure name
        comm.CommandText = "CountOldCarts";
        // create a new parameter
        DbParameter param = comm.CreateParameter();
        param.ParameterName = "@Days";
        param.Value = days;
        param.DbType = DbType.Byte;
        comm.Parameters.Add(param);

        // execute the procedure and return number of old shopping carts
        try
        {
            return Byte.Parse(DataAccess.ExecuteScalar(comm));
        }
        catch
        {
            return -1;
        }
    }

    // Deletes old shopping carts
    public static bool DeleteOldCarts(byte days)
    {
        // get a configured DbCommand object
        DbCommand comm = DataAccess.CreateCommand();
        // set the stored procedure name
        comm.CommandText = "DeleteOldCarts";
        // create a new parameter
        DbParameter param = comm.CreateParameter();
        param.ParameterName = "@Days";
        param.Value = days;
        param.DbType = DbType.Byte;
        comm.Parameters.Add(param);
        // execute the procedure and return true if no problem occurs
        try
        {
            DataAccess.ExecuteNonQuery(comm);
            return true;
        }
        catch
        {
            return false;
        }
    }

}


ShoppingCart.aspx.cs
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class ShoppingCart : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Populate();
        }
    }
    // fill shopping cart controls with data
    private void Populate()
    {
        // get the items in the shopping cart
        DataTable dt = CartAccess.GetItem();
        // if the shopping cart is empty...
        if (dt.Rows.Count == 0)
        {
            gridview.Visible = true;
            Updatebtn.Enabled = false;
            TotalAmount.Text = String.Format("{0:c}", 0);
        }
        else
        // if the shopping cart is not empty...
        {
            // populate the list with the shopping cart contents
            gridview.DataSource = dt;
            gridview.DataBind();
            gridview.Visible = true;
            Updatebtn.Enabled = true;
            // display the total amount
            decimal amount = CartAccess.GetTotalAmount();
            TotalAmount.Text = String.Format("{0:c}", amount);
        }
    }
    protected void continuebtn_Click(object sender, EventArgs e)
    {
        Response.Redirect("/");
    }
}


using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class Product : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
            // Retrieve BookID from the query string
            string bookId = Request.QueryString["BookID"];
            // Retrieves product details
            BookDetails bd = CatalogAccess.GetBookDetails(bookId);
            // Does the product exist?
            if (bd.Name != null)
            {
                Populate(bd);
            }
            else
            {
                Server.Transfer("ProductNotfound.aspx");
            }
    }
    // Fill the control with data
    private void Populate(BookDetails bd)
    {
        
        // Display product details
        titleLabel.Text = bd.Name;
        descriptionLabel.Text = bd.Description;
        stockLabel.Text = Convert.ToString(bd.Quantity);
        priceLabel.Text = String.Format("{0:c}", bd.Price);
        authorLabel.Text = bd.Author;
        productImage.ImageUrl = "BookImage/" + bd.Image;
        // Set the title of the page
        this.Title = deltaconfig.SiteName + bd.Name;
    }
    protected void AddToCartButton_Click(object sender, EventArgs e)
    {
        // Retrieve ProductID from the query string
        string bookId = Request.QueryString["BookID"];

        CartAccess.AddItem(bookId);
    }
}

and thanks again
AnswerRe: cannot do add to cart function Pin
T M Gray2-Nov-10 4:33
T M Gray2-Nov-10 4:33 
GeneralRe: cannot do add to cart function Pin
Fayu9-Nov-10 4:44
Fayu9-Nov-10 4:44 
QuestionDataset or ado.net Pin
future38391-Nov-10 3:35
future38391-Nov-10 3:35 
AnswerRe: Dataset or ado.net Pin
Not Active1-Nov-10 4:38
mentorNot Active1-Nov-10 4:38 
Questionhelp with url rewrite... Pin
swornavidhya_m31-Oct-10 23:09
swornavidhya_m31-Oct-10 23:09 
QuestionAjax with VS 2005 Pin
AndieDu31-Oct-10 20:51
AndieDu31-Oct-10 20:51 
QuestionHyper Link in datagrid to download a file Pin
<<Tash18>>31-Oct-10 20:38
<<Tash18>>31-Oct-10 20:38 
AnswerRe: Hyper Link in datagrid to download a file Pin
Not Active1-Nov-10 2:14
mentorNot Active1-Nov-10 2:14 
GeneralRe: Hyper Link in datagrid to download a file Pin
<<Tash18>>1-Nov-10 19:00
<<Tash18>>1-Nov-10 19:00 
QuestionCopy Multiple File on Client Machine Pin
raushan_931-Oct-10 20:00
raushan_931-Oct-10 20:00 
QuestionASP.net VB Pin
Xinxen31-Oct-10 19:50
Xinxen31-Oct-10 19:50 
Questionbest way to save news? Pin
Jassim Rahma31-Oct-10 4:40
Jassim Rahma31-Oct-10 4:40 
AnswerRe: best way to save news? Pin
Brij31-Oct-10 4:53
mentorBrij31-Oct-10 4:53 
Questionlosing the focud!! Pin
Jassim Rahma31-Oct-10 4:36
Jassim Rahma31-Oct-10 4:36 
QuestionHow do I integrate my asp.net website with other websites...(similar to Facebook Connect)? Pin
Nada Adel31-Oct-10 1:45
Nada Adel31-Oct-10 1:45 
Question500-inernal server error . Pin
Davood Riazi30-Oct-10 22:33
Davood Riazi30-Oct-10 22:33 
AnswerRe: 500-inernal server error . Pin
Keith Barrow31-Oct-10 4:37
professionalKeith Barrow31-Oct-10 4:37 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.