Get a boolean value from DataTable

Developer picture Developer · Jun 26, 2009 · Viewed 27.7k times · Source

How do I retrieve a Boolean value in dataset, I'm using visual studio 2003,I am trying the following, but it's not working:

//if product inactive, don't display, and redirect to main page
  if((dbDataSet.Tables["productGeneral"].Rows[0]["Active"].Equals(0)))

I even tried, but not working:

if((dbDataSet.Tables["productGeneral"].Rows[0]["Active"].toString() == false)

the columns name is ["active"], the value with in column is either True or False, using sql server 2000

please help

Answer

Reed Copsey picture Reed Copsey · Jun 26, 2009

You need to cast to a bool directly, and just check using that.

Have you tried?:

if(((bool)dbDataSet.Tables["productGeneral"].Rows[0]["Active"] == false))

If it's a bool, you'll want to cast the result to a bool directly.

The first fails since 0 is an Int32, not a Boolean. They are not comparable in C#, since they're distinct types. The second fails since ToString() turns the result into a string, and you're comparing a string to a bool, which again will not work.