473,795 Members | 3,440 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

A better way to check for IsDBNull ?

I have this code snippet below where I'm loading all the labels based upon
the retrieved record from the DataReader. Unfortunatley, some of the values
are null, so I can't explicitly set the labels without checking first. Is
there a better method to write this than using a bunch of "If" statements?
Could it be a function? if so, how would I write it?

'Read the single record
While drLeadFeedback. Read()

If Not drLeadFeedback. IsDBNull(1) Then
lblLeadName.Tex t = drLeadFeedback. Item("Lead_Name ")
End If
If Not drLeadFeedback. IsDBNull(2) Then
lblLeadEmail.Te xt = drLeadFeedback. Item("Lead_Emai l")
End If
If Not drLeadFeedback. IsDBNull(3) Then
lblLeadPhone.Te xt = drLeadFeedback. Item("Lead_Phon e")
End If
If Not drLeadFeedback. IsDBNull(4) Then
lblUnderwriter. Text = drLeadFeedback. Item("Underwrit er")
End If
If Not drLeadFeedback. IsDBNull(10) Then
lblCreateDate.T ext = drLeadFeedback. Item("Create_Da te")
End If
If Not drLeadFeedback. IsDBNull(18) Then
lblStatus.Text =
drLeadFeedback. Item("Lead_Feed back_Status")
End If
If Not drLeadFeedback. IsDBNull(19) Then
lblAssignedTo.T ext = drLeadFeedback. Item("Assigned_ To")
End If
If Not drLeadFeedback. IsDBNull(11) Then
lblDealName.Tex t = drLeadFeedback. Item("Deal_Name ")
End If
If Not drLeadFeedback. IsDBNull(12) Then
lblClient.Text = drLeadFeedback. Item("Client_Na me")
End If
If Not drLeadFeedback. IsDBNull(13) Then
lblCSMName.Text = drLeadFeedback. Item("CSM_Name" )
End If
If Not drLeadFeedback. IsDBNull(14) Then
lblSpeedRating. Text =
drLeadFeedback. Item("Speed_Rat ing")
End If
If Not drLeadFeedback. IsDBNull(15) Then
lblAccuracyRati ng.Text =
drLeadFeedback. Item("Accuracy_ Rating")
End If
If Not drLeadFeedback. IsDBNull(16) Then
lblKnowledgeRat ing.Text =
drLeadFeedback. Item("Knowledge _Rating")
End If
If Not drLeadFeedback. IsDBNull(17) Then
lblWorkEffortRa ting.Text =
drLeadFeedback. Item("Work_Effo rt_Rating")
End If
If Not drLeadFeedback. IsDBNull(8) Then
lblResolvedBy.T ext = drLeadFeedback. Item("Resolved_ By")
End If
If Not drLeadFeedback. IsDBNull(7) Then
lblResolutionDa te.Text =
drLeadFeedback. Item("Resolutio n_Date")
End If
If Not drLeadFeedback. IsDBNull(9) Then
lblFinalResolut ion.Text =
drLeadFeedback. Item("Final_Res olution")
End If
If Not drLeadFeedback. IsDBNull(6) Then
lblDetail.Text = drLeadFeedback. Item("Detail")
End If
If Not drLeadFeedback. IsDBNull(5) Then
lblSubject.Text =
drLeadFeedback. Item("Lead_Feed back_Subject")
End If
If Not drLeadFeedback. IsDBNull(20) Then
lblCategory.Tex t =
drLeadFeedback. Item("Lead_Feed back_Category")
End If
End While
Nov 19 '05 #1
3 3590
There are several ways to skin this cat; here is one:

lblLeadName.Tex t = GetItem( drLeadFeedback, "Lead_Name" );

Shared Function GetItem( reader As IDataReader, field As String ) As String
If reader.Item( field ) <> DBNull.Value Then
Return reader.Item( field ).ToString()
End If
Return String.Empty
End Function

HTH,

bill
"Paul D. Fox" <pd*****@rcn.co m> wrote in message
news:uj******** ******@TK2MSFTN GP12.phx.gbl...
I have this code snippet below where I'm loading all the labels based upon
the retrieved record from the DataReader. Unfortunatley, some of the values are null, so I can't explicitly set the labels without checking first. Is
there a better method to write this than using a bunch of "If" statements?
Could it be a function? if so, how would I write it?

'Read the single record
While drLeadFeedback. Read()

