My Shopping list datagridview not show data

  • Thread starter Thread starter FcoyClau
  • Start date Start date
F

FcoyClau

Guest
Hi

I try to build a shopping list, based in this tutorial.

Build a Shopping Cart in ASP.NET

For this i have a product, ShoppingCart and CartItem classes. When i test this build, this show everything ok between the classes but my problem begin when i change the product page to Shopping cart page. This not show data.

My Product Class

using System;
using System.Data;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Configuration;
namespace mrosite
{
public class Product
{
public int PCode { get; set; }
public string PartNumber { get; set; }
public string Description { get; set; }
public int Available { get; set; }
public decimal Price { get; set; }


public Product(int pcode)
{
this.PCode = pcode;
/* Begin Searh the product code into the database */
string SqlStr = "SELECT [PCode], [PartNumber], [Description], [PStock], [Price] FROM Products WHERE PCode=@PCode";
DataSet SqlDs = new DataSet();
SqlCommand SqlCmd = new SqlCommand(SqlStr);
String connStr = ConfigurationManager.ConnectionStrings["TOOLCRIB"].ConnectionString;
SqlConnection SqlConn = new SqlConnection(connStr);
SqlDataAdapter SqlAdap = new SqlDataAdapter();
SqlCmd.CommandType = CommandType.Text;
SqlCmd.Parameters.AddWithValue("@PCode", PCode);
SqlCmd.Connection = SqlConn;
SqlAdap.SelectCommand = SqlCmd;
SqlAdap.Fill(SqlDs, "Prod");
SqlConn.Close();

this.PCode= Convert.ToInt32(SqlDs.Tables["Prod"].Rows[0][0]);
this.PartNumber = SqlDs.Tables["Prod"].Rows[0][1].ToString();
this.Description = SqlDs.Tables["Prod"].Rows[0][2].ToString();
this.Available = Convert.ToInt32(SqlDs.Tables["Prod"].Rows[0][3]);
this.Price = Convert.ToDecimal(SqlDs.Tables["Prod"].Rows[0][4]);

}
}
}


My ShoppingCart class

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

namespace mrosite
{
/// <summary>
/// Summary description for ShoppingCart
/// </summary>
public class ShoppingCart
{
#region Properties

public List<CartItem> Items { get; private set; }

#endregion

#region Singleton Implementation

// Readonly properties can only be set in initialization or in a constructor
public static readonly ShoppingCart Instance;

// The static constructor is called as soon as the class is loaded into memory
static ShoppingCart()
{
// If the cart is not in the session, create one and put it there
// Otherwise, get it from the session
if (HttpContext.Current.Session["ASPNETShoppingCart"] == null)
{
Instance = new ShoppingCart();
Instance.Items = new List<CartItem>();
HttpContext.Current.Session["ASPNETShoppingCart"] = Instance;
}
else
{
Instance = (ShoppingCart)HttpContext.Current.Session["ASPNETShoppingCart"];
}
}

// A protected constructor ensures that an object can't be created from outside
protected ShoppingCart() { }

#endregion

#region Item Modification Methods
/**
* AddItem() - Adds an item to the shopping
*/
public void AddItem(int pCode)
{
// Create a new item to add to the cart
CartItem newItem = new CartItem(pCode);

// If this item already exists in our list of items, increase the quantity
// Otherwise, add the new item to the list
if (Items.Contains(newItem))
{
foreach (CartItem item in Items)
{
if (item.Equals(newItem))
{
item.Quantity++;
return;
}
}
}
else
{
newItem.Quantity = 1;
Items.Add(newItem);
}
}

/**
* SetItemQuantity() - Changes the quantity of an item in the cart
*/
public void SetItemQuantity(int pCode, int quantity)
{
// If we are setting the quantity to 0, remove the item entirely
if (quantity == 0)
{
RemoveItem(pCode);
return;
}

// Find the item and update the quantity
CartItem updatedItem = new CartItem(pCode);

foreach (CartItem item in Items)
{
if (item.Equals(updatedItem))
{
item.Quantity = quantity;
return;
}
}
}

/**
* RemoveItem() - Removes an item from the shopping cart
*/
public void RemoveItem(int pCode)
{
CartItem removedItem = new CartItem(pCode);
Items.Remove(removedItem);
}
#endregion

#region Reporting Methods
/**
* GetSubTotal() - returns the total price of all of the items
* before tax, shipping, etc.
*/
public decimal GetSubTotal()
{
decimal subTotal = 0;
foreach (CartItem item in Items)
subTotal += item.TotalPrice;

return subTotal;
}
#endregion
}
}

my CartItem class

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
/**
* The CartItem Class
*
* Basically a structure for holding item data
*/
namespace mrosite
{
public class CartItem : IEquatable<CartItem>
{
#region Properties

// A place to store the quantity in the cart
// This property has an implicit getter and setter.
public int Quantity { get; set; }

private int _productId;
public int ProductId
{
get { return _productId; }
set {
// To ensure that the Prod object will be re-created
_product = null;
_productId = value;
}
}

private Product _product = null;
public Product Prod
{
get {
// Lazy initialization - the object won't be created until it is needed
if (_product == null) {
_product = new Product(ProductId);
}
return _product;
}
}
public string PartNumber
{
get { return Prod.PartNumber; }
}

public string Description {
get { return Prod.Description; }
}

public int Available
{
get { return Prod.Available; }
}

public decimal UnitPrice {
get { return Prod.Price; }
}

public decimal TotalPrice {
get { return UnitPrice * Quantity; }
}

#endregion

// CartItem constructor just needs a productId
public CartItem(int productId) {
this.ProductId = productId;
}

/**
* Equals() - Needed to implement the IEquatable interface
* Tests whether or not this item is equal to the parameter
* This method is called by the Contains() method in the List class
* We used this Contains() method in the ShoppingCart AddItem() method
*/
public bool Equals(CartItem item) {
return item.ProductId == this.ProductId;
}
}
}

Here the item has been added in the shopping cart1403710.png

This is how i send the data to the shopping list form from products page

protected void GrdBrowse_RowCommand1(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "SendCart")
{
//Determine the RowIndex of the Row whose Button was clicked.
int index = Convert.ToInt32(e.CommandArgument);

//Reference the GridView Row.
GridViewRow row = GrdBrowse.Rows[index];

//Fetch value of Product Code
int myId = Convert.ToInt32(row.Cells[1].Text);

//Set value for add to shopping cart
Product Prod = new Product(Convert.ToInt32(row.Cells[1].Text));

ShoppingCart.Instance.AddItem(Prod.PCode);
//----------------------------------------------------------------------
Response.Redirect("ViewCart.aspx");
}
The final result





And thanks for your help.

Continue reading...
 
Back
Top