473,394 Members | 1,645 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

DBNull.Value

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
5 13571
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: DraguVaso | last post by:
Hi, Something I don't understand about a Typed DataSet: When a value in the DataSet is DBNull, it throws this error: "Cannot get value because it is DBNull". But aren't Typed DataSets...
11
by: Patrick.O.Ige | last post by:
When i try and use this (Where Unit is a column in my Table):- If Unit Is DBNull.Value Then Return "1" Else Return "2" End If I always have 2 returned! Even when Unit is NULL! I want a...
4
by: Dursun | last post by:
Hi, I am trying to assign NULL to a datetime field in the SQL Server database. Here is the code that does NOT work: INSERT INTO ... .... VALUES ... .... CType(IIf(dateWitness2Date.Checked,...
8
by: MattB | last post by:
Hello. I have a vb.net (asp.net) application that uses ado.net datasets. At one point, I need to check a text field in a DataTable to see if there's any text in it before performing text operations...
6
by: tshad | last post by:
I have a value coming from my Object (or it could also be from a SqlDbReader) where I need to test for DBNull and 0. I tried to do it in one call: if (not (newPosition.ReportsTo is...
2
by: Julian | last post by:
I have the following code: Dim strQuery As String = "SELECT tblData.CoPayFundGrant, tblData.M_DSS_SSA_Benefits, tblData.O_DSS_SSA_Benefits, " & _ "tblData.Ref_MPS, tblData.Ref_LA,...
6
by: Charlie Brown | last post by:
When checking for NULL values from a database, normally I would use If Not row("NetSales") Is DBNull.Value Then NetSales = row("NetSales") End If However, if I use a typed dataset then I...
19
by: Dave | last post by:
If Iwant to check if dataset1.SelectQuery1.column1 == System.DBNull.Value. How do I do this? What I wrote above will give an error. -- L. A. Jones
6
by: scott ocamb | last post by:
Hello, I have a function that expects the following parms. How can I handle null values. For example, if FirstName comes through as null, how can i propogate that null value to the stored...
3
by: Finn Stampe Mikkelsen | last post by:
Hi I have defined a table in my database, with 2 date-fields. I have set a default value to DBNull. I have integrated a nullable datetimepicker control to my project and set the apropriate...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.