Check variable to prevent form submit if zero in ASP.Net

Last Reply 16 days ago By dharmendr

Posted 16 days ago

How do i check both Qty and Range and prevent user from submitting if the both qty is zero

This code only checks the quantity of an item if the qty is zero don't submit

but now i want also check the availablerange and availableQuantity2

so if availableQuantity2 and availablerange is zero don't submit.

    int availableQuantityRang = Convert.ToInt32(txtrange.Text.Trim());
    int availablerange = Convert.ToInt32(txtacctualrange.Text.Trim());
    string model2 = txtEmpName1.Text.Trim();
    string price = txtprice2.Text.Trim();
    string sum = txtsum.Text.Trim();
    string serial = txtserial2.Text.Trim();
    decimal unitPrice2 = Convert.ToDecimal(txtprice2.Text.Trim());
    int availableQuantity2 = Convert.ToInt32(txtstock2.Text.Trim());
    int quantity2 = Convert.ToInt32(txtqty2.Text.Trim());
    int inserted2 = 0;
 
    if (availableQuantity2 > 0 && quantity2 > 0)
 
    {
        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 (Sales_Person,Receipt,Serial,Fromthis,Tothis,Dsc,Qty,Price,Sum) VALUES(@Sales_Person,@Receipt,@Serial,@Fromthis,@Tothis,@Dsc,@Qty,@Price,@Sum)";
                // cmd.Parameters.AddWithValue("@Sales_Person", HttpContext.Current.User.Identity.Name);
                cmd.Parameters.AddWithValue("@Sales_Person", HttpContext.Current.User.Identity.Name);
                cmd.Parameters.AddWithValue("@Fromthis", txtfrom2.Text.Trim());
                cmd.Parameters.AddWithValue("@Tothis", txtto2.Text.Trim());
                cmd.Parameters.AddWithValue("@Receipt", txtcode2.Text.Trim());
                cmd.Parameters.AddWithValue("@Serial", txtserial2.Text.Trim());
 
                cmd.Parameters.AddWithValue("@Dsc", txtEmpName1.Text.Trim());
                cmd.Parameters.AddWithValue("@Price", price);
                cmd.Parameters.AddWithValue("@Qty", txtqty2.Text.Trim());
 
                cmd.Parameters.AddWithValue("@Sum", sum);
 
                //  cmd.Parameters.AddWithValue("@Sum", grandtotal);
                // cmd.Parameters.AddWithValue("@Sum", txttotalamount.Text.Trim());
                con.Open();
                inserted2 = 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(btnorder2, this.GetType(), "alert", "<script>alert('Data Submitted Successfully ... !!')</script>", false);
}
if (inserted2 > 0)
{
    int updatedStock2 = availableQuantity2 - quantity2;
    int updatedrange = availableQuantityRang - availablerange;
    using (SqlConnection con = new SqlConnection())
    {
        con.ConnectionString = str;
        using (SqlCommand cmd = new SqlCommand())
        {
            cmd.CommandType = CommandType.Text;
            cmd.Connection = con; // cmd.CommandText = "UPDATE Stock_Table SET Quantity = " + updatedStock + " WHERE Brand = '" + model + "'";
            cmd.CommandText = "UPDATE Stock_Table SET RangeRemaining = '" + updatedrange + "', QtyRemaining = '" + updatedStock2 + "' WHERE Serial = '" + serial + "'";
            con.Open();
            inserted2 = cmd.ExecuteNonQuery();
 
            con.Close();
 
        }
    }
}

 

Posted 16 days ago
micah says:
if (availableQuantity2 > 0 && quantity2 > 0)

 Add with and condition.

if (availableQuantity2 > 0 && quantity2 > 0 && availablerange > 0)