Qty Control in C#

后端 未结 5 967
-上瘾入骨i
-上瘾入骨i 2021-01-27 07:50

pstrjds, From the code you have provided I am trying to implement that code and the msdn link you provided couple of days ago. I am not sure how to put it together but I am see

5条回答
  •  爱一瞬间的悲伤
    2021-01-27 08:05

    Your application flow should be as following;

    protected void Button1_Click(object sender, EventArgs e)
        {
            SqlConnection c = new SqlConnection("Data Source=.; Integrated Security=SSPI; Initial Catalog=FA");
    
    
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = c;
            c.Open();
    
            // get remaining stocks
            cmd.CommandText = "SELECT Qty from TEST WHERE Id=1";
            int ret = Convert.ToInt32(cmd.ExecuteScalar().ToString());
    
            if (ret == 0)
            {
                Label1.Text = "Not enough stocks.";
            }
            else 
            {
                cmd.CommandText = "UPDATE TEST SET Qty = Qty-1 WHERE Id=1";
                cmd.ExecuteNonQuery();
    
                if (ret == 2)
                {
                    Label1.Text = "Re-order. Remaining stocks: 1";
                }
                else
                {
                    Label1.Text = "Remaining stocks: " + (ret-1).ToString();
                }
            }
    
    
            c.Close();
        }
    

提交回复
热议问题