i have done that see this answer here, working very well
// int availableInventoryQty = AvailableInventoryQty();
if (lblProductPrice.Text.Length > 0 && txtQuantity.Text.Length > 0)
{
double stock = Convert.ToDouble(txtQuantity.Text);
double customerqty = Convert.ToDouble(txtQuantity.Text);
double tqty = stock - customerqty;
double gqty = tqty;
{
if (lblProductPrice.Text.Length > 0 && txtQuantity.Text.Length > 0)
{
double unitprice2 = Convert.ToDouble(lblProductPrice.Text);
double customerqty2 = Convert.ToDouble(txtQuantity.Text);
double grandtotal = unitprice2 * customerqty;
double total = grandtotal;
{
{
if ((availableQuantity != 0) && (quantity != 0) && (quantity < availableQuantity))
{
using (SqlConnection con = new SqlConnection())
{
con.ConnectionString = str;
using (SqlCommand cmd = new SqlCommand())
{
cmd.CommandType = CommandType.Text;
cmd.Connection = con;
//Change the query like below
//Make sure the column name matches the with the name provided in code as per your table
cmd.CommandText = "INSERT INTO Sells (UserName,CustomerName,CustomerAddress,CustomerPhone,ProductCategory,UnitPrice,Quantity,PaymentStatus,TotalAmount) VALUES(@UserName,@CustomerName,@CustomerAddress,@CustomerPhone,@ProductCategory,@UnitPrice,@Quantity,@PaymentStatus,@TotalAmount)";
cmd.Parameters.AddWithValue("@UserName", Session["userName"]);
cmd.Parameters.AddWithValue("@CustomerName", ddlcustomername.SelectedItem.Text.Trim());
cmd.Parameters.AddWithValue("@CustomerAddress", ddlcustomeraddress.SelectedItem.Text.Trim());
cmd.Parameters.AddWithValue("@CustomerPhone", customerphone);
cmd.Parameters.AddWithValue("@ProductCategory", productcategory);
cmd.Parameters.AddWithValue("@UnitPrice", lblProductPrice.Text.Trim());
cmd.Parameters.AddWithValue("@Quantity", txtQuantity.Text.Trim());
cmd.Parameters.AddWithValue("@PaymentStatus", ddlpaymentstatus.SelectedItem.Text.Trim());
cmd.Parameters.AddWithValue("@TotalAmount", grandtotal);
// cmd.Parameters.AddWithValue("@OrderDate", DateTime.Today);
con.Open();
inserted = cmd.ExecuteNonQuery();
con.Close();
}
}
}
else
{
ClientScript.RegisterClientScriptBlock(this.GetType(), "", "alert('No of Quantity entered is not available in Stock')", true);
}
if (inserted > 0)
{
int updatedStock = availableQuantity - quantity;
using (SqlConnection con = new SqlConnection())
{
con.ConnectionString = str;
using (SqlCommand cmd = new SqlCommand())
{
cmd.CommandType = CommandType.Text;
cmd.Connection = con;
cmd.CommandText = "UPDATE Products SET Quantity = " + updatedStock + " WHERE ProductCategory = '" + productcategory + "'";
con.Open();
inserted = cmd.ExecuteNonQuery();
con.Close();
}
}
}
BindProductsSells();
}