Allow insert record in database if item available in stock in ASP.Net using C#

micah
 
on Mar 25, 2021 11:40 PM
847 Views

I have this code below that ensures that user don't submit more than quantity in stock table, but let’s say i have 100 items in stock table the code below doesn't allow transaction to pass, i want it to pass but when the quantity entered is more than the stock show the error message.

It doesn't allow the last item to be submitted, if it remains one and i want to submit the item it won’t go, unless it’s less than the number. I want only zero number no to be submitted. If the stock is zero don’t submit.

protected void btnorder_Click(object sender, EventArgs e)
{
    string customerphone = txtphone.Text.Trim();
    string model = ddlbrand.SelectedItem.Text.Trim();
    // string serial = ddlserial.SelectedItem.Text.Trim();
    string name = txtname.Text.Trim();
    string address = txtaddress.Text.Trim();
    //  string name = ddlcustomername.SelectedItem.Text.Trim();
    // string address = ddlcustomeraddress.SelectedItem.Text.Trim();
    decimal unitPrice = Convert.ToDecimal(txtprice.Text.Trim());
    int availableQuantity = Convert.ToInt32(ddstock.SelectedItem.Text.Trim());
    int quantity = Convert.ToInt32(txtqty.Text.Trim());
    int inserted = 0;

    //  int availableInventoryQty = AvailableInventoryQty();
    if (txtqty.Text.Length > 0 && ddstock.SelectedItem.Text.Length > 0)
    {
        double stock = Convert.ToDouble(ddstock.SelectedItem.Text);
        double customerqty = Convert.ToDouble(txtqty.Text);
        double tqty = stock - customerqty;
        double gqty = tqty;
        {
            if (txtprice.Text.Length > 0 && txtqty.Text.Length > 0)
            {
                double unitprice2 = Convert.ToDouble(txtprice.Text);
                double customerqty2 = Convert.ToDouble(txtqty.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;


                            cmd.CommandText = "INSERT INTO ItemOrdered (Receipt,Brand,Quantity,Price,Sum) VALUES(@Receipt,@Brand,@Quantity,@Price,@Sum)";


                            cmd.Parameters.AddWithValue("@Receipt", txtcode.Text.Trim());
                            // cmd.Parameters.AddWithValue("@Item", ddlitem.SelectedItem.Text.Trim());

                            cmd.Parameters.AddWithValue("@Brand", model);
                            cmd.Parameters.AddWithValue("@Price", txtprice.Text.Trim());
                            cmd.Parameters.AddWithValue("@Quantity", txtqty.Text.Trim());

                            cmd.Parameters.AddWithValue("@Sum", grandtotal);
                            // cmd.Parameters.AddWithValue("@Sum", txttotalamount.Text.Trim());
                            con.Open();
                            inserted = cmd.ExecuteNonQuery();

                            // txtphone.Text = sdr["Phone"].ToString();
                            con.Close();
                            //BindGridreport();
                        }
                    }
                }
                else
                {
                    ClientScript.RegisterClientScriptBlock(this.GetType(), "", "alert('No of Quantity entered is not available in Stock')", true);
                }
                ScriptManager.RegisterClientScriptBlock(btnsave, this.GetType(), "alert", "<script>alert('Data Submitted Successfully ... !!')</script>", false);
            }
        }
        // BindProductsSells();
    }
}

 

Download FREE API for Word, Excel and PDF in ASP.Net: Download
dharmendr
 
on Mar 26, 2021 07:36 AM
micah says:
if ((availableQuantity != 0) && (quantity != 0) && (quantity < availableQuantity))
                                               {

Change above with below.

if (availableQuantity > 0 && quantity > 0)
{