-4

I am writing to develop a program concerning stock of a firm.

I have variable quantity in stock in int, ı am trying to get all quantity and compare with the result written on textbox.

I am using the following code:

SqlConnection sqlcon = new SqlConnection("server=.;database=DB_TEST;trusted_connection=true"); - SERVER CONNECTION


SqlCommand sqlcmd = new SqlCommand("Select SUM(Quantity) from Stock where ProductID='" + cmbxProductID.SelectedValue + "'", sqlcon); - GETTING TOTAL QUANTITY FROM DATABASE - quantity in int

sqlcon.Open(); - SQL CONNECTION OPEN

textBox1.Text = sqlcmd.ExecuteScalar().ToString(); -- transfering total quantity to textbox1

int result=int.Parse(textBox1.Text); -- CONVERT CONTENT OF TEXTBOX to ınt

if (result == 0)
        {
            MessageBox.Show("Stock is not adequate");
        }

        sqlcmd.ExecuteNonQuery();

        sqlcon.Close()

        MessageBox.Show("Stock is OK");
4

1 回答 1

0

您需要将结果传递给变量而不是 textBox1。然后你可以比较变量值 & textBox1 值

SqlConnection sqlcon = new SqlConnection("server=.;database=DB_TEST;" + 
                                     "trusted_connection=true");
SqlCommand sqlkmt = new SqlCommand("Select SUM(QUANTITY) from Stock " + 
                                   "where ProductID='" + 
                                    cmbxProductID.SelectedValue + "'",  sqlcon);                       
sqlcon.Open();
int result= sqlkmt.ExecuteScalar();
sqlcon.Close();
if (result == int.Parse(textBox1.Text))
{
    MessageBox.Show("Stock in not adeqaute");
}

标记这是否有帮助

于 2014-08-04T06:33:53.123 回答