473,472 Members | 1,831 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Working with null database values

Hi there,

I hope I am posting to the correct news group. What would be a good way to
deal with null database values using ADO.NET. My SQL queries sometimes
return null values when I have left outer joints for example and when I use
GetString() or GetInt32() from DBReader I get an error -
System.InvalidCastException: Specified cast is not valid. I know that one
way to correct this is to make sure the SQL queries never return null
values, but there must be more elegant way of fixing it.

TIA, regards,
George
Nov 19 '05 #1
2 1501
George,

You can always check if the value is null prior to refereing to it. Method
IsDBNull().

Eliyahu

"George G." <george.g@--xx--buildsmart.co.za> wrote in message
news:OO**************@tk2msftngp13.phx.gbl...
Hi there,

I hope I am posting to the correct news group. What would be a good way to
deal with null database values using ADO.NET. My SQL queries sometimes
return null values when I have left outer joints for example and when I use GetString() or GetInt32() from DBReader I get an error -
System.InvalidCastException: Specified cast is not valid. I know that one
way to correct this is to make sure the SQL queries never return null
values, but there must be more elegant way of fixing it.

TIA, regards,
George

Nov 19 '05 #2
"George G." <george.g@--xx--buildsmart.co.za> wrote in message
news:OO**************@tk2msftngp13.phx.gbl...
I hope I am posting to the correct news group. What would be a good way to
deal with null database values using ADO.NET. My SQL queries sometimes
return null values when I have left outer joints for example and when I
use GetString() or GetInt32() from DBReader I get an error -
System.InvalidCastException: Specified cast is not valid. I know that one
way to correct this is to make sure the SQL queries never return null
values, but there must be more elegant way of fixing it.


Let's say, for example, that you are displaying details of a single record
in a SQL Server table which has a DateTime field called dtmCompleted. When
the record is created, this field will be null because it hasn't been
completed yet. However, at some time in the future the field will contain
the datetime that the record was completed.

You have a Label called lblCompleted on your WebForm, and you populate it
from a SqlDataReader called objDR.

lblCompleted.Text = (objDR["dtmCompleted"] == DBNull.Value ? "&nbsp;" :
Convert.ToDateTime(objDR["dtmCompleted"]).ToString("dd MMM yyyy HH:mm"));
Nov 19 '05 #3

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

Similar topics

26
by: Agoston Bejo | last post by:
I want to enforce such a constraint on a column that would ensure that the values be all unique, but this wouldn't apply to NULL values. (I.e. there may be more than one NULL value in the column.)...
5
by: Andrew DeFaria | last post by:
I created the following .sql file to demonstrate a problem I'm having. According to the manual: If |ON DELETE CASCADE| is specified, and a row in the parent table is deleted, then InnoDB...
12
by: D Witherspoon | last post by:
What is the accepted method of creating a data class or business rules object class with properties that will allow the returning of null values? For example... I have a class named CResults with...
10
by: Python_it | last post by:
Python 2.4 MySQL-python.exe-1.2.0.win32-py2.4.zip How can I insert a NULL value in a table (MySQL-database). I can't set a var to NULL? Or is there a other possibility? My var must be variable...
13
by: Lee | last post by:
Hello All, First of all I would like to say thank you for all of the help I have received here. I have been teaching myself Access for about 4 years now and I've always been able to find a...
17
by: Mark A | last post by:
DB2 8.2 for Linux, FP 10 (also performs the same on DB2 8.2 for Windoes, FP 11). Using the SAMPLE database, tables EMP and EMLOYEE. In the followng stored procedure, 2 NULL columns (COMM) are...
0
by: pmaghnani | last post by:
Hai...I am a Newbie to DotNet and having few problems regarding storing and retriving Null values from SQL Server 2000 database... I'm storing the NULL values in db like this.. If...
17
by: erikcw | last post by:
Hi all, I'm trying to run the following query: amember_db = MySQLdb.connect(host="localhost", user="**********", passwd="*****", db="*******") # create a cursor self.amember_cursor =...
7
by: Ahmedhussain | last post by:
Hi, My application creates a thread for the conversion of several strings. I have an eventhandler called app.finished, but unfortunately, it is never called. Can any one tell me why? The code is...
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...
1
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...
0
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...
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...
0
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 ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.