Hi,
Which is the best way to prevent this from throwing an exception, e.g. if it's null
C#
(bool)(aDataTable.Rows[0]["aColumn"]))Thanks
Hi,
Which is the best way to prevent this from throwing an exception, e.g. if it's null
(bool)(aDataTable.Rows[0]["aColumn"]))Thanks
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Jignesh KumarRank #91 · 22.9K reputationPosted Oct 11, 2022, 12:03 AM EDT
Hi Jonson Bill,
Please check this one, incase your column has null vaue then it will return false.
bool b = bool.TryParse(aDataTable.Rows[0]["aColumn"], out b);Vishal YelveRank #178 · 13.6K reputationPosted Oct 11, 2022, 1:25 AM EDT
use
Boolean.TryParse()
Boolean.TryParse( aDataTable.Rows[0]["aColumn"], out result );Uttam KumarRank #1,076 · 1.8K reputationPosted Oct 11, 2022, 2:35 AM EDT
bool result = false; Boolean.TryParse(aDataTable.Rows[0]["aColumn"], out result); Console.WriteLine(result);You can use this code to avoid the exception.
Rajanikant HawaldarRank #110 · 20.2K reputationPosted Oct 11, 2022, 2:52 AM EDT
bool result = false; bool b = Boolean.TryParse(((string)(aDataTable?.Rows?[0]["aColumn"])), out result);