如果变量为true,则更新数据库

时间:2018-02-01 15:41:00

标签: c# asp.net visual-studio web-applications web-deployment

即使请求的金额超过可用金额,也会跳到第四个if语句。

它适用于前两个,但是当我添加第三个和第四个if语句时,它似乎根本没有阅读第三个并跳过预先形成第四个并进入 - 库存。

欢迎任何帮助。

这是我的代码全部在一个按钮下。

 protected void Button1_Click(object sender, EventArgs e)
{




   // 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);






    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);

    int ammountRequested = Convert.ToInt32(txtStickers.Text);
int ammountAvailable = Convert.ToInt32(Label12.Text);

int ammountRequestedlids = Convert.ToInt32(txtLids.Text);
int ammountAvailablelids = Convert.ToInt32(Label13.Text);

if (ammountRequested > ammountAvailable)
        {



            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 (ammountRequested < ammountAvailable)
        {


            con.Open();
            cmd.ExecuteNonQuery();
            cmd1.ExecuteNonQuery();
            cmd2.ExecuteNonQuery();
            cmd3.ExecuteNonQuery();
            cmd4.ExecuteNonQuery();

            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");
        }
else if (ammountRequestedlids > ammountAvailablelids)
        {
            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 0, <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 Small Jar Stock level is 0 and therefore a Mail has been Sent. <br> unfortunatley ')</script>");

        }
else if (ammountRequestedlids < ammountAvailablelids)
        {

            con.Open();
            cmd.ExecuteNonQuery();
            cmd1.ExecuteNonQuery();
            cmd2.ExecuteNonQuery();
            cmd3.ExecuteNonQuery();
            cmd4.ExecuteNonQuery();

            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 small jar stock level is low and therefore a Mail has been Sent notifying the manager. ')</script>");
            Server.Transfer("orderDisplay.aspx");
        }

1 个答案:

答案 0 :(得分:0)

经过大量讨论后,听起来您的实际问题是您希望确保用户无法申请更多的库存。你有很多代码,并且不清楚从哪里获得所有信息,但这里有一个简单的模板,说明你如何能够实现这个目标

protected void Button1_Click(object sender, EventArgs e)
{
    //Get the values from SQL etc.

    int ammountRequested = //This needs to be pulled from somewhere
    int ammountAvailable = Convert.ToInt32(Label12.Text);

    if(ammountRequested > ammountAvailable)
    {
        //Send the error message
    }
    else
    {
        //Do the updates
        //Send the message
    }
}

修改

查看更新后的代码,您似乎对if语句感到困惑。我建议阅读this tutorial

听起来您还没有清楚了解您希望代码遵循的工作流程。在编写更多代码之前,您应该花些时间考虑不同的情况以及如何处理它们。问自己一些问题:如果用户请求多个股票但一个不可用而另一个是?如何处理这种情况?

在SO上有很多有才华的用户,他们中的很多人都可以为你解答这些问题。但这就是SO的重点,我们在这里帮助解决编程问题,这样你就可以学习编程。如果在考虑了工作流程并理解if语句后,您仍然不清楚某些事情,请写下Minimal, Complete, and Verifiable example您坚持的内容,我们会帮助您。如果您还没有通读Asking a Question中的Help Center部分。