title: Need a concise way to evaluate NULL when assigning IDataRecord values
body: <p>I have a FillDataRecord method that assigns values to an object from an IDataRecord.  It works until it encounters a field with a NULL value, at which point it breaks with the message, "Data is Null. This method or property cannot be called on Null values."</p>
  
  <p>The workaround is to use IDataRecord.IsDBNull, which I've done, but I'd like to make this cleaner.  Here is the code with some relevant comments.</p>
  
  <pre><code>private static Employee FillDataRecord(IDataRecord dataRecord)
  {
      Employee employee = new BusinessEntities.Employee();
      employee.EmployeeID = dataRecord.GetInt32(dataRecord.GetOrdinal("EmployeeID"));
      // Other fields omitted for brevity...
  
      // This breaks when StreetLine2 is NULL.
      employee.StreetLine2 = dataRecord.GetString(dataRecord.GetOrdinal("StreetLine2"));
  
      // This is my first workaround, which fixes the above error but is verbose.
      if (dataRecord.IsDBNull(dataRecord.GetOrdinal("StreetLine2")))
          employee.StreetLine2 = "";
      else
          employee.StreetLine2 = dataRecord.GetString(dataRecord.GetOrdinal("StreetLine2"));
  
      // This is my second workaround, which uses a custom method shown below.
      // But it requires casting.
      employee.StreetLine2 = (string)setDataRecordSafely(dataRecord, "StreetLine2");
  
      // Other fields omitted for brevity...
      return employee;
  }
  </code></pre>
  
  <p>Here is the method that I wrote to handle NULL values.  This gets called by the second workaround shown above.</p>
  
  <pre><code>public static object setDataRecordSafely(IDataRecord dataRecord, string fieldName)
  {
      int fieldIndex = dataRecord.GetOrdinal(fieldName);
      bool isFieldNull = dataRecord.IsDBNull(fieldIndex);
  
      Type fieldType = dataRecord.GetFieldType(fieldIndex);
      switch (Type.GetTypeCode(fieldType))
      {
          case TypeCode.String:
              return isFieldNull ? "" : dataRecord.GetString(fieldIndex);
          case TypeCode.Int32:
              return isFieldNull ? 0 : dataRecord.GetInt32(fieldIndex);
          case TypeCode.Boolean:
              return isFieldNull ? false : dataRecord.GetBoolean(fieldIndex);
          // TODO: Extend to handle other types as required.
      }
  
      return null; // The type wasn't handled.
  }
  </code></pre>
  
  <p>Is there a way to overload that setDataRecordSafely() method to return the appropriate System.Type so that I don't have to cast the returned value?  What I'd like to avoid is this type of casting within the FillDataRecord() method.</p>
  
  <pre><code>employee.StreetLine2 = (string)setDataRecordSafely(dataRecord, "StreetLine2");
  employee.City = (string)setDataRecordSafely(dataRecord, "City");
  employee.StateID = (int)setDataRecordSafely(dataRecord, "StateID");
  </code></pre>
  
  <p>Or, is there a better way to handle NULL when assigning column values via an IDataReader?  Thanks for your help.</p>
  
  <p>=== Edit 12/31/2014 at 1:45 PM Central ===</p>
  
  <p>Thanks @Rhumborl and @Jeff Mercado. I ran with the Extension Methods you suggested, and my solution is below.  Props for the paragraph 2 guidance you provided Jeff.  Here is my class.</p>
  
  <pre><code>public static class IDataRecordExtensions
  {
  
      /// &lt;summary&gt;
      /// Extension that gets the field's string value, or transforms null into an empty string.
      /// &lt;/summary&gt;
      public static string GetString(this IDataRecord dataRecord, string fieldName)
      {
          int fieldIndex = dataRecord.GetOrdinal(fieldName);
          bool isFieldNull = dataRecord.IsDBNull(fieldIndex);
          return isFieldNull ? string.Empty : dataRecord.GetString(fieldIndex);
      }
  
      /// &lt;summary&gt;
      /// Extension that gets the field's int value, or transforms null into 0.
      /// &lt;/summary&gt;
      public static int GetInt32(this IDataRecord dataRecord, string fieldName)
      {
          int fieldIndex = dataRecord.GetOrdinal(fieldName);
          bool isFieldNull = dataRecord.IsDBNull(fieldIndex);
          return isFieldNull ? 0 : dataRecord.GetInt32(fieldIndex);
      }
  
      /// &lt;summary&gt;
      /// Extension that gets the field's bool value, or transforms null into false.
      /// &lt;/summary&gt;
      public static bool GetBoolean(this IDataRecord dataRecord, string fieldName)
      {
          int fieldIndex = dataRecord.GetOrdinal(fieldName);
          bool isFieldNull = dataRecord.IsDBNull(fieldIndex);
          return isFieldNull ? false : dataRecord.GetBoolean(fieldIndex);
      }
  
  }
  </code></pre>
  
  <p>And here is the implementation, after importing the class namespace via a using statement.</p>
  
  <pre><code>public static Employee FillDataRecord(IDataRecord dataRecord)
  {
      Employee employee = new BusinessEntities.Employee();
  
      employee.EmployeeID = dataRecord.GetInt32("EmployeeID");
      employee.FirstName = dataRecord.GetString("FirstName");
      employee.LastName = dataRecord.GetString("LastName");
      employee.Title = dataRecord.GetString("Title");
      employee.Email = dataRecord.GetString("Email");
      employee.StreetLine1 = dataRecord.GetString("StreetLine1");
      employee.StreetLine2 = dataRecord.GetString("StreetLine2");
      employee.City = dataRecord.GetString("City");
      employee.StateID = dataRecord.GetInt32("StateID");
      employee.ZipCode = dataRecord.GetString("ZipCode");
      employee.CountryID = dataRecord.GetInt32("CountryID");
      employee.IsDeleted = dataRecord.GetBoolean("IsDeleted");
  
      // Above is in lieu of this syntax, which doesn't handle null.
      // employee.Email = dataRecord.GetString(dataRecord.GetOrdinal("Email"));
  
      return employee;
  }
  </code></pre>