0

I have a select like this

object endTime = null;
string selectCommandText = "SELECT endtime FROM alapplication WHERE applicationid='" + application + "'";
DbProviderFactory dbf = DbProviderFactories.GetFactory("Npgsql");
using (IDbConnection conn = dbf.CreateConnection())
{
     conn.ConnectionString = ASSettings.ConnectionInfo_MASTER;
     conn.Open();
     IDbCommand selectCommand = conn.CreateCommand();
     selectCommand.CommandText = selectCommandText;
     IDataReader reader = selectCommand.ExecuteReader();
     if (reader.Read())
     {
         endTime = reader.GetValue(reader.GetOrdinal("endtime"));
     }
     reader.Close();
     conn.Close();
     conn.Dispose();
}

where i want to read the value endtime which is of type timezone. The problem is that sometimes this value is null and then when i read it i have that endtime is like this

{}

so its like empty, NEITHER NULL NOR LIKE "". I don't know how to check whether it is like this or not, because if it is not like this that i can try to parse it to datetime like this

DateTime expireDate = DateTime.Parse(endTime.ToString());

For now I change the query to

string selectCommandText = "SELECT endtime FROM alapplication WHERE applicationid='" + application + "' AND endtime IS NOT NULL";

so if in the db endtime is null i will not get anything.. But I wanted to understand this thing because it happened to me other times..

ayasha
  • 1,131
  • 3
  • 23
  • 43
  • I'm just throwing ideas out there, but can you perhaps read it as a Nullable Datetime? (questionmark-pun unintended). I would think that it would attempt to parse it to a DateTime, and return null if it fails. Although not sure. – Falgantil Nov 06 '15 at 13:22
  • 1
    I believe you're looking for [DBNull.Value](https://msdn.microsoft.com/en-us/library/system.dbnull.value%28v=vs.110%29.aspx). You'd read your value and check it against DBNull.Value like this: `if(!DBNull.Value.Equals(myVal)) [...]` – germi Nov 06 '15 at 13:33

0 Answers0