Get a boolean value from DataTable

冷暖自知 提交于 2019-12-10 17:31:01

问题


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


回答1:


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.




回答2:


Try out the following

   if (Convert.ToBoolean(dbDataSet.Tables["productGeneral"].Rows[0]["Active"]) == true)
   {}


来源:https://stackoverflow.com/questions/1050663/get-a-boolean-value-from-datatable

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!