1

I want to read NULL values from database, if its not NULL I want to make the checkboxes checked but this code is not working.

if (dr["p51"] != null)
{
chkP51.Checked = true;
}
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Kevin Rodriguez
  • 181
  • 2
  • 3
  • 15

1 Answers1

4

Check the values null by using DataRow.IsNull(string columnName). Change your code like this.

if (!dr.IsNull("p51"))
{
    chkP51.Checked = true;
}

Finally from @Kevin Rodriguez suggestion,because dr["p51"] return 0

if ((string)dr["p51"] == "0")
{
    chkP51.Checked = true;
}