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
|