ShoppingDb.cs

using System;

using System.Configuration;

using System.Data;

using System.Data.SqlClient;

 

namespace prjstart.Components

{

       /// <summary>

       /// Summary description for Shoppingfunct.

       /// </summary>

       public class Shoppingfunct

       {

        

              public string getshoppingcart()

              {

                     System.Web.HttpContext context =  System.Web.HttpContext.Current;

 

                     if(context.User.Identity.Name!="") // if the user ia registered the return the customerid.

                     {

                           return context.User.Identity.Name;

                     }

                     if(context.Request.Cookies["ShoppingCart"]!= null)

                     {

                           return context.Request.Cookies["ShoppingCart"].Value;

                     }

                     else

                     {// Generate a new random GUID using System.Guid Class

                           Guid tempCartId = Guid.NewGuid();

 

                           // Send tempCartId back to client as a cookie

                           context.Response.Cookies["ShoppingCart"].Value = tempCartId.ToString();

 

                           // Return tempCartId

                           return tempCartId.ToString();

 

                     }

 

              }

 

              public void MigrateCart(String oldCartId, int newCartId)

              {

 

                     // Create Instance of Connection and Command Object

                     SqlConnection myConnection = new SqlConnection(ConfigurationSettings.AppSettings["ConnectionString"]);

                     SqlCommand myCommand = new SqlCommand("MigrateShoppingCart", myConnection);

 

                     // Mark the Command as a SPROC

                     myCommand.CommandType = CommandType.StoredProcedure;

 

                     // Add Parameters to SPROC

                     SqlParameter cart1 = new SqlParameter("@OriginalCartId ", SqlDbType.NVarChar, 50);

                     cart1.Value = oldCartId;

                     myCommand.Parameters.Add(cart1);

 

                     SqlParameter cart2 = new SqlParameter("@NewCartId", SqlDbType.Int, 50);

                     cart2.Value = newCartId;

                     myCommand.Parameters.Add(cart2);

 

                     // Open the connection and execute the Command

                     myConnection.Open();

                     myCommand.ExecuteNonQuery();

                     myConnection.Close();

              }

 

              public void additem(string cartid,int productid,int quantity)

              {

                     // Adds the item into shopping cart .Exceutes the stored procedure additemintoshoppingcart.

                     // Requires three parameters cartid,productid(this is the product the user selects).

                     // This stored procedure adds the items into shopping cart.It updates the quantity if needed and inserts into table shopping cart the record about a particualr product.

 

                     SqlConnection objcon = new SqlConnection(ConfigurationSettings.AppSettings["ConnectionString"]);

                     SqlCommand objcom = new SqlCommand("additemintoshoppingcart",objcon);

                    

                     objcom.CommandType = CommandType.StoredProcedure;

 

                     SqlParameter parametercartid = new SqlParameter("@cartid",SqlDbType.VarChar,50);

                     parametercartid.Value = cartid;

                     objcom.Parameters.Add(parametercartid);

 

                     SqlParameter parameterproductid = new SqlParameter("@productid",SqlDbType.Int,20);

                     parameterproductid.Value = productid;

                     objcom.Parameters.Add(parameterproductid);

 

                     SqlParameter parameterquantity = new SqlParameter("@quantity",SqlDbType.Int,20);

                     parameterquantity.Value = quantity;

                     objcom.Parameters.Add(parameterquantity);

 

                     try

                     {

 

                           objcon.Open();

                           objcom.ExecuteNonQuery();

                           objcon.Close();

                     }

                     catch(Exception e)

                     {

                           Console.WriteLine(e.Message);

                     }

 

                    

              }

 

 

              public DataSet fillshoppinggrid(string id)

              {

                     SqlConnection objcon = new SqlConnection(ConfigurationSettings.AppSettings["ConnectionString"]);

                     string sql="Select pr.productid,pr.modelname,pr.modelnumber,sc.quantity,pr.unitcost,(sc.quantity * pr.unitcost)as totalamount from products pr,shoppingcart sc where pr.productid = sc.productid and sc.cartid='" + id + "'order by modelname";

                     SqlDataAdapter objda = new SqlDataAdapter(sql,objcon);

                     DataSet ds = new DataSet();

                     objda.Fill(ds);

                     return ds;

 

              }

 

              public string gettotal(string getcartidfortotal)

              {

                     string fin="";

                     SqlConnection objcon = new SqlConnection(ConfigurationSettings.AppSettings["ConnectionString"]);

                     string sql="select finalamount = SUM(pr.unitcost * sc.quantity)from products pr,shoppingcart sc where pr.productid =sc.productid and sc.cartid='"+getcartidfortotal+"'";

                    

                    

                     SqlCommand objcom = new SqlCommand(sql,objcon);

                     objcon.Open();

 

                     SqlDataReader dr =objcom.ExecuteReader();

 

                     while(dr.Read())

                     {

                           fin =dr["finalamount"].ToString();

                          

                     }

 

                     return fin;

 

                    

                    

 

                    

                    

              }

 

              public void RemoveItem(string cartid,int productid)

              {

                     SqlConnection objcon = new SqlConnection(ConfigurationSettings.AppSettings["ConnectionString"]);

                     string sql ="Delete from shoppingcart where cartid='"+ cartid + "'and productid ='" +productid +"'";

                     SqlCommand objcom = new SqlCommand(sql,objcon);

                     objcon.Open();

                     objcom.ExecuteNonQuery();

                     objcon.Close();

              }

 

              public void UpdateItem(string cartid,int productid,int quantity)

              {

                     if(quantity < 0)

                     {

                           throw new Exception("Quantity cannot be a negative number");

                     }

                    

                     SqlConnection objcon = new SqlConnection(ConfigurationSettings.AppSettings["ConnectionString"]);

                     string sql ="update shoppingcart set quantity ='" + quantity +"' where cartid='" +cartid +"' and productid='" +productid +"'";

                     SqlCommand objcom = new SqlCommand(sql,objcon);

                     objcon.Open();

                     objcom.ExecuteNonQuery();

                     objcon.Close();

              }

 

             

             

              public Shoppingfunct()

              {

                     //

                     // TODO: Add constructor logic here

                     //

              }

 

             

       }

}

 

 

Back to Main Page