Wrox Programmer Forums
Go Back   Wrox Programmer Forums > .NET > Other .NET > ADO.NET
|
ADO.NET For discussion about ADO.NET.  Topics such as question regarding the System.Data namespace are appropriate.  Questions specific to a particular application should be posted in a forum specific to the application .
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ADO.NET 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 March 28th, 2005, 05:17 AM
Registered User
 
Join Date: Feb 2004
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default Handling Null Values

Hi,

How to get rid of these null value exceptions.


CODE Sample

strSQL= "select * from department where departmentid = 'D101';
cmd = new SqlCommand(strSQL,conn);

reader = cmd.ExecuteReader();

while(reader.Read())
{
m_Deptname = reader.GetString(1);
m_Description=reader.GetString(2);
}


Table
departmentid varchar(15)
departmentname varchar(30)
description varchar(50)

Sample Data

ID Name Description

D101 Accounts Accounts Dept
D102 Admin Admin Dept
D103 HRM <NULL>
D104 IT <NULL>
D105 Prod Production Dept

if the column description has a null value(Ex: D103,D104) is accessed, i get the error as mentioned below. but its working fine for the other sample criteria

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Data.SqlTypes.SqlNullValueException: Data is Null. This method or property cannot be called on Null values.



thanks,


Venkat


Strength is Life, Weakness is Death

- Swami Vivekananda
 
Old March 28th, 2005, 11:14 AM
Friend of Wrox
 
Join Date: Jul 2004
Posts: 623
Thanks: 0
Thanked 1 Time in 1 Post
Default

Code:
while(reader.Read())
{
if(!reader.IsDBNull(1)) 
    m_Deptname = reader.GetString(1);
//else{for null values}
if(!reader.IsDBNull(2)) 
    m_Description=reader.GetString(2);
//else{for null values}
}
_____________
Mehdi.
software student.
 
Old January 16th, 2006, 05:46 PM
Registered User
 
Join Date: Jan 2006
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi

What may be the reason for "No data exists for the row/column" error
when using reader.IsDBNull(0) method? I catch such error!!

FYI: reader contains data (reader.HasRows is true) and field with index 0 IS NOT NULL!!!

Helena





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
Passing null values maddy137 ASP.NET 1.0 and 1.1 Basics 1 May 7th, 2006 12:26 PM
Handling Null Values ~Bean~ ASP.NET 1.x and 2.0 Application Design 1 January 31st, 2006 02:02 PM
Catching NULL values hasanali00 BOOK: ASP.NET Website Programming Problem-Design-Solution 1 July 1st, 2005 10:29 PM
Handling Null Dates [email protected] ASP.NET 1.0 and 1.1 Professional 12 February 27th, 2004 07:09 AM





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