briskovich
briskovich

Reputation: 690

compare Object to variable in an if statement c#

I have a query that will return a value in this case and inventory category. If the user enters a certain box number then the form should disable if it is anything else the form will enable. The problem is I use the ExecuteScalar and need to compare that value to a string "AP". I am pretty new to C# and cannot figure out how to do this. Here is my code so far:

private void hide_apple_controls()
{
        SqlCommand cmd = new SqlCommand();
        Object invCat;
        cmd.CommandText = "SELECT itm.inv_cat FROM " +
                          "t_item_master itm " +
                          "LEFT OUTER JOIN  t_box box (NOLOCK) ON box.product_code LIKE itm.item_number " +
                          "where box.box_id = @box_id ";

        cmd.Parameters.Add("@box_id", SqlDbType.VarChar).Value = boxBox.Text;

        cmd.CommandType = CommandType.Text;
        cmd.Connection = this.sqlConnection1;
        this.sqlConnection1.Open();
        SqlDataAdapter adpt = new SqlDataAdapter(cmd);
        DataSet ds = new DataSet();
        adpt.Fill(ds);
        invCat = cmd.ExecuteScalar();          
        this.sqlConnection1.Close();

        string ap = "AP";

        if (invCat == ap ) 
        {
            disable_qc_form();
        }

        enable_qc_form();
}

Upvotes: 0

Views: 187

Answers (1)

Madushan
Madushan

Reputation: 7468

in the if condition, you need the else clause.

if (invCat == ap ) 
   disable_qc_form();
else
   enable_qc_form();

Without the else , your last line will execute no matter what. With the else it will execute only if the invCat == ap doesn't evaluate to true.

Upvotes: 4

Related Questions