I later got the answer, see the answer below
protected void btnSave_Click(object sender, EventArgs e)
{
// string name = ddlcustomername.Text.Trim();
// string address = customeraddress.Text.Trim();
string productName = ddlProducts.SelectedItem.Text.Trim();
string name = ddlcustomername.SelectedItem.Text.Trim();
string address = ddlcustomeraddress.SelectedItem.Text.Trim();
decimal unitPrice = Convert.ToDecimal(lblProductPrice.Text.Trim());
int availableQuantity = Convert.ToInt32(lblQuantityAvailable.Text.Trim());
int quantity = Convert.ToInt32(txtQuantity.Text.Trim());
int inserted = 0;
// 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 (txtunitprice.Text.Length > 0 && txtdiscount.Text.Length > 0)
{
// double unitprice = Convert.ToDouble(txtunitprice.Text);
// double discount = Convert.ToDouble(txtdiscount.Text);
// double sum = grandtotal - discount;
// double sumb = sum;
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,ProductName,Ordered,UnitPrice,TotalAmount,OrderDate) VALUES(@UserName,@CustomerName,@ProductName,@Ordered,@UnitPrice,@TotalAmount,@OrderDate)";
cmd.Parameters.AddWithValue("@UserName", Session["userName"]);
cmd.Parameters.AddWithValue("@CustomerName", ddlcustomername.Text.Trim());
cmd.Parameters.AddWithValue("@ProductName", ddlProducts.Text.Trim());
cmd.Parameters.AddWithValue("@Ordered", txtQuantity.Text.Trim());
cmd.Parameters.AddWithValue("@UnitPrice", lblProductPrice.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);
}
}
}
}
}
}
}
cmd.CommandText = "INSERT INTO Sells (UserName,CustomerName,ProductName,Ordered,UnitPrice,TotalAmount,OrderDate) VALUES(@UserName,@CustomerName,@ProductName,@Ordered,@UnitPrice,@TotalAmount,@OrderDate)";