473,414 Members | 1,628 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,414 software developers and data experts.

Detecting "Null" database fields

I'm using SQL Server 2000 and on my page, I'm simply creating a
SQLDataReader and filling in Labels with the retrieved (single) record.
However, how can I prevent from getting errors when a field is null?
I've tried something like this (experimenting with IsDBNull):
================================================== =======
If dtrData.IsDBNull("Address2") = True Then
lblAddress2.Text = ""
Else
lblAddress2.Text = dtrData("Address2")
End If

And this....

If IsNothing(dtrDatal("Address2")) = True Then
lblAddress2.Text = ""
Else
lblAddress2.Text = dtrData("Address2")
End If
================================================== ======
...and neither seem to work. Any suggestions? Thanks!


Nov 18 '05 #1
9 1994
You can use

Select ISNULL(Address2, '') AS Address2 FROM MyTable

in your SELECT statement or just use this when reading the data:

lblAddress2.Text = "" + dtrData("Addres2")

Dale

"D. Shane Fowlkes" <sh**********@h-o-t-m-a-i-l.com> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
I'm using SQL Server 2000 and on my page, I'm simply creating a
SQLDataReader and filling in Labels with the retrieved (single) record.
However, how can I prevent from getting errors when a field is null?
I've tried something like this (experimenting with IsDBNull):
================================================== =======
If dtrData.IsDBNull("Address2") = True Then
lblAddress2.Text = ""
Else
lblAddress2.Text = dtrData("Address2")
End If

And this....

If IsNothing(dtrDatal("Address2")) = True Then
lblAddress2.Text = ""
Else
lblAddress2.Text = dtrData("Address2")
End If
================================================== ======
..and neither seem to work. Any suggestions? Thanks!

