Quantcast
Channel: CSharp Forum Latest Questions
Viewing all articles
Browse latest Browse all 32059

Adding Item From GridView To Shopping Cart Page

$
0
0
 

 

 

I have modified my code where the data for the GridView is loaded when the page loads.

Go to www.jeremyconrad2.net/pizza.aspx to view the page.

As you can see the Totally Awesome Pizza page shows the pizza.  These items are listed using a GridView and the data is loaded on page_load using the following code:

usingSystem.Web;
usingSystem.Web.UI;
usingSystem.Web.UI.WebControls;
usingSystem.Configuration;
usingSystem.Data.SqlClient;
usingSystem.Data;

publicpartialclass pizza :System.Web.UI.Page
{
   
SqlDataAdapter adapter =newSqlDataAdapter();
   
DataSet dtset =newDataSet();

   
protectedvoidPage_Load(object sender,EventArgs e)
   
{
       
if(!IsPostBack)
       
{
           
stringDatabaseConnection=ConfigurationManager.ConnectionStrings["dbconnect"].ToString();

           
SqlConnection connect =newSqlConnection(DatabaseConnection);

            connect
.Open();

           
SqlCommand myCommand =newSqlCommand("SELECT product.p_description, product.p_ingredient, inventory.inv_size, inventory.inv_price FROM inventory INNER JOIN product ON inventory.product_id = product.product_id WHERE (product.p_description = 'Totally Awesome Pizza')", connect);

            adapter
.SelectCommand= myCommand;
            adapter
.Fill(dtset,"ProductTable");
           
GridView1.DataSource= dtset.Tables["ProductTable"];
           
GridView1.DataBind();
           
            connect
.Close();
       
}
   
}
}

 



 

I want to add an "Add" button but I also want the add button to add the pizza to the shopping cart.

How do I use the add button to add the corresponding GridView Row to the shopping car?

 

I have three classes; item.cs, ShoppingItem.cs, and ShoppingCart.cs.

 

Here is the code for item.cs:

I want to add an "Add" button but I also want the add button to add the pizza to the shopping cart.

How do I use the add button to add the corresponding GridView Row to the shopping car?

 

I have three classes; item.cs, ShoppingItem.cs, and ShoppingCart.cs.

 

Here is the code for item.cs:


 

usingSystem;
usingSystem.Collections.Generic;
usingSystem.Linq;
usingSystem.Web;
usingSystem.Configuration;
usingSystem.Data.SqlClient;
usingSystem.Data;

/// <summary>
/// Summary description for item
/// </summary>
publicclass item
{
   
publicstringDescription{get;set;}
   
publicstringIngredients{get;set;}
   
publicstringSize{get;set;}
   
publicdecimalPrice{get;set;}
   
publicstringPrices{get;set;}

   
public item(intProductId)
   
{
       
stringDatabaseConnection=ConfigurationManager.ConnectionStrings["dbconnect"].ToString();

       
SqlConnection connect =newSqlConnection(DatabaseConnection);

        connect
.Open();

       
SqlCommand myCommand =newSqlCommand("SELECT product.p_description AS Description, inventory.inv_size AS Size, inventory.inv_price AS Price FROM inventory INNER JOIN product ON inventory.product_id = product.product_id WHERE (inventory.inv_id = @add_id)", connect);
        myCommand
.Parameters.AddWithValue("@add_id",SqlDbType.Int).Value=ProductId;

       
SqlDataReader reader = myCommand.ExecuteReader();

        reader
.Read();

       
this.Description=(string)reader["p_description"];
       
this.Ingredients=(string)reader["p_ingredient"];
       
this.Size=(string)reader["inv_size"];
       
this.Price=(decimal)reader["inv_price"];
       
this.Prices=Convert.ToString(Price);

        reader
.Close();
        connect
.Close();
   
}
}

Here is the code for ShoppingItem.cs:

usingSystem;
usingSystem.Collections.Generic;
usingSystem.Linq;
usingSystem.Web;
usingSystem.Configuration;
usingSystem.Data.SqlClient;
usingSystem.Data;

