c# - best way to prevent Null failure with string casting -


_callreportcode = reader["call report code"].tostring(); 

i attempting handle possibility object calling tostring on null. going using above statement several variables , dont want make individual try/catch each one... best way null checking strings.

other datatypes ive been doing this:

int.tryparse(reader["account number"].tostring(), out _accountnumber); 

in code "reader" refers sqldatareader thats not important question.

use null-coalescing operator: ??

callreportcode = (reader["call report code"] ?? "").tostring(); 

if data in field dbnull.value (rather null), still work, because dbnull.value not null, ?? won't used, , dbnull.value.tostring() "", you'd want.


Comments

Popular posts from this blog

monitor web browser programmatically in Android? -

Shrink a YouTube video to responsive width -

wpf - PdfWriter.GetInstance throws System.NullReferenceException -