Nov 18 '05 #2
My code is normally more like so (C#)

if(dr["field"] == DBNull.Value)
statement;
else
statement;

Which equates to (VB.NET)

If (dr("field") == DBNull.Value) Then
Else
End If

As suggested, you can also do this in the SQL statement if you want to fill
with a string, as you are doing.

--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

************************************************** ********************
Think Outside the Box!
************************************************** ********************
"D. Shane Fowlkes" <sh**********@h-o-t-m-a-i-l.com> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
I'm using SQL Server 2000 and on my page, I'm simply creating a
SQLDataReader and filling in Labels with the retrieved (single) record.
However, how can I prevent from getting errors when a field is null?
I've tried something like this (experimenting with IsDBNull):
================================================== =======
If dtrData.IsDBNull("Address2") = True Then
lblAddress2.Text = ""
Else
lblAddress2.Text = dtrData("Address2")
End If

And this....

If IsNothing(dtrDatal("Address2")) = True Then
lblAddress2.Text = ""
Else
lblAddress2.Text = dtrData("Address2")
End If
================================================== ======
..and neither seem to work. Any suggestions? Thanks!

Nov 18 '05 #3
Try:

if (your parameter name) is System.DBNull.Value then
lblAddress2.text = ""
else
lblAddress2.text = (your parameter name)
end if
"D. Shane Fowlkes" <sh**********@h-o-t-m-a-i-l.com> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
I'm using SQL Server 2000 and on my page, I'm simply creating a
SQLDataReader and filling in Labels with the retrieved (single) record.
However, how can I prevent from getting errors when a field is null?
I've tried something like this (experimenting with IsDBNull):
================================================== =======
If dtrData.IsDBNull("Address2") = True Then
lblAddress2.Text = ""
Else
lblAddress2.Text = dtrData("Address2")
End If

And this....

If IsNothing(dtrDatal("Address2")) = True Then
lblAddress2.Text = ""
Else
lblAddress2.Text = dtrData("Address2")
End If
================================================== ======
..and neither seem to work. Any suggestions? Thanks!

Nov 18 '05 #4
Modifying my Select would be a sloppy hack...seems like it would anyway.

I did get this to work:

If IsDBNull(dtrData("Address2")) Then
lblAddress2.Text = ""
Else
lblAddress2.Text = dtrData("Address2")
End If
Thanks guys.
"Cowboy (Gregory A. Beamer)" <No************@comcast.netNoSpamM> wrote in
message news:O5**************@TK2MSFTNGP11.phx.gbl...
My code is normally more like so (C#)

if(dr["field"] == DBNull.Value)
statement;
else
statement;

Which equates to (VB.NET)

If (dr("field") == DBNull.Value) Then
Else
End If

As suggested, you can also do this in the SQL statement if you want to fill with a string, as you are doing.

--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

************************************************** ********************
Think Outside the Box!
************************************************** ********************
"D. Shane Fowlkes" <sh**********@h-o-t-m-a-i-l.com> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
I'm using SQL Server 2000 and on my page, I'm simply creating a
SQLDataReader and filling in Labels with the retrieved (single) record.
However, how can I prevent from getting errors when a field is null?
I've tried something like this (experimenting with IsDBNull):
================================================== =======
If dtrData.IsDBNull("Address2") = True Then
lblAddress2.Text = ""
Else
lblAddress2.Text = dtrData("Address2")
End If

And this....

If IsNothing(dtrDatal("Address2")) = True Then
lblAddress2.Text = ""
Else
lblAddress2.Text = dtrData("Address2")
End If
================================================== ======
..and neither seem to work. Any suggestions? Thanks!


Nov 18 '05 #5
I wouldn't call ISNULL a sloppy hack. There's a reason they built the
functionality into SQL. The advantage is that, if it's in a stored
procedure, any app that calls the stored proc gets the same handling and
gives a consistent result. Also, it is one place to change the code, rather
than in every app.

Just my opinion.

Dale

"D. Shane Fowlkes" <sh**********@h-o-t-m-a-i-l.com> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
Modifying my Select would be a sloppy hack...seems like it would anyway.

I did get this to work:

If IsDBNull(dtrData("Address2")) Then
lblAddress2.Text = ""
Else
lblAddress2.Text = dtrData("Address2")
End If
Thanks guys.
"Cowboy (Gregory A. Beamer)" <No************@comcast.netNoSpamM> wrote in
message news:O5**************@TK2MSFTNGP11.phx.gbl...
My code is normally more like so (C#)

if(dr["field"] == DBNull.Value)
statement;
else
statement;

Which equates to (VB.NET)

If (dr("field") == DBNull.Value) Then
Else
End If

As suggested, you can also do this in the SQL statement if you want to

fill
with a string, as you are doing.

--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

************************************************** ********************
Think Outside the Box!
************************************************** ********************
"D. Shane Fowlkes" <sh**********@h-o-t-m-a-i-l.com> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
I'm using SQL Server 2000 and on my page, I'm simply creating a
SQLDataReader and filling in Labels with the retrieved (single) record. However, how can I prevent from getting errors when a field is null?
I've tried something like this (experimenting with IsDBNull):
================================================== =======
If dtrData.IsDBNull("Address2") = True Then
lblAddress2.Text = ""
Else
lblAddress2.Text = dtrData("Address2")
End If

And this....

If IsNothing(dtrDatal("Address2")) = True Then
lblAddress2.Text = ""
Else
lblAddress2.Text = dtrData("Address2")
End If
================================================== ======
..and neither seem to work. Any suggestions? Thanks!



Nov 18 '05 #6
No offense. A sloppy hack for me maybe....I just wanted to learn what I was
doing wrong first.
"DalePres" <don-t-spa-m-me@lea-ve-me-a-lone--.com> wrote in message
news:u2**************@tk2msftngp13.phx.gbl...
I wouldn't call ISNULL a sloppy hack. There's a reason they built the
functionality into SQL. The advantage is that, if it's in a stored
procedure, any app that calls the stored proc gets the same handling and
gives a consistent result. Also, it is one place to change the code, rather than in every app.

Just my opinion.

Dale

"D. Shane Fowlkes" <sh**********@h-o-t-m-a-i-l.com> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
Modifying my Select would be a sloppy hack...seems like it would anyway.

I did get this to work:

If IsDBNull(dtrData("Address2")) Then
lblAddress2.Text = ""
Else
lblAddress2.Text = dtrData("Address2")
End If
Thanks guys.
"Cowboy (Gregory A. Beamer)" <No************@comcast.netNoSpamM> wrote in
message news:O5**************@TK2MSFTNGP11.phx.gbl...
My code is normally more like so (C#)

if(dr["field"] == DBNull.Value)
statement;
else
statement;

Which equates to (VB.NET)

If (dr("field") == DBNull.Value) Then
Else
End If

As suggested, you can also do this in the SQL statement if you want to

fill
with a string, as you are doing.

--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

************************************************** ********************
Think Outside the Box!
************************************************** ********************
"D. Shane Fowlkes" <sh**********@h-o-t-m-a-i-l.com> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
> I'm using SQL Server 2000 and on my page, I'm simply creating a
> SQLDataReader and filling in Labels with the retrieved (single)

record. > However, how can I prevent from getting errors when a field is null?
>
>
> I've tried something like this (experimenting with IsDBNull):
> ================================================== =======
> If dtrData.IsDBNull("Address2") = True Then
> lblAddress2.Text = ""
> Else
> lblAddress2.Text = dtrData("Address2")
> End If
>
> And this....
>
> If IsNothing(dtrDatal("Address2")) = True Then
> lblAddress2.Text = ""
> Else
> lblAddress2.Text = dtrData("Address2")
> End If
> ================================================== ======
>
>
> ..and neither seem to work. Any suggestions? Thanks!
>
>
>
>



Nov 18 '05 #7
The code there is very VB.NET centric. This is not a problem, but realize
that it does not translate directly to other .NET languages. If VB.NET is
the language of choice, go with it. Peace!

--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

************************************************** ********************
Think Outside the Box!
************************************************** ********************
"D. Shane Fowlkes" <sh**********@h-o-t-m-a-i-l.com> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
Modifying my Select would be a sloppy hack...seems like it would anyway.

I did get this to work:

If IsDBNull(dtrData("Address2")) Then
lblAddress2.Text = ""
Else
lblAddress2.Text = dtrData("Address2")
End If
Thanks guys.
"Cowboy (Gregory A. Beamer)" <No************@comcast.netNoSpamM> wrote in
message news:O5**************@TK2MSFTNGP11.phx.gbl...
My code is normally more like so (C#)

if(dr["field"] == DBNull.Value)
statement;
else
statement;

Which equates to (VB.NET)

If (dr("field") == DBNull.Value) Then
Else
End If

As suggested, you can also do this in the SQL statement if you want to

fill
with a string, as you are doing.

--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

************************************************** ********************
Think Outside the Box!
************************************************** ********************
"D. Shane Fowlkes" <sh**********@h-o-t-m-a-i-l.com> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
I'm using SQL Server 2000 and on my page, I'm simply creating a
SQLDataReader and filling in Labels with the retrieved (single) record. However, how can I prevent from getting errors when a field is null?
I've tried something like this (experimenting with IsDBNull):
================================================== =======
If dtrData.IsDBNull("Address2") = True Then
lblAddress2.Text = ""
Else
lblAddress2.Text = dtrData("Address2")
End If

And this....

If IsNothing(dtrDatal("Address2")) = True Then
lblAddress2.Text = ""
Else
lblAddress2.Text = dtrData("Address2")
End If
================================================== ======
..and neither seem to work. Any suggestions? Thanks!



Nov 18 '05 #8
In some instances, it would be a sloppy hack. For example, I have seen
databases (God only knows why) that use both null and empty string (or
single char) to represent different conditions. ISNULL would not detect
between the two.

Detecting on the UI layer is a better option in some situations, while
detecting and "correcting" on the data layer is better in others.

--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

************************************************** ********************
Think Outside the Box!
************************************************** ********************
"DalePres" <don-t-spa-m-me@lea-ve-me-a-lone--.com> wrote in message
news:u2**************@tk2msftngp13.phx.gbl...
I wouldn't call ISNULL a sloppy hack. There's a reason they built the
functionality into SQL. The advantage is that, if it's in a stored
procedure, any app that calls the stored proc gets the same handling and
gives a consistent result. Also, it is one place to change the code, rather than in every app.

Just my opinion.

Dale

"D. Shane Fowlkes" <sh**********@h-o-t-m-a-i-l.com> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
Modifying my Select would be a sloppy hack...seems like it would anyway.

I did get this to work:

If IsDBNull(dtrData("Address2")) Then
lblAddress2.Text = ""
Else
lblAddress2.Text = dtrData("Address2")
End If
Thanks guys.
"Cowboy (Gregory A. Beamer)" <No************@comcast.netNoSpamM> wrote in
message news:O5**************@TK2MSFTNGP11.phx.gbl...
My code is normally more like so (C#)

if(dr["field"] == DBNull.Value)
statement;
else
statement;

Which equates to (VB.NET)

If (dr("field") == DBNull.Value) Then
Else
End If

As suggested, you can also do this in the SQL statement if you want to

fill
with a string, as you are doing.

--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

************************************************** ********************
Think Outside the Box!
************************************************** ********************
"D. Shane Fowlkes" <sh**********@h-o-t-m-a-i-l.com> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
> I'm using SQL Server 2000 and on my page, I'm simply creating a
> SQLDataReader and filling in Labels with the retrieved (single)

record. > However, how can I prevent from getting errors when a field is null?
>
>
> I've tried something like this (experimenting with IsDBNull):
> ================================================== =======
> If dtrData.IsDBNull("Address2") = True Then
> lblAddress2.Text = ""
> Else
> lblAddress2.Text = dtrData("Address2")
> End If
>
> And this....
>
> If IsNothing(dtrDatal("Address2")) = True Then
> lblAddress2.Text = ""
> Else
> lblAddress2.Text = dtrData("Address2")
> End If
> ================================================== ======
>
>
> ..and neither seem to work. Any suggestions? Thanks!
>
>
>
>



Nov 18 '05 #9
Usually a NULL value means that the value has not been set and "waits" some
value in a later update statement later in the business logic or program
life.
While an empty string value means that the value is set to empty

let say u have a table with names (the exemple is stupid it is just for
exemple sake)

FirstName
MiddleName
LastName

- MiddleName to null means the middle name is not know yet. then a UI could
say to the application user something like "hey dude, ask your guy to let
you know about his middle name".
- MiddleName to "" (empty string) means that we know the guy does not have a
middle name (yes it happens I do not have any!). No need to ask for it !

My 2 cents, :)

Francois

"Cowboy (Gregory A. Beamer)" <No************@comcast.netNoSpamM> wrote in
message news:u3*************@tk2msftngp13.phx.gbl...
In some instances, it would be a sloppy hack. For example, I have seen
databases (God only knows why) that use both null and empty string (or
single char) to represent different conditions. ISNULL would not detect
between the two.

Detecting on the UI layer is a better option in some situations, while
detecting and "correcting" on the data layer is better in others.

--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

************************************************** ********************
Think Outside the Box!
************************************************** ********************
"DalePres" <don-t-spa-m-me@lea-ve-me-a-lone--.com> wrote in message
news:u2**************@tk2msftngp13.phx.gbl...
I wouldn't call ISNULL a sloppy hack. There's a reason they built the
functionality into SQL. The advantage is that, if it's in a stored
procedure, any app that calls the stored proc gets the same handling and
gives a consistent result. Also, it is one place to change the code,

rather
than in every app.

Just my opinion.

Dale

"D. Shane Fowlkes" <sh**********@h-o-t-m-a-i-l.com> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
Modifying my Select would be a sloppy hack...seems like it would anyway.
I did get this to work:

If IsDBNull(dtrData("Address2")) Then
lblAddress2.Text = ""
Else
lblAddress2.Text = dtrData("Address2")
End If
Thanks guys.
"Cowboy (Gregory A. Beamer)" <No************@comcast.netNoSpamM> wrote in message news:O5**************@TK2MSFTNGP11.phx.gbl...
> My code is normally more like so (C#)
>
> if(dr["field"] == DBNull.Value)
> statement;
> else
> statement;
>
> Which equates to (VB.NET)
>
> If (dr("field") == DBNull.Value) Then
> Else
> End If
>
> As suggested, you can also do this in the SQL statement if you want to fill
> with a string, as you are doing.
>
> --
> Gregory A. Beamer
> MVP; MCP: +I, SE, SD, DBA
>
> ************************************************** ******************** > Think Outside the Box!
> ************************************************** ******************** > "D. Shane Fowlkes" <sh**********@h-o-t-m-a-i-l.com> wrote in message
> news:%2****************@TK2MSFTNGP09.phx.gbl...
> > I'm using SQL Server 2000 and on my page, I'm simply creating a
> > SQLDataReader and filling in Labels with the retrieved (single)

record.
> > However, how can I prevent from getting errors when a field is null? > >
> >
> > I've tried something like this (experimenting with IsDBNull):
> > ================================================== =======
> > If dtrData.IsDBNull("Address2") = True Then
> > lblAddress2.Text = ""
> > Else
> > lblAddress2.Text = dtrData("Address2")
> > End If
> >
> > And this....
> >
> > If IsNothing(dtrDatal("Address2")) = True Then
> > lblAddress2.Text = ""
> > Else
> > lblAddress2.Text = dtrData("Address2")
> > End If
> > ================================================== ======
> >
> >
> > ..and neither seem to work. Any suggestions? Thanks!
> >
> >
> >
> >
>
>



Nov 18 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: hjyn | last post by:
Hi All, I create a form for the user to enter the information, the form contains two table, table A and table B. I wrote a script to prompt the user to fill all the necessary data if they miss to...
19
by: Gav | last post by:
Hi, At the moment i am checking that all the fields have been filled out, at the moment i am using the following... if firstname = "" and surname = "" and address1 = "" and town = "" and county...
3
by: Karunakararao | last post by:
Hi all Presently i am sending data to database filed like this "EquipmentFilterDevPrimaryId = 0" i need Instead of "0" (NULL)i need store the data null value how can i pass this null...
8
by: craigkenisston | last post by:
I have a generic function that receives a couple of datetime values to work with. They can or cannot have a value, therefore I wanted to use null. This function will call a database stored...
2
by: Anastasios Papadopoulos | last post by:
Hello all, I have statements like the following in my Property Get: Public Property AccountNumber() As String Get Return MyClass.AccountNumber.ToString End Get blah blah End Property
5
by: Stewart | last post by:
Hi there, I would like to calculate a person's age in years (between 2 dates). I am using the following function for this calculation (source: http://www.mvps.org/access/datetime/date0001.htm)...
5
by: yeoj13 | last post by:
Hello, I have a db2load script I'm using to populate a large table. Ideally, my target table is required to have "Not Null" constraints on a number of different columns. I've noticed a ...
7
mkremkow
by: mkremkow | last post by:
Hello all, Access 2003 Windows PC: I am trying to figure out why my Query isn't working. In this query, I want to get only those jobs that are still open and where at least one of four Yes/No...
1
by: veaux | last post by:
Question deals with linking tables in queries. I'm not a code writer so use the GUI for all my queries. Table 1 - Master Table 2 - Sub1 Table 3 - Sub 2 All 3 tables have the same key field....
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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:
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
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.