Click here to Skip to main content
15,881,872 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
How do I cast object to Boolean value from database.

this is throwing invalid case excetion.

emp.IsActive = (bool)rdr["Is_Active"];
emp.IsActive = Convert.ToBoolean(rdr["Is_Active"]);


What I have tried:

emp.IsActive = (bool)rdr["Is_Active"];
emp.IsActive = Convert.ToBoolean(rdr["Is_Active"]);
Posted
Updated 19-Sep-17 17:41pm
Comments
[no name] 15-Sep-17 13:36pm    
tried this one also.
var i = rdr.GetOrdinal("Is_Active");
emp.IsActive = rdr.GetBoolean(i);

1 solution

check Boolean.TryParse Method (String, Boolean) (System)[^]
bool isActive = false;
          if (bool.TryParse(rdr["Is_Active"] + "", out isActive)) {
              emp.IsActive = isActive
          }
          else
          {
             // show the error message to the user
          }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900