If Not drLeadFeedback. IsDBNull(1) Then
lblLeadName.Tex t = drLeadFeedback. Item("Lead_Name ")
End If
If Not drLeadFeedback. IsDBNull(2) Then
lblLeadEmail.Te xt = drLeadFeedback. Item("Lead_Emai l")
End If
If Not drLeadFeedback. IsDBNull(3) Then
lblLeadPhone.Te xt = drLeadFeedback. Item("Lead_Phon e")
End If
If Not drLeadFeedback. IsDBNull(4) Then
lblUnderwriter. Text = drLeadFeedback. Item("Underwrit er") End If
If Not drLeadFeedback. IsDBNull(10) Then
lblCreateDate.T ext = drLeadFeedback. Item("Create_Da te") End If
If Not drLeadFeedback. IsDBNull(18) Then
lblStatus.Text =
drLeadFeedback. Item("Lead_Feed back_Status")
End If
If Not drLeadFeedback. IsDBNull(19) Then
lblAssignedTo.T ext = drLeadFeedback. Item("Assigned_ To") End If
If Not drLeadFeedback. IsDBNull(11) Then
lblDealName.Tex t = drLeadFeedback. Item("Deal_Name ")
End If
If Not drLeadFeedback. IsDBNull(12) Then
lblClient.Text = drLeadFeedback. Item("Client_Na me")
End If
If Not drLeadFeedback. IsDBNull(13) Then
lblCSMName.Text = drLeadFeedback. Item("CSM_Name" )
End If
If Not drLeadFeedback. IsDBNull(14) Then
lblSpeedRating. Text =
drLeadFeedback. Item("Speed_Rat ing")
End If
If Not drLeadFeedback. IsDBNull(15) Then
lblAccuracyRati ng.Text =
drLeadFeedback. Item("Accuracy_ Rating")
End If
If Not drLeadFeedback. IsDBNull(16) Then
lblKnowledgeRat ing.Text =
drLeadFeedback. Item("Knowledge _Rating")
End If
If Not drLeadFeedback. IsDBNull(17) Then
lblWorkEffortRa ting.Text =
drLeadFeedback. Item("Work_Effo rt_Rating")
End If
If Not drLeadFeedback. IsDBNull(8) Then
lblResolvedBy.T ext = drLeadFeedback. Item("Resolved_ By") End If
If Not drLeadFeedback. IsDBNull(7) Then
lblResolutionDa te.Text =
drLeadFeedback. Item("Resolutio n_Date")
End If
If Not drLeadFeedback. IsDBNull(9) Then
lblFinalResolut ion.Text =
drLeadFeedback. Item("Final_Res olution")
End If
If Not drLeadFeedback. IsDBNull(6) Then
lblDetail.Text = drLeadFeedback. Item("Detail")
End If
If Not drLeadFeedback. IsDBNull(5) Then
lblSubject.Text =
drLeadFeedback. Item("Lead_Feed back_Subject")
End If
If Not drLeadFeedback. IsDBNull(20) Then
lblCategory.Tex t =
drLeadFeedback. Item("Lead_Feed back_Category")
End If
End While

Nov 19 '05 #2
refactoring 101, would say to build a function:

lblLeadName.Tex t =
DRHelper.GetTex tValue(drLeadFe edback,"Lead_Na me")

where (air code)

public class DRHelper
{
static public string GetTextValue (IDataReader dr, string colName)
{
int colnum = dr.GetOrdinal(c olName);
if (dr.IsDBNull(co lnum))
return "";
else
return dr[colnum].ToString();
}
}

-- bruce (sqlwork.com)
"Paul D. Fox" <pd*****@rcn.co m> wrote in message
news:uj******** ******@TK2MSFTN GP12.phx.gbl...
I have this code snippet below where I'm loading all the labels based upon
the retrieved record from the DataReader. Unfortunatley, some of the
values are null, so I can't explicitly set the labels without checking
first. Is there a better method to write this than using a bunch of "If"
statements? Could it be a function? if so, how would I write it?

'Read the single record
While drLeadFeedback. Read()

