0

guys I have a field in my database having datatype BIT. I need to extract this value and perform an IF ELSE condition according to its value.

I tried getting the value as follows

   string UStat = "SELECT UserStatus FROM " + MainForm.schema + "Adm.SysUser WHERE USerId ='" + cmbUserID.Text + "'";
            cmd = ccs.CreateCommand();
            ccs.Open();
            cmd.CommandText = UStat;
            int User_stat = ((int)cmd.ExecuteScalar());
            string user_stat = Convert.ToString(User_stat);
            ccs.Close();

At the level of ExecuteScalar i am being notified of an incorrect casting. Where was I wrong guys ?

1
  • what is the datatype of UserStatus in the DB ? Commented Sep 13, 2013 at 5:32

3 Answers 3

1

If the data type is Bit, cast it to bool. That should work.

Sign up to request clarification or add additional context in comments.

Comments

0

Try the following code,

string user_stat = Convert.ToString(cmd.ExecuteScalar())
if(Convert.ToBoolean(user_stat ))
{
-----
-----
}

In side if condition, You will get either true/false

Comments

0

I got it guys ....

i solved it by modifying my codes to

  string UStat = "SELECT UserStatus FROM " + MainForm.schema + "Adm.SysUser WHERE USerId ='" + cmbUserID.Text + "'";
            cmd = ccs.CreateCommand();
            ccs.Open();
            cmd.CommandText = UStat;
            bool User_stat = ((bool)cmd.ExecuteScalar()); 
            ccs.Close();

Thanks to all that tried to help. Cheers

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.