ExecuteScalar()
sometimes returns empty object -not null- although the record is exists. When I analyze this object with quickwatch, I see that object.GetType()
is equal to DbNull
.
I can handle this empty object but I need to know why it is returns empty object sometimes although the record is exists.
string sql = @"SELECT SentDate
FROM dbo.EmailOut
WHERE ID = @ID";
SqlCommand cmd = new SqlCommand(sql, _cnn);
cmd.CommandType = System.Data.CommandType.Text;
cmd.Parameters.Add(new SqlParameter("@ID", ID));
object obj = cmd.ExecuteScalar();
if (obj == null)
return false
sentDate = (DateTime)obj;
cmd.Dispose();
Most of the time my query runs perfectly. Can you please check my code?