By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,885 Members | 1,499 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,885 IT Pros & Developers. It's quick & easy.

DBNull.Value

P: n/a
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");
}

Nov 15 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
Hi Mark,

"Mark" <mf****@idonotlikespam.cce.umn.edu> wrote in message
news:%2****************@TK2MSFTNGP11.phx.gbl...
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
Nov 15 '05 #2

P: n/a
Checking strApplication == null works, but why doesn't DBNull work?

Thanks!
Mark

"Mark" <mf****@idonotlikespam.cce.umn.edu> wrote in message
news:%2****************@TK2MSFTNGP11.phx.gbl...
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");
}

Nov 15 '05 #3

P: n/a
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]
- mv*@spam.guard.caspershouse.com

"Mark" <mf****@idonotlikespam.cce.umn.edu> wrote in message
news:%2****************@TK2MSFTNGP11.phx.gbl...
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");
}

Nov 15 '05 #4

P: n/a
Sorry about the lag ... got it. Thanks!

"Mark" <mf****@idonotlikespam.cce.umn.edu> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
Checking strApplication == null works, but why doesn't DBNull work?

Thanks!
Mark

"Mark" <mf****@idonotlikespam.cce.umn.edu> wrote in message
news:%2****************@TK2MSFTNGP11.phx.gbl...
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");
}


Nov 15 '05 #5

P: n/a
Hi Mark,

"Mark" <mf****@idonotlikespam.cce.umn.edu> wrote in message
news:%2****************@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
Nov 15 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.