Its Urgent. I want it by tonight.
I am making a website that sells the products. My page that I want to do is after the user chooses a product, updates the quantity that the user wants to buy & clicks on the Checkout button, it should automatically update the existing quantity in the database by subtracting with the quantity the user ordered. If its not clear, I ll give an example. My Product Table has ProductId, ProductName, Price, Description, ProductImage, Seller & ProductStock. The Product Table has ProductA which has a stock of 10. The user buys 3. After the user buys 3 such products, I want the table to be updated to 7. The user makes use of the textbox that helps him to update any quantity that he wants.
This is my code below:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Data.Sql;
public partial class ViewCart : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (Profile.SCart == null)
{
Profile.SCart = new ShoppingCartExample.Cart();
}
if (!Page.IsPostBack)
{
ReBindGrid();
}
if (Profile.SCart.Items == null)
{
TotalLabel.Visible = false;
}
}
protected void grdCart_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
string connStr2 = ConfigurationManager.ConnectionStrings["siteConnectionString"].ConnectionString;
SqlConnection con2 = new SqlConnection(connStr2);
con2.Open();
SqlCommand com2;
TextBox txtQuantity = (TextBox)grdCart.Rows[e.RowIndex].Cells[2].Controls[0];
int Quantity = Convert.ToInt32(txtQuantity.Text);
if (Quantity == 0)
{
Profile.SCart.Items.RemoveAt(e.RowIndex);
}
else
{
Profile.SCart.Items[e.RowIndex].Quantity = Quantity;
com2 = new SqlCommand("UPDATE Product SET ProductStock = ( (SELECT ProductStock FROM Product WHERE ProductName = @ProductName) - " + (txtQuantity.Text) + " ) WHERE UserName = @UserName");
com2.ExecuteNonQuery();
}
con2.Close();
grdCart.EditIndex = -1;
ReBindGrid();
}
protected void grdCart_RowEditing(object sender, GridViewEditEventArgs e)
{
grdCart.EditIndex = e.NewEditIndex;
ReBindGrid();
}
protected void grdCart_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
Profile.SCart.Items.RemoveAt(e.RowIndex);
ReBindGrid();
}
protected void grdCart_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
grdCart.EditIndex = -1;
ReBindGrid();
}
private void ReBindGrid()
{
grdCart.DataSource = Profile.SCart.Items;
DataBind();
TotalLabel.Text = string.Format("Total Cost:{0,19:C}", Profile.SCart.Total);
}
protected void grdCart_SelectedIndexChanged(object sender, EventArgs e)
{
}
protected void Checkout_Click(object sender, EventArgs e)
{
string connStr = ConfigurationManager.ConnectionStrings["siteConnectionString"].ConnectionString;
SqlCommand com;
{
foreach (GridViewRow g1 in grdCart.Rows)
{
SqlConnection con = new SqlConnection(connStr);
com = new SqlCommand("insert into OrderDetails(OrderDetailsID,Product,Quantity,Price,Total) values ('" + g1.Cells[0].Text + "','" + g1.Cells[1].Text + "','" + g1.Cells[2].Text + "','" + g1.Cells[3].Text + "','" + g1.Cells[4].Text + "')", con);
con.Open();
com.ExecuteNonQuery();
con.Close();
Response.Redirect("Payment.aspx");
}
}
}
}
The inserting of data in the OrdersDetails are working but not the subtraction operation. Pls help.