Results 1 to 4 of 4

Thread: How to handle Null values

  1. #1

    Thread Starter
    Member
    Join Date
    Jun 2002
    Location
    Madras
    Posts
    50

    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

  2. #2
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    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.

  3. #3

    Thread Starter
    Member
    Join Date
    Jun 2002
    Location
    Madras
    Posts
    50

    Question Re: How to handle Null values

    Quote 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

  4. #4
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    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
  •  



Click Here to Expand Forum to Full Width