C#SQL Server从数据库读取数量并将其显示在消息框中

时间:2018-12-14 15:47:34

标签: c# sql-server-2012

因此,我使用C#和sql server management studio的POS快要完成了,但是如果您输入的数量大于库存量,则在POS中显示特定产品的可用数量会出现问题。每种产品的库存都基于我的库存),然后点击添加按钮,将出现一个消息框,提示用户该产品的唯一可用数量就是这样。

这是用于识别数量是否超过:

 public void Q_tity()
    {


        try
        {
            SqlConnection cn = new SqlConnection();
            con.Open();
            string qwe = "Select MIN(Quantity/Quantifier) from Stocks where ItemID in (Select ItemID from Recipe where MenuID in (Select MenuID from Menu where ItemName ='" + txtProd.Text + "'))";


            SqlCommand cmd = new SqlCommand(qwe, con);
            dr = cmd.ExecuteReader();
            while (dr.Read())
            {
                if (txtProd.Text == "")
                {
                    MessageBox.Show("Please Input a correct format", "Format info", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else
                {


                    double val = double.Parse(dr[0].ToString());
                    Quantity = Convert.ToInt32(val);



                }
            }
        }
        catch
        {
            MessageBox.Show("The ingredient on recipe cannot identify. please fix it from the maintenance form", "Recipe Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
        con.Close();
    }

This is for identifying if the quantity exceeds.

这是应该提示用户的添加按钮

private void btnAdd_Click(object sender, EventArgs e)
    {

        if (txtQuant.Text.Length == 0)
        {
            MessageBox.Show("Quantity Field is Required!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
        else
        {
            Q_tity();
            con.Open();

          //  con.Close();
            if (Quantity < Convert.ToDouble(txtQuant.Text))
            {
                string qwe = "Select MIN(Quantity/Quantifier) from Stocks where ItemID in (Select ItemID from Recipe where MenuID in (Select MenuID from Menu where ItemName ='" + txtProd.Text + "'))";


                SqlCommand cmd = new SqlCommand(qwe, con);
                SqlDataReader dr = cmd.ExecuteReader();


                MessageBox.Show(txtQuant.Text + " is unavailable! \n The Available Quantity of  " + txtProd.Text + " is " + dr + " only!", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                con.Close();
            }

This is the add button that should prompt the user

可悲的是,这是结果:

And sadly this is the result.

1 个答案:

答案 0 :(得分:1)

在这种情况下,必须使用ExecuteScalar()而不是ExecuteReader

在这里修复

        .collect(Collectors.toCollection(ArrayList::new));