Reputation: 3111
I have a Hashtable called EmployeeInformation in my ASP.Net 4.0 web page (written in C#) and I want to check if a particular item ("DateEff") in the Hashtable has a null value. When I walk through the code with the debugger, I see that EmployeeInformation["DateEff"] has a value of {Null}. However, when I get to this condition:
if (EmployeeInformation["Context"].ToString() != "Existing" && EmployeeInformation["DateEff"] != null && EmployeeInformation["DateEff"].ToString() != "")
<do something here>
my code sees that all three conditions are true and continues on to the "do something here" line of code. I've tried using
string.IsNullOrEmpty(EmployeeInformation["DateEff"])
also, but the result is the same. I have also checked that EmployeeInformation contains the key DateEff and it does. Does anyone know why this is happening?
Based on MethodMan's comment below, I went back to see how I was populating the Hashtable. Here's what I'm doing:
string strDateHired = txtDateHired.Text;
if (strDateHired == string.Empty)
dtDateHired = SqlDateTime.Null;
else
dtDateHired = Convert.ToDateTime(strDateHired);
EmployeeInformation.Add("DateEff", dtDateHired);
So the problem is that my value isn't null, it's SqlDateTime.Null. But how do I test for that? I can't convert EmployeeInformation["DateEff"] to a DateTime because it's empty.
Upvotes: 0
Views: 898
Reputation: 3030
I think (based on your clarification) Convert.IsDBNull(DateEff)
will be true. You are setting that value to DBNull, not null as defined by c#.
Fun, right!?
Upvotes: 1
Reputation: 137
If you ask for a bogus field that you know is never populated, does <do something here>
ever run? If so, I would question if DataEff is getting populated when you aren't expecting it to be.
Upvotes: 0