none
reading a null value field in sqlite

    Question

  • Hi

    I'm reading a sqlite DB in c++/cli. All works fine but for one slight problem:

    login.resolution    = (reader-> GetValue(9) == NULL) ? 1024 : Convert::ToInt16(reader-> GetValue(9));

     

    This works fine if reader-> GetValue(9) has an int value in the DB field. When it is a NULL value the program crashes.

    Tried: reader-> GetValue(9) == nullptr but no joy.

    Can some kind person help me with testing for a NULL value field entry please?

     

     

    Tuesday, December 6, 2011 10:11 PM

Answers

  • You can use IsDBNull to check to see if the column is null prior to fetching the value.  See: http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqldatareader.isdbnull.aspx
    Reed Copsey, Jr. - http://reedcopsey.com
    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    • Marked as answer by nz556 Wednesday, December 7, 2011 11:14 PM
    Wednesday, December 7, 2011 12:07 AM

All replies

  • You can use IsDBNull to check to see if the column is null prior to fetching the value.  See: http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqldatareader.isdbnull.aspx
    Reed Copsey, Jr. - http://reedcopsey.com
    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    • Marked as answer by nz556 Wednesday, December 7, 2011 11:14 PM
    Wednesday, December 7, 2011 12:07 AM
  • Thanks for that. Worked a treat:

     

    login.resolution    = Convert::IsDBNull(reader-> GetValue(9)) ? 1024 : Convert::ToInt16(reader-> GetValue(9)); the help

     

    Appreciate the help.

    Wednesday, December 7, 2011 11:14 PM