Originariamente inviato da Leonardo M.
A occhio dipende da dove e come dichiari Items.
probabilmente l'oggetto items è static o qualcosa del genere insomma è condiviso a livello applicazione mnetre dobvrebbe essere messo in sessione.
in quel pezzo di codice non c'è nulla che non va.
Ecco i 3 fiel che uso:
CartItem.cs
Codice PHP:
using System;
/**
* The CartItem Class
*
* Basically a structure for holding item data
*/
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; }
public int scot { 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 NomeCompleto {
get { return Prod.NomeCompleto; }
}
public string Codice
{
get { return Prod.Codice; }
}
public decimal UnitPrice
{
get { return Prod.Price; }
}
public decimal prezzoconIVA
{
get { return Prod.piuIVA; }
}
public decimal TotalPrice {
get { return prezzoconIVA * 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;
}
}
Product.cs
Codice PHP:
/**
* The Product class
*
* This is just to simulate some way of accessing data about our products
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.OleDb;
public class Product
{
public int Id { get; set; }
public decimal Price { get; set; }
public decimal piuIVA { get; set; }
public string NomeCompleto { get; set; }
public string Codice { get; set; }
OleDbConnection Accesdb = new OleDbConnection();
string constring = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|BDPSW.mdb;Persist Security Info=True";
public Product(int id)
{
this.Id = id;
Accesdb.ConnectionString = constring;
OleDbCommand comando = new OleDbCommand();
comando.Connection = Accesdb;
comando.CommandText = "SELECT * FROM SIS_LISTINO_WEB WHERE IdProdotto = " + id + "";
try
{
Accesdb.Open();
OleDbDataReader esecuzione = comando.ExecuteReader();
esecuzione.Read();
string a = esecuzione["PrezzoDef"].ToString();
this.Price = Convert.ToDecimal(esecuzione["PrezzoDef"].ToString());
this.NomeCompleto = esecuzione["Denominazione"].ToString();
this.Codice = esecuzione["Codice"].ToString();
}
catch
{
}
finally
{
Accesdb.Close();
CalcolaIva(id);
}
}
public int prendiIVA(int iva)
{
string a = "";
Accesdb.ConnectionString = constring;
OleDbCommand comando = new OleDbCommand();
comando.Connection = Accesdb;
comando.CommandText = "SELECT CodiceIva FROM SIS_PRODOTTI_IVA WHERE IdProdotto = " + iva;
try
{
Accesdb.Open();
OleDbDataReader esecuzione = comando.ExecuteReader();
while (esecuzione.Read())
{
a = esecuzione["CodiceIva"].ToString();
}
}
catch
{
}
finally
{
Accesdb.Close();
}
comando.CommandText = "SELECT Aliquota FROM TEC_IVA WHERE Codice = '" + a + "'";
try
{
Accesdb.Open();
OleDbDataReader esecuzione = comando.ExecuteReader();
while (esecuzione.Read())
{
a = esecuzione["Aliquota"].ToString();
}
}
catch
{
}
finally
{
Accesdb.Close();
}
return Convert.ToInt32(a);
}
public void CalcolaIva(int id)
{
int percentualeIVA = prendiIVA(id);
decimal iva = this.Price + (( this.Price * percentualeIVA) / 100 );
this.piuIVA = Convert.ToDecimal(String.Format("{0:N2}", iva));
}
}
Shoppingcart.cs
Codice PHP:
using System.Collections.Generic;
using System.Web;
using System;
using System.Linq;
using System.Web.UI;
using System.Web.UI.WebControls;
/**
* The ShoppingCart class
*
* Holds the items that are in the cart and provides methods for their manipulation
*/
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 productId, int scot)
{
// Create a new item to add to the cart
CartItem newItem = new CartItem(productId);
// 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.scot = scot;
item.Quantity++;
return;
}
}
} else {
newItem.Quantity = 1;
newItem.scot = scot;
Items.Add(newItem);
}
}
/**
* SetItemQuantity() - Changes the quantity of an item in the cart
*/
public void SetItemQuantity(int productId, int quantity) {
// If we are setting the quantity to 0, remove the item entirely
if (quantity == 0) {
RemoveItem(productId);
return;
}
// Find the item and update the quantity
CartItem updatedItem = new CartItem(productId);
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 productId) {
CartItem removedItem = new CartItem(productId);
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
}
Se vuoi posto il codice lato client che richiama quelle classi, ma non credo serva. Sta tutto i quei 3 file