|
-
Mar 26th, 2005, 07:13 AM
#1
Thread Starter
Member
How to handle Null values
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
venkat
Strength is Life, Weakness is Death
- Swami Vivekananda
-
Mar 27th, 2005, 12:45 AM
#2
Re: How to handle Null values
Post the code you're using that errors out to give you that message. And any relevant associated code.
-
Mar 28th, 2005, 02:22 AM
#3
Thread Starter
Member
Re: How to handle Null values
 Originally Posted by mendhak
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.
venkat
Strength is Life, Weakness is Death
- Swami Vivekananda
-
Mar 29th, 2005, 12:32 AM
#4
Re: How to handle Null values
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().
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|