Wrox Programmer Forums
|
BOOK: ASP.NET Website Programming Problem-Design-Solution
This is the forum to discuss the Wrox book ASP.NET Website Programming: Problem - Design - Solution, Visual Basic .NET Edition by Marco Bellinaso, Kevin Hoffman; ISBN: 9780764543869
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: ASP.NET Website Programming Problem-Design-Solution section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old June 30th, 2005, 05:12 AM
Authorized User
 
Join Date: Mar 2005
Posts: 36
Thanks: 0
Thanked 0 Times in 0 Posts
Default Catching NULL values

Just imagine that the CategoryID column in News_News table has a null value.

Now if call
public NewsDetails GetDetails(int newsID)
        {......
if (news.Tables[0].Rows.Count > 0)
                {
                    DataRow rowNews = news.Tables[0].Rows[0];
                                        details.CategoryID = (int)rowNews["CategoryID"];
.....}

This would give me an runtime error: System.InvalidCastException: Specified cast is not valid

Because CategoryID contained a NULL value instead of an int.

Its the same thing with a DataSet. If I bind a dataset to a datagrid and some of the columns contain a NULL values, again the code will break.

How can I catch these types of errors?? What can I do to ensure that when there are NULL values in the table, my code does not break.


Thanks

 
Old July 1st, 2005, 10:29 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 917
Thanks: 0
Thanked 0 Times in 0 Posts
Default

The first thing you need to understand is that NULL in a database column has a meaning: it means that column was not populated. NULLs are not evil, but they are misunderstood. Would it be right for me to let you do math on a NULL, or use it as a key for another table? No, and that's by design.

The thing that burns a lot of people is that NULL is NEVER equal to anything, not even another NULL.

If you are working with a column that MUST be populated in every case, then you put a NOT NULL constraint on it. Nobody will ever be able to insert NULL into that column.

What if you are trying to read a column that is supposed to allow NULL values? There are several ways of testing for null. This example is for a SqlDataReader (C# syntax, but you can easily convert if needed):

Test before reading:

         string s;
         if (rdr.IsDbNull(0))
           s = "NULL";
         else
           s = rdr.GetString(0);

Or, if you want to read the value as a string, this will automatically convert a NULL to an empty string:

        rdr["col_name"].ToString(); // return empty string for NULL

Or use the SQL function ISNULL to translate a NULL value to something else from within SQL Server:

        select isnull(CategoryID,0) from ...

Eric





Similar Threads
Thread Thread Starter Forum Replies Last Post
Null values jmcgranahan BOOK: Access 2003 VBA Programmer's Reference 0 August 29th, 2006 02:25 PM
Outputing Null values. Neal XSLT 2 July 25th, 2006 05:46 AM
CheckBoxList and Null Values steve35719 ASP.NET 2.0 Basics 1 April 21st, 2006 09:42 AM
Handling Null Values ~Bean~ ASP.NET 1.x and 2.0 Application Design 1 January 31st, 2006 02:02 PM
Checking for Null Values asmodeus BOOK: Beginning VB.NET Databases 2 June 3rd, 2005 03:18 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.