/// <summary>
/// Summary description for ShoppingCart
/// </summary>
publicclassShoppingItem:IEquatable<ShoppingItem>
{

   
publicintQuantity{get;set;}

   
privateint inventoryId;
   
publicintInventoryId
   
{
       
get{return inventoryId;}
       
set{ inventoryId = value;}
   
}

   
private item _item =null;
   
public item Item
   
{
       
get
       
{
           
if(_item ==null)
           
{
                _item
=new item(InventoryId);
           
}
           
return _item;
       
}
   
}

   
publicstringDescription
   
{
       
get{returnItem.Description;}
   
}

   
publicstringIngredients
   
{
       
get{returnItem.Ingredients;}
   
}

   
publicstringSize
   
{
       
get{returnItem.Size;}
   
}

   
publicdecimalPrice
   
{
       
get{returnItem.Price;}
   
}

   
publicdecimalTotalPrice
   
{
       
get{returnPrice*Quantity;}
   
}

   
publicShoppingItem(intItemId)
   
{
       
this.InventoryId=ItemId;
   
}

   
publicboolEquals(ShoppingItem item)
   
{
       
return item.InventoryId==this.InventoryId;
   
}
}

Here is the code for ShoppingCart.cs:

usingSystem;
usingSystem.Collections.Generic;
usingSystem.Linq;
usingSystem.Web;

/// <summary>
/// Summary description for ShoppingCart
/// </summary>
publicclassShoppingCart
{
   
publicList<ShoppingItem>Items{get;privateset;}
   
publicstaticreadonlyShoppingCartInstance;

   
staticShoppingCart()
   
{
       
if(HttpContext.Current.Session["ShoppingCart"]==null)
       
{
           
Instance=newShoppingCart();
           
Instance.Items=newList<ShoppingItem>();
           
HttpContext.Current.Session["ShoppingCart"]=Instance;
       
}
       
else
       
{
           
Instance=(ShoppingCart)HttpContext.Current.Session["ShoppingCart"];
       
}
   
}

   
protectedShoppingCart(){}

   
publicvoidAddItem(intItemsID)
   
{
       
ShoppingItem newItem =newShoppingItem(ItemsID);

       
if(Items.Contains(newItem))
       
{
           
foreach(ShoppingItem item inItems)
           
{
               
if(item.Equals(newItem))
               
{
                    item
.Quantity++;
                   
return;
               
}
           
}
       
}
       
else
       
{
            newItem
.Quantity=1;
           
Items.Add(newItem);
       
}
   
}

   
publicvoidSetItemQuantity(intItemsID,intQuantity)
   
{
       
if(Quantity==0)
       
{
           
RemoveItem(ItemsID);
           
return;
       
}

       
ShoppingItem updatedItem =newShoppingItem(ItemsID);

       
foreach(ShoppingItem item inItems)
       
{
           
if(item.Equals(updatedItem))
           
{
                item
.Quantity=Quantity;
               
return;
           
}
       
}
   
}

   
publicvoidRemoveItem(intItemsID)
   
{
       
ShoppingItem removedItem =newShoppingItem(ItemsID);
       
Items.Remove(removedItem);
   
}

   
publicdecimalGetSubTotal()
   
{
       
decimal subTotal =0;
       
foreach(ShoppingItem item inItems)
       
{
            subTotal
+= item.TotalPrice;
       
}
       
return subTotal;
   
}
}

I know I am close to getting this to work.  I just need some advice on how to pass the selected row on the GridView from the pizza.aspx page to the shopping cart on the order.aspx page.

Thank you in advance  for your help.

usingSystem;
usingSystem.Collections.Generic;
usingSystem.Linq;
usingSystem.Web;
usingSystem.Configuration;
usingSystem.Data.SqlClient;
usingSystem.Data;

/// <summary>
/// Summary description for item
/// </summary>
publicclass item
{
   
publicstringDescription{get;set;}
   
publicstringIngredients{get;set;}
   
publicstringSize{get;set;}
   
publicdecimalPrice{get;set;}
   
publicstringPrices{get;set;}

   
public item(intProductId)
   
{
       
stringDatabaseConnection=ConfigurationManager.ConnectionStrings["dbconnect"].ToString();

       
SqlConnection connect =newSqlConnection(DatabaseConnection);

        connect
.Open();

       
SqlCommand myCommand =newSqlCommand("SELECT product.p_description AS Description, inventory.inv_size AS Size, inventory.inv_price AS Price FROM inventory INNER JOIN product ON inventory.product_id = product.product_id WHERE (inventory.inv_id = @add_id)", connect);
        myCommand
.Parameters.AddWithValue("@add_id",SqlDbType.Int).Value=ProductId;

       
SqlDataReader reader = myCommand.ExecuteReader();

        reader
.Read();

       
this.Description=(string)reader["p_description"];
       
this.Ingredients=(string)reader["p_ingredient"];
       
this.Size=(string)reader["inv_size"];
       
this.Price=(decimal)reader["inv_price"];
       
this.Prices=Convert.ToString(Price);

        reader
.Close();
        connect
.Close();
   
}
}

