Anoushka Seechurn
Anoushka Seechurn

Reputation: 2256

Casting datatype c#

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 ?

Upvotes: 0

Views: 155

Answers (3)

Anoushka Seechurn
Anoushka Seechurn

Reputation: 2256

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

Upvotes: 0

Saibal
Saibal

Reputation: 812

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

Upvotes: 1

ZubinAmit
ZubinAmit

Reputation: 76

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

Upvotes: 0

Related Questions