Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Checking for nulls

Sorry for such simple question.

How would I check this for nulls?

obj.DivisionNotes = (string)row["DivisionNotes"];

I'm thinking something like this.

obj.DivisionNotes = (string)row["DivisionNotes"]?null:"No notes";

Am I right.

Any help much appreciated.

like image 421
Chin Avatar asked Sep 14 '26 05:09

Chin


1 Answers

Your casting of a null will cause a problem, you can use an as cast along with the null coalescing operator to solve your issues..

obj.DivisionNotes = (row["DivisionNotes"] as string) ?? "No notes";
like image 75
Quintin Robinson Avatar answered Sep 15 '26 20:09

Quintin Robinson



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!