Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

DBNull.Value

2 views
Skip to first unread message

Mark

unread,
Jan 9, 2004, 2:50:11 PM1/9/04
to
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");
}

Daniel Pratt

unread,
Jan 9, 2004, 3:01:48 PM1/9/04
to
Hi Mark,

"Mark" <mfi...@idonotlikespam.cce.umn.edu> wrote in message
news:%23nuCMnu...@TK2MSFTNGP11.phx.gbl...

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

unread,
Jan 9, 2004, 3:05:41 PM1/9/04
to
Checking strApplication == null works, but why doesn't DBNull work?

Thanks!
Mark

"Mark" <mfi...@idonotlikespam.cce.umn.edu> wrote in message
news:%23nuCMnu...@TK2MSFTNGP11.phx.gbl...

Nicholas Paldino [.NET/C# MVP]

unread,
Jan 9, 2004, 3:02:27 PM1/9/04
to
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.


--
- Nicholas Paldino [.NET/C# MVP]
- m...@spam.guard.caspershouse.com

"Mark" <mfi...@idonotlikespam.cce.umn.edu> wrote in message
news:%23nuCMnu...@TK2MSFTNGP11.phx.gbl...

Mark

unread,
Jan 9, 2004, 3:13:05 PM1/9/04
to
Sorry about the lag ... got it. Thanks!

"Mark" <mfi...@idonotlikespam.cce.umn.edu> wrote in message

news:%23dXm2vu...@TK2MSFTNGP09.phx.gbl...

Daniel Pratt

unread,
Jan 9, 2004, 3:21:26 PM1/9/04
to
Hi Mark,

"Mark" <mfi...@idonotlikespam.cce.umn.edu> wrote in message

news:%23dXm2vu...@TK2MSFTNGP09.phx.gbl...


> 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


0 new messages