Click to See Complete Forum and Search --> : How to handle Null values
nvenkat75
Mar 26th, 2005, 06:13 AM
Hi,
i get this error while retriving the data from the sqlserver database.
System.Data.SqlTypes.SqlNullValueException: Data is Null.
This method or property cannot be called on Null values.
How to get rid of these null value exceptions.
thanks,
venkat
mendhak
Mar 26th, 2005, 11:45 PM
Post the code you're using that errors out to give you that message. And any relevant associated code.
nvenkat75
Mar 28th, 2005, 01:22 AM
Post the code you're using that errors out to give you that message. And any relevant associated code.
CODE Snippet
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
Departmentid Department 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.
mendhak
Mar 28th, 2005, 11:32 PM
Do a comparison with DBNull.Value for each of those fields, and assign to a variable depending on that.
Or, you could modify your SQL Statement to use COALESCE().
vbforums.com
Copyright Internet.com Inc., All Rights Reserved.