限制何时执行数据库更新

时间:2018-02-01 12:18:47

标签: c# asp.net

这是我的库存管理代码。我已经做到这一点,如果股票小于或等于500,它将显示MessageBox,但它将处理订单。

但是如果变量(p <= 0)则理想情况下不应该处理该顺序。

但在按钮下,即使库存低于0,订单也会被处理。

我需要限制何时处理这些订单并对数据库进行更新。

    //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 = "molagahoney@gmail.com";
            password.Text = "***";
            from.Text = "molagahoney@gmail.com";
            to.Text ="molagahoney@gmail.com";
            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 = "molagahoney@gmail.com";
            password.Text = "***";
            from.Text = "molagahoney@gmail.com";
            to.Text = "molagahoney@gmail.com";
            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");
        }

因此,如果p介于1和500之间,我认为我需要做出第一个陈述。

有关如何执行此操作的任何建议吗?

3 个答案:

答案 0 :(得分:1)

零小于500,这就是为什么它只运行第一个语句。如果你先把小于零的东西按预期工作:

if(p <= 0)
{
    // some code
} else if (p <= 500) {
    // Some code
}

答案 1 :(得分:1)

if (p >= 1 and p <= 500) { first block code }
else if (p <= 0) { second block code }

或更好

if (p <= 0) { second block code }
else if (p <= 500) { first block code }

答案 2 :(得分:0)

听起来你只是在寻找这个:

if (p <= 500 && p > 0)

否则永远不会输入else因为所有小于0的值也小于500。

或者,您可以交换条件并首先测试更受约束的条件:

if (p <= 0)
{
    //...
}
else if (p <= 500)
{
    //...
}