Reputation: 29
This is my code for stock inventory management. I have made it so that if the stock is less than or equal to 500
, it will show a MessageBox
, but it will process the order.
but if the variable (p <= 0) then ideally the order should not be processed.
but under the button the orders are being processed even if the stock is below 0.
i need to restrict when these orders are processed and the updates are made to the DB.
//Creating a connection to my database using the connection string
string cs = System.Configuration.ConfigurationManager.ConnectionStrings["RegistrationConnectionString"].ConnectionString;
SqlConnection con = new SqlConnection(cs);
//preparing a query which will take away the values entered in the textboxes from the stock count currently in the database table a rows in the database
// http://www.c-sharpcorner.com/forums/error-when-trying-reduce-a-certain-number-from-the-database
SqlCommand cmd = new SqlCommand("UPDATE StockTable set AmtOfStickers= AmtOfStickers - " + this.txtStickers.Text + ";", con);
SqlCommand cmd1 = new SqlCommand("UPDATE StockTable set AmtOfLids= AmtOfLids - " + this.txtLids.Text + ";", con);
SqlCommand cmd2 = new SqlCommand("UPDATE StockTable set AmtOfSJars= AmtOfSJars - " + this.txtSmallJars.Text + ";", con);
SqlCommand cmd3 = new SqlCommand("UPDATE StockTable set AmtOfLJars= AmtOfLJars - " + this.txtLargeJars.Text + ";", con);
SqlCommand cmd4 = new SqlCommand("INSERT INTO OrderDetails(OrderDate, CustomerID, LidNo, StickerNo, LJarNo, SJarNo) values('" + this.txtDate.Text + "','" + this.txtId.Text + "','"
+ this.txtLids.Text + "','" + this.txtStickers.Text + "','" + this.txtLargeJars.Text + "','" + this.txtSmallJars.Text + "')", con);
con.Open();
cmd.ExecuteNonQuery();
cmd1.ExecuteNonQuery();
cmd2.ExecuteNonQuery();
cmd3.ExecuteNonQuery();
cmd4.ExecuteNonQuery();
SqlCommand cmd5 = new SqlCommand();
cmd5.CommandText = "Select * from [StockTable]";
cmd5.Connection = con;
SqlDataAdapter da = new SqlDataAdapter(cmd5);
DataSet ds = new DataSet();
da.Fill(ds);
Label12.Text = ds.Tables[0].Rows[0]["AmtOfStickers"] + "";
Label13.Text = ds.Tables[0].Rows[0]["AmtOfSJars"] + "";
Label14.Text = ds.Tables[0].Rows[0]["AmtOfLJars"] + "";
Label15.Text = ds.Tables[0].Rows[0]["AmtOfLids"] + "";
int p = Convert.ToInt32(Label12.Text);
int p1 = Convert.ToInt32(Label13.Text);
int p2 = Convert.ToInt32(Label14.Text);
int p3 = Convert.ToInt32(Label15.Text);
if (p <= 0)
{
smtp.Text = "smtp.gmail.com";
username.Text = "[email protected]";
password.Text = "***";
from.Text = "[email protected]";
to.Text ="[email protected]";
subject.Text = "Stock is Low";
body.Text = "Hi Jerry, your sticker stock is 0, please restock soon to continue taking orders. Kay Oates.";
MailMessage mail = new MailMessage(from.Text, to.Text, subject.Text, body.Text);
SmtpClient Client = new SmtpClient(smtp.Text);
Client.Port = 587;
Client.Credentials = new System.Net.NetworkCredential(username.Text, password.Text);
Client.EnableSsl = true;
Client.Send(mail);
//https://stackoverflow.com/questions/15196381/display-messagebox-in-asp-net messagebox dispalying mail sent
Response.Write("<script LANGUAGE='JavaScript' >alert('Your Sticker Stock level is 0 and therefore cannot process this order. A Mail has been Sent notifying the manager. ')</script>");
}
else if (p >= 1 && p <= 500)
{
smtp.Text = "smtp.gmail.com";
username.Text = "[email protected]";
password.Text = "***";
from.Text = "[email protected]";
to.Text = "[email protected]";
subject.Text = "Stock is Low";
body.Text = "Hi Jerry <br> your small jar stock is low, <br> please restock soon to continue taking orders. <br> Kay Oates.";
MailMessage mail = new MailMessage(from.Text, to.Text, subject.Text, body.Text);
SmtpClient Client = new SmtpClient(smtp.Text);
Client.Port = 587;
Client.Credentials = new System.Net.NetworkCredential(username.Text, password.Text);
Client.EnableSsl = true;
Client.Send(mail);
//https://stackoverflow.com/questions/15196381/display-messagebox-in-asp-net messagebox dispalying mail sent
Response.Write("<script LANGUAGE='JavaScript' >alert('Your sticker stock level is low and therefore a Mail has been Sent notifying the manager. ')</script>");
Server.Transfer("orderDisplay.aspx");
}
Therefore I think I need to make the first statement if p is between 1 and 500.
Any suggestions on how to do this?
Upvotes: 0
Views: 104
Reputation: 2598
if (p >= 1 and p <= 500) { first block code }
else if (p <= 0) { second block code }
or better
if (p <= 0) { second block code }
else if (p <= 500) { first block code }
Upvotes: 1
Reputation: 218837
It sounds like you're just looking for this:
if (p <= 500 && p > 0)
Otherwise your else
would never be entered because all values less than 0 are also less than 500.
Alternatively, you could swap the conditions and test the more constrained one first:
if (p <= 0)
{
//...
}
else if (p <= 500)
{
//...
}
Upvotes: 0
Reputation: 1056
zero is less than 500 and thats why it only runs the first statement. If you put the less than zero first it will work as expected:
if(p <= 0)
{
// some code
} else if (p <= 500) {
// Some code
}
Upvotes: 1