Here is the code for ShoppingItem.cs:

usingSystem;
usingSystem.Collections.Generic;
usingSystem.Linq;
usingSystem.Web;
usingSystem.Configuration;
usingSystem.Data.SqlClient;
usingSystem.Data;

/// <summary>
/// Summary description for ShoppingCart
/// </summary>
publicclassShoppingItem:IEquatable<ShoppingItem>
{

   
publicintQuantity{get;set;}

   
privateint inventoryId;
   
publicintInventoryId
   
{
       
get{return inventoryId;}
       
set{ inventoryId = value;}
   
}

   
private item _item =null;
   
public item Item
   
{
       
get
       
{
           
if(_item ==null)
           
{
                _item
=new item(InventoryId);
           
}
           
return _item;
       
}
   
}

   
publicstringDescription
   
{
       
get{returnItem.Description;}
   
}

   
publicstringIngredients
   
{
       
get{returnItem.Ingredients;}
   
}

   
publicstringSize
   
{
       
get{returnItem.Size;}
   
}

   
publicdecimalPrice
   
{
       
get{returnItem.Price;}
   
}

   
publicdecimalTotalPrice
   
{
       
get{returnPrice*Quantity;}
   
}

   
publicShoppingItem(intItemId)
   
{
       
this.InventoryId=ItemId;
   
}

   
publicboolEquals(ShoppingItem item)
   
{
       
return item.InventoryId==this.InventoryId;
   
}
}

Here is the code for ShoppingCart.cs:

usingSystem;
usingSystem.Collections.Generic;
usingSystem.Linq;
usingSystem.Web;

/// <summary>
/// Summary description for ShoppingCart
/// </summary>
publicclassShoppingCart
{
   
publicList<ShoppingItem>Items{get;privateset;}
   
publicstaticreadonlyShoppingCartInstance;

   
staticShoppingCart()
   
{
       
if(HttpContext.Current.Session["ShoppingCart"]==null)
       
{
           
Instance=newShoppingCart();
           
Instance.Items=newList<ShoppingItem>();
           
HttpContext.Current.Session["ShoppingCart"]=Instance;
       
}
       
else
       
{
           
Instance=(ShoppingCart)HttpContext.Current.Session["ShoppingCart"];
       
}
   
}

   
protectedShoppingCart(){}

   
publicvoidAddItem(intItemsID)
   
{
       
ShoppingItem newItem =newShoppingItem(ItemsID);

       
if(Items.Contains(newItem))
       
{
           
foreach(ShoppingItem item inItems)
           
{
               
if(item.Equals(newItem))
               
{
                    item
.Quantity++;
                   
return;
               
}
           
}
       
}
       
else
       
{
            newItem
.Quantity=1;
           
Items.Add(newItem);
       
}
   
}

   
publicvoidSetItemQuantity(intItemsID,intQuantity)
   
{
       
if(Quantity==0)
       
{
           
RemoveItem(ItemsID);
           
return;
       
}

       
ShoppingItem updatedItem =newShoppingItem(ItemsID);

       
foreach(ShoppingItem item inItems)
       
{
           
if(item.Equals(updatedItem))
           
{
                item
.Quantity=Quantity;
               
return;
           
}
       
}
   
}

   
publicvoidRemoveItem(intItemsID)
   
{
       
ShoppingItem removedItem =newShoppingItem(ItemsID);
       
Items.Remove(removedItem);
   
}

   
publicdecimalGetSubTotal()
   
{
       
decimal subTotal =0;
       
foreach(ShoppingItem item inItems)
       
{
            subTotal
+= item.TotalPrice;
       
}
       
return subTotal;
   
}
}

I know I am close to getting this to work.  I just need some advice on how to pass the selected row on the GridView from the pizza.aspx page to the shopping cart on the order.aspx page.

Thank you in advance  for your help.

 

 

 


Viewing all articles
Browse latest Browse all 32059

Trending Articles