If Not drLeadFeedback. IsDBNull(1) Then
lblLeadName.Tex t = drLeadFeedback. Item("Lead_Name ")
End If
If Not drLeadFeedback. IsDBNull(2) Then
lblLeadEmail.Te xt = drLeadFeedback. Item("Lead_Emai l")
End If
If Not drLeadFeedback. IsDBNull(3) Then
lblLeadPhone.Te xt = drLeadFeedback. Item("Lead_Phon e")
End If
If Not drLeadFeedback. IsDBNull(4) Then
lblUnderwriter. Text =
drLeadFeedback. Item("Underwrit er")
End If
If Not drLeadFeedback. IsDBNull(10) Then
lblCreateDate.T ext = drLeadFeedback. Item("Create_Da te")
End If
If Not drLeadFeedback. IsDBNull(18) Then
lblStatus.Text =
drLeadFeedback. Item("Lead_Feed back_Status")
End If
If Not drLeadFeedback. IsDBNull(19) Then
lblAssignedTo.T ext = drLeadFeedback. Item("Assigned_ To")
End If
If Not drLeadFeedback. IsDBNull(11) Then
lblDealName.Tex t = drLeadFeedback. Item("Deal_Name ")
End If
If Not drLeadFeedback. IsDBNull(12) Then
lblClient.Text = drLeadFeedback. Item("Client_Na me")
End If
If Not drLeadFeedback. IsDBNull(13) Then
lblCSMName.Text = drLeadFeedback. Item("CSM_Name" )
End If
If Not drLeadFeedback. IsDBNull(14) Then
lblSpeedRating. Text =
drLeadFeedback. Item("Speed_Rat ing")
End If
If Not drLeadFeedback. IsDBNull(15) Then
lblAccuracyRati ng.Text =
drLeadFeedback. Item("Accuracy_ Rating")
End If
If Not drLeadFeedback. IsDBNull(16) Then
lblKnowledgeRat ing.Text =
drLeadFeedback. Item("Knowledge _Rating")
End If
If Not drLeadFeedback. IsDBNull(17) Then
lblWorkEffortRa ting.Text =
drLeadFeedback. Item("Work_Effo rt_Rating")
End If
If Not drLeadFeedback. IsDBNull(8) Then
lblResolvedBy.T ext = drLeadFeedback. Item("Resolved_ By")
End If
If Not drLeadFeedback. IsDBNull(7) Then
lblResolutionDa te.Text =
drLeadFeedback. Item("Resolutio n_Date")
End If
If Not drLeadFeedback. IsDBNull(9) Then
lblFinalResolut ion.Text =
drLeadFeedback. Item("Final_Res olution")
End If
If Not drLeadFeedback. IsDBNull(6) Then
lblDetail.Text = drLeadFeedback. Item("Detail")
End If
If Not drLeadFeedback. IsDBNull(5) Then
lblSubject.Text =
drLeadFeedback. Item("Lead_Feed back_Subject")
End If
If Not drLeadFeedback. IsDBNull(20) Then
lblCategory.Tex t =
drLeadFeedback. Item("Lead_Feed back_Category")
End If
End While

Nov 19 '05 #3
Thanks Guy's, that worked much better...

Paul
"William F. Robertson, Jr." <th****@nameht. org> wrote in message
news:e%******** ********@TK2MSF TNGP09.phx.gbl. ..
There are several ways to skin this cat; here is one:

lblLeadName.Tex t = GetItem( drLeadFeedback, "Lead_Name" );

Shared Function GetItem( reader As IDataReader, field As String ) As
String
If reader.Item( field ) <> DBNull.Value Then
Return reader.Item( field ).ToString()
End If
Return String.Empty
End Function

HTH,

bill
"Paul D. Fox" <pd*****@rcn.co m> wrote in message
news:uj******** ******@TK2MSFTN GP12.phx.gbl...
I have this code snippet below where I'm loading all the labels based
upon
the retrieved record from the DataReader. Unfortunatley, some of the

values
are null, so I can't explicitly set the labels without checking first.
Is
there a better method to write this than using a bunch of "If"
statements?
Could it be a function? if so, how would I write it?

'Read the single record
While drLeadFeedback. Read()

If Not drLeadFeedback. IsDBNull(1) Then
lblLeadName.Tex t = drLeadFeedback. Item("Lead_Name ")
End If
If Not drLeadFeedback. IsDBNull(2) Then
lblLeadEmail.Te xt = drLeadFeedback. Item("Lead_Emai l")
End If
If Not drLeadFeedback. IsDBNull(3) Then
lblLeadPhone.Te xt = drLeadFeedback. Item("Lead_Phon e")
End If
If Not drLeadFeedback. IsDBNull(4) Then
lblUnderwriter. Text =

drLeadFeedback. Item("Underwrit er")
End If
If Not drLeadFeedback. IsDBNull(10) Then
lblCreateDate.T ext =

drLeadFeedback. Item("Create_Da te")
End If
If Not drLeadFeedback. IsDBNull(18) Then
lblStatus.Text =
drLeadFeedback. Item("Lead_Feed back_Status")
End If
If Not drLeadFeedback. IsDBNull(19) Then
lblAssignedTo.T ext =

