运算符'=='不能应用于'方法组'和'int'类型的操作数

时间:2014-08-04 06:13:34

标签: c# sql sql-server-2014

我正在写一篇关于公司股票的计划。

我在int中有可变数量的库存,我试图获取所有数量并与文本框上的结果进行比较。

我使用以下代码:

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

1 个答案:

答案 0 :(得分: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");
}

标记这是否有用