【问题标题】:restrict when to execute database update限制何时执行数据库更新
【发布时间】:2018-02-01 12:18:46
【问题描述】:

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

但如果变量 (p

但在按钮下,即使库存低于 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 之间,我认为我需要做第一个陈述。

关于如何做到这一点的任何建议?

【问题讨论】:

  • if (p &lt;= 500 &amp;&amp; p &gt; 0) {... 花点时间重命名变量。给他们起有意义的名字。你未来的自己会为此感谢我的。

标签: c# asp.net


【解决方案1】:

zero 小于 500,这就是它只运行第一条语句的原因。如果您将小于零放在首位,它将按预期工作:

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

【讨论】:

    【解决方案2】:
    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 }
    

    【讨论】:

    • 谢谢你的工作。我还有一个问题。如果 P=0 我不想执行按钮来更新数据库表。你知道如果 p
    • 不客气。我不喜欢使用太多的“=”,所以,可以试试:if (p
    • 是的,这行得通。但也在这个按钮下我必须更新数据库,但我只想在库存大于0时更新数据库,目前即使库存低于0,它仍在更新数据库,即使有订单也在处理没有库存来创建订单
    • 如果我是对的,可能您将日期保存在“IF”语句之外。尝试将保存在DB中的代码放入IF中
    • 我在上面编辑了我的代码,如果你想看看的话。你可能更清楚我想要做什么。
    【解决方案3】:

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

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

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

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

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

    【讨论】:

      猜你喜欢
      • 2012-12-12
      • 1970-01-01
      • 2019-07-02
      • 2019-11-07
      • 2012-05-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-02-27
      相关资源
      最近更新 更多