DBNull.Value

  • Thread starter Thread starter Mark
  • Start date Start date
M

Mark

Assume that "some_app_setting" does not exist in my web.config file. Then,
the first line of code below assigns the value null to strApplication. Why
doesn't my IF statement return true, and therefore execute the
Response.Write statement? Is there an alternative way to compare it to
null?

Thanks!
Mark

String strApplication=
ConfigurationSettings.AppSettings["some_app_setting"];

if (DBNull.Value.Equals(strApplication))
{
Response.Write("This should print because strApplication is null");
}
 
Hi Mark,

Mark said:
Assume that "some_app_setting" does not exist in my web.config file. Then,
the first line of code below assigns the value null to strApplication. Why
doesn't my IF statement return true, and therefore execute the
Response.Write statement? Is there an alternative way to compare it to
null?

Thanks!
Mark

String strApplication=
ConfigurationSettings.AppSettings["some_app_setting"];

if (DBNull.Value.Equals(strApplication))
{
Response.Write("This should print because strApplication is null");
}

DBNull is used to represent NULL values in a database table. Although
the term for the value of an unassigned reference is also "null", they are
not the same thing. Suggest:

...
if (strApplication == null)
{
...
}

Regards,
Dan
 
Mark,

Why are you comparing it against DBNull.Value? Why not just compare it
to null? DBNull is used for database null comparisons (for the most part).

Hope this helps.
 
Sorry about the lag ... got it. Thanks!

Mark said:
Checking strApplication == null works, but why doesn't DBNull work?

Thanks!
Mark

Mark said:
Assume that "some_app_setting" does not exist in my web.config file. Then,
the first line of code below assigns the value null to strApplication. Why
doesn't my IF statement return true, and therefore execute the
Response.Write statement? Is there an alternative way to compare it to
null?

Thanks!
Mark

String strApplication=
ConfigurationSettings.AppSettings["some_app_setting"];

if (DBNull.Value.Equals(strApplication))
{
Response.Write("This should print because strApplication is null");
}
 
Hi Mark,

Mark said:
Checking strApplication == null works, but why doesn't DBNull work?

Thanks!
Mark

Just to be explicit: A reference variable having the value of "null"
means that the variable does not reference an object instance. That is, a
reference variable can reference an object instance OR be "null", not both.
DBNull.Value is a reference to an object instance (of type DBNull,
confusingly enough). Therefore DBNull.Value is not "null". Don't get hung up
on the fact that DBNull contains the term "Null". Databases and programming
languages happen to use the same term to represent similar concepts.

Regards,
Dan
 
Back
Top