The best way to prevent null failure in line casting

_callReportCode = reader["Call Report Code"].ToString(); 

I am trying to handle the possibility for an object that I am calling ToString so that it is NULL. I am going to use the above statement with several variables, and I don't want to do an individual try / catch for each of them ... which is the best way to do a null string check.

Other data types do this:

 int.TryParse(reader["Account Number"].ToString(), out _accountNumber); 

In this code, the β€œreader” refers to SqlDataReader, but this is not very important for this issue.

+7
source share
11 answers

Use the zero-connected operator : ??

 callReportCode = (reader["Call Report Code"] ?? "").ToString(); 

If the data in your DBNull.Value field (and not null ), this will still work, because DBNull.Value not null , so ?? will not be used, but DBNull.Value.ToString() "" is what you need.

+29
source
 Convert.ToString(reader["Call Report Code"]); 

It will return string.Empty if the value is null.

Source: http://msdn.microsoft.com/en-us/library/astxcyeh.aspx

Update: it also works with DBNull , I just confirmed.

Update 2 . I decided to bring a more complete test here, just to make sure:

 DBNull dbNull = null; DBNull dbNullEmpty = DBNull.Value; string stringNull = null; string stringEmpty = string.Empty; var outcome1 = Convert.ToString(dbNull);//Empty string var outcome2 = Convert.ToString(dbNullEmpty);//Empty string var outcome3 = Convert.ToString(stringNull);//NULL var outcome4 = Convert.ToString(stringEmpty);//Empty string 
+10
source

If your row is NULL, you need to check the value returned from SqlDataReader on DBNull.Value :

 _callReportCode = reader["Call Report Code"] as string; 

If the object returned by reader["Call Report Code"] is not a string , it is DBNull.Value , so the as cast will set _callReportCode to null .

If you must set the string to null, if there is no database value, add ?? , eg:

 _callReportCode = (reader["Call Report Code"] as string) ?? string.Empty; 
+3
source

My suggestion is to never convert ToString when the data is not a string, and if the data is already a string, then calling ToString is redundant, and casting is required for everyone.

I am assuming that the data type in the database is an integer, in which case you can use a nullable int.

 int? accountNumber = reader["Account Number"] == DBNull.Value ? null : (int?)reader["Account Number"]; 

I made an extension method to do just this thing.

 public static class SqlDataReaderExtensions { public static T Field<T>(this SqlDataReader reader, string columnName) { object obj = reader[columnName]; if (obj == null) { throw new IndexOutOfRangeException( string.Format( "reader does not contain column: {0}", columnName ) ); } if (obj is DBNull) { obj = null; } return (T)obj; } } 

Using

 int? accountType = reader.Field<int?>("Account Number"); // will return NULL or the account number. 
+3
source

The easiest way I've found is

 _callReportCode = reader["Call Report Code"] + ""; 
+1
source

I have the simplest and most common method.

  public static string ToNULLString(this string Values) { if (string.IsNullOrEmpty(Values)) { return ""; } else { return Values.ToString(); } } 

use in c #

 string item = null; string value = item.ToNULLString(); 
+1
source

_callReportCode = Convert.ToString (reader ["Call Report Code"]) must ensure that there is no null code there.

0
source

you can create a method that you call when you want to check. That way you have to enter try catch only once ... or you can create an extension method for the string class to do this

0
source

Use the following line of code:

 _callReportCode = String.IsNullorEmpty(reader["Call Report Code"]) ? String.Empty : reader["Call Report Code"].ToString(); 

instead of the following line:

 _callReportCode = reader["Call Report Code"].ToString(); 
0
source

I like to use a combination of the null coalescence operator and the null conditional operator :

 string nn = MyObject.myNullableVar?.ToString() ?? ""; 

he is basically the same as this

 string ss = (MyObject.MyNullableVar == null) ? "" : MyObject.MyNullableVar.ToString(); 

but shorter.

0
source

You can check with String.IsNullOrEmpty() to make sure it is not empty, or you could write an extension method to perform some action if it is not null and another / nothing if it is.

-one
source

All Articles