drLeadFeedback. Item("Assigned_ To")
End If
If Not drLeadFeedback. IsDBNull(11) Then
lblDealName.Tex t = drLeadFeedback. Item("Deal_Name ")
End If
If Not drLeadFeedback. IsDBNull(12) Then
lblClient.Text = drLeadFeedback. Item("Client_Na me")
End If
If Not drLeadFeedback. IsDBNull(13) Then
lblCSMName.Text = drLeadFeedback. Item("CSM_Name" )
End If
If Not drLeadFeedback. IsDBNull(14) Then
lblSpeedRating. Text =
drLeadFeedback. Item("Speed_Rat ing")
End If
If Not drLeadFeedback. IsDBNull(15) Then
lblAccuracyRati ng.Text =
drLeadFeedback. Item("Accuracy_ Rating")
End If
If Not drLeadFeedback. IsDBNull(16) Then
lblKnowledgeRat ing.Text =
drLeadFeedback. Item("Knowledge _Rating")
End If
If Not drLeadFeedback. IsDBNull(17) Then
lblWorkEffortRa ting.Text =
drLeadFeedback. Item("Work_Effo rt_Rating")
End If
If Not drLeadFeedback. IsDBNull(8) Then
lblResolvedBy.T ext =

drLeadFeedback. Item("Resolved_ By")
End If
If Not drLeadFeedback. IsDBNull(7) Then
lblResolutionDa te.Text =
drLeadFeedback. Item("Resolutio n_Date")
End If
If Not drLeadFeedback. IsDBNull(9) Then
lblFinalResolut ion.Text =
drLeadFeedback. Item("Final_Res olution")
End If
If Not drLeadFeedback. IsDBNull(6) Then
lblDetail.Text = drLeadFeedback. Item("Detail")
End If
If Not drLeadFeedback. IsDBNull(5) Then
lblSubject.Text =
drLeadFeedback. Item("Lead_Feed back_Subject")
End If
If Not drLeadFeedback. IsDBNull(20) Then
lblCategory.Tex t =
drLeadFeedback. Item("Lead_Feed back_Category")
End If
End While


Nov 19 '05 #4

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

Similar topics

11
3766
by: Wolfgang Kaml | last post by:
Hello All, I have been working on this for almost a week now and I haven't anything up my sleeves anymore that I could test in addition or change.... Since I am not sure, if this is a Windows 2003 Server or ADO or ODBC issue, I am posting this on all of the three newsgroups. That's the setup: Windows 2003 Server with IIS and ASP.NET actiavted Access 2002 mdb file (and yes, proper rights are set on TMP paths and path,
4
3452
by: Jim | last post by:
I am having a problem with the IsDBNull function. When it checks what type of value is contained in the parameter that is passed to it, it does not successfully determine that it has a DBNull value. If I access the value of what I am going to pass to the IsDBNull function before I call the IsDBNull function then it does successfully determine that it has a DBNull value. Below is a page that I am using to test. Note: that the value for...
2
11705
by: martin | last post by:
Hi, I am looking for a method to check if an item (any item) is selected in a particular listbox before deleteing it from my database, however I keep getting errors, The code I have is lbSpecificProgram is a listbox.
8
1199
by: Larry Smith | last post by:
If Not dtrMyDataReader.IsDBNull(dtrMyDataReader.GetOrdinal("Customer")) Then strCustomer = dtrMyDataReader("Customer").ToString() End If Where "Customer" is a string data type.
4
4016
by: sck10 | last post by:
Hello (converting from vb to c#), Is there a way to test the value of null of a particular field of a dataset from SQL Server? I am going through a dataset and trying to determine if the value being sent is Null using IsDBNull. I am using the following: if (! IsDBNull(spCurrentHighLgt)) this.ltlCHLTitle.Text = spCurrentHighLgt.toString(); However, I am getting the error that the name "IsDBNull" does not exist in
2
6310
by: John Dalberg | last post by:
..NET 2.0. Is there a generic single function to check for a dbnull regardless of data type? John Dalberg
13
1456
by: pvong | last post by:
VB.Net I'm pulling Data from a DB and creating a letter by filling the Labels. It works perfectly, but if Addr2 and Addr3 are blank, it leaves a Blank space in that area. Is there a way to make it so I can say if the data is empty, temporarely delete the label or make it not exist at all? I'm not talking about just hiding it because it still leaves an empy space. This is what I get: FirstName LastName
2
8121
by: =?Utf-8?B?TWlrZSBPS0M=?= | last post by:
VB 2005 All developers face this issue; so I'm sure Microsoft was a solution for it. What is the built in method to check DBNull in a recordset field? NOT a typed dataset. I don't want to modify my sql script because it is created dynamically. Below I have the old method to check for DBNull and to deal with DBNull in the recordset. This method of coding is used over 80 times as I have over 80 fields to check for DBNull, so a one...
3
6708
by: cj2 | last post by:
if myodbcreader.hasrows then if myodbcreader("code").trim = "" or myodbcreader("reas").trim = "aVAL" then do something endif endif Is this the appropriate way to test if the code field is something other an a value? I find I can also say if myodbcreader("code").trim = nothing.
0
9672
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10437
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10214
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10164
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10001
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9042
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6780
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4113
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2920
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.