public partial class AddToCart : System.Web.UI.Page
{
decimal total;
DataTable cart = null;
protected void Page_Load(object sender, EventArgs e)
{
if (Session["currentUId"] == null)
Response.Redirect("~/Account/Login.aspx");
if (Session["cart"] != null)
{
cart = (DataTable)Session["cart"];
}
else
{
cart = new DataTable();
cart.Columns.Add("p_id", typeof(int));
cart.Columns.Add("p_name", typeof(string));
cart.Columns.Add("p_price", typeof(decimal));
cart.Columns.Add("p_quantity", typeof(int));
cart.Columns.Add("item_total", typeof(decimal));
}
refreshTotal();
if (Request["DelID"] != null)
{
for (int i = 0; i < cart.Rows.Count; i++)
{
if (cart.Rows[i]["p_id"].ToString() == Request["DelID"].ToString())
cart.Rows.Remove(cart.Rows[i]);
}
Session["cart"] = cart;
refreshTotal();
}
if (Request["ID"] != null)
{
bool Found = false;
for (int i = 0; i < cart.Rows.Count; i++)
{
if (cart.Rows[i]["p_id"].ToString() == Request["ID"].ToString())
Found = true;
}
if (Found == false)
{
int pid = Convert.ToInt16(Request["ID"]);
DataTable dt = new DataTable();
using (SqlConnection conn = new SqlConnection(SqlDataSource1.ConnectionString.ToString()))
{
using (SqlCommand comm = new SqlCommand("select ProductName,UnitCost from Products where ProductID = " + pid, conn))
{
conn.Open();
using (SqlDataReader myReader = comm.ExecuteReader())
{
dt.Load(myReader);
}
conn.Close();
}
}
string name = (string)dt.Rows[0]["ProductName"];
decimal price = (decimal)dt.Rows[0]["UnitCost"];
{
cart.Rows.Add(new object[] { Request["ID"], name, price, (int)1, (decimal)price * 1 });
}
}
Session["cart"] = cart;
refreshTotal();
}
if (IsPostBack == false)
{
GridView4.DataSource = cart;
GridView4.DataBind();
refreshTotal();
}
refreshTotal();
}
protected void refreshCart()
{
for (int i = 0; i < GridView4.Rows.Count; i++)
{
TextBox Tb = (TextBox)GridView4.Rows[i].FindControl("TextBoxCount");
cart.Rows[i]["p_quantity"] = Convert.ToInt16(Tb.Text);
decimal qxp = (decimal)((Convert.ToInt32(Tb.Text) * (decimal)cart.Rows[i]["p_price"]));
cart.Rows[i]["item_total"] = qxp;
}
refreshTotal();
Response.Redirect("~/Products.aspx");
}
protected void refreshTotal()
{
total = (decimal)0;
for (int i = 0; i < cart.Rows.Count; i++)
{
total += (decimal)(cart.Rows[i]["item_total"]);
}
Label1.Text = total.ToString();
Session["total"] = total;
}
protected void Button1_Click(object sender, EventArgs e)
{
refreshCart();
}
protected int GetMaxRequest()
{
using (SqlConnection conn = new SqlConnection(SqlDataSourceRequest.ConnectionString.ToString()))
{
SqlCommand maximum = new SqlCommand("SELECT MAX(R_Id) as maximum FROM [Requests]", conn);
conn.Open();
int m = Convert.ToInt32(maximum.ExecuteScalar());
conn.Close();
return m + 1;
}
}
protected void InsertOrder(int r_id, int p_id, int p_quantity, decimal item_total)
{
using (SqlConnection conn = new SqlConnection(SqlDataSourceOrder.ConnectionString.ToString()))
{
SqlCommand cmd = new SqlCommand("INSERT INTO [Orders](R_Id, ProductID, quantity,Items_total) VALUES (" + r_id + "," + p_id + "," + p_quantity + "," + item_total + ")", conn);
conn.Open();
cmd.ExecuteNonQuery();
conn.Close();
}
}
protected void InsertRequest(string UId, string sdate, decimal total)
{
using (SqlConnection conn = new SqlConnection(SqlDataSourceRequest.ConnectionString.ToString()))
{
SqlCommand cmd = new SqlCommand("INSERT INTO Requests(C_Id, R_Time,R_total) VALUES ('" + UId + "','" + sdate + "'," + total + ")", conn);
conn.Open();
cmd.ExecuteNonQuery();
conn.Close();
}
}
protected void Button2_Click(object sender, EventArgs e)
{
int r_id = GetMaxRequest();
for (int i = 0; i < cart.Rows.Count; i++)
{
InsertOrder((int)r_id, Convert.ToInt16(cart.Rows[i]["p_id"]), Convert.ToInt16(cart.Rows[i]["p_quantity"]), Convert.ToDecimal(cart.Rows[i]["item_total"]));
}
InsertRequest(Session["currentUId"].ToString(), DateTime.Now.ToString(), (decimal)total);
ClientScript.RegisterStartupScript(this.GetType(), "onclick", "<script language=javascript>window.open('Print.aspx','PrintMe','height=300px,width=300px,scrollbars=1');</script>");
Session["cart"] = null;
Session["r_id"] = r_id;
Response.Write("<div class=response> Thank You For Purchasing Our Products</div>");
}
}