title: Nullable DateTime with SQLDataReader
body: <p>I almost hate to ask this question seems like it has been asked a million times before but even with me researching the other question I still cant seem to figure this out in my case. </p>
  
  <p>I read that DateTime is a nullable type and I tried a few of the examples but I am trying to figure out if it is NULL in the database my SQLDATAREADER is failing.</p>
  
  <p><strong>Error</strong></p>
  
  <blockquote>
    <p>System.Data.SqlTypes.SqlNullValueException: Data is Null. This method or property cannot 'be called on Null values.'</p>
  </blockquote>
  
  <p><strong>DetailsClass</strong></p>
  
  <pre><code>private DateTime? startingDate;
  
  public DateTime? StartingDate
  {
      get{ return startingDate; }
      set{ startingDate = value; }
  }
  
  // constructor
  Public DetailsClass(DateTime? startingDate)
  {
      this.startingDate = startingDate;
  }
  </code></pre>
  
  <p><strong>DBClass</strong></p>
  
  <pre><code>   using (SqlConnection con = new SqlConnection(connectionString))
              using (SqlCommand cmd = con.CreateCommand())
              {
  
                  List&lt;DetailsClass&gt; details = new List&lt;DetailsClass&gt;();
                  DetailsClass dtl;
                  try
                  {
                      con.Open();
                      cmd.CommandText = "Stored Procedure Name";
                      cmd.CommandType = CommandType.StoredProcedure;
                      cmd.Parameters.AddWithValue("@MyParameter", myparameter);
  
                      using (SqlDataReader reader = cmd.ExecuteReader())
                      {
                          while (reader.Read())
                          {
                              dtl = new DetailsClass((
                                  reader.GetInt32(reader.GetOrdinal("MEMBERSHIPGEN"))),
                                  reader.IsDBNull(1) ? null : reader.GetString(reader.GetOrdinal("EMAIL")),
                                  reader.GetDateTime(reader.GetOrdinal("STARTINGDATE")));
  
  
                              details.Add(dtl);
                          }
                          reader.Close();
                          return details;
  
                      }
                  }
  </code></pre>