Neil Knight
Neil Knight

Reputation: 48547

Cleanest way to load a BIT into a CheckBox

I am loading BIT datatype from the database to populate a CheckBox. Currently I have:

chkDependencies.Checked = (objRdr["BitColumn"].ToString().ToLower() == "true" ? true : false);

objRdr is a SqlDataReader.

This looks messy and I was wondering if there is a cleaner way to load the BIT data?

Upvotes: 0

Views: 1573

Answers (3)

Antonio Bakula
Antonio Bakula

Reputation: 20693

You can use Field extension method for DataRow

chkDependencies.Checked = dataRow.Field<bool>("BitColumn");

Upvotes: 3

Jon Rea
Jon Rea

Reputation: 9455

If the underlying type of you 'BIT' is a string, then this will work:

chkDependencied.Checked = bool.Parse(objRdr["BitColumn"]);

If the underlying type of you 'BIT' is a boolean, then this will work:

chkDependencied.Checked = (bool)objRdr["BitColumn"];

This assumes that objReader is some sort of keyed dictionary typed to contain base-object i.e.:

Dictionary<string, object>

Upvotes: 0

freefaller
freefaller

Reputation: 19953

Assuming the BitColumn is an actual bit field in the database (and not null)...

chkDependencied.Checked = (bool)objRdr["BitColumn"];

Upvotes: 1

Related Questions