Dataset field DBNull -> int?

Posted by BobClegg on Stack Overflow See other posts from Stack Overflow or by BobClegg
Published on 2009-03-02T02:45:53Z Indexed on 2010/04/07 2:43 UTC
Read the original article Hit count: 407

Filed under:
|
|

SQLServer int field. Value sometimes null. DataAdapter fills dataset OK and can display data in DatagridView OK.

When trying to retrieve the data programmatically from the dataset the Dataset field retrieval code throws a StronglyTypedException error.

 [global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
    public int curr_reading {
        get {
            try {
                return ((int)(this[this.tableHistory.curr_readingColumn]));
            }
            catch (global::System.InvalidCastException e) {
                throw new global::System.Data.StrongTypingException("The value for column \'curr_reading\' in table \'History\' is DBNull.", e);
            }

Got past this by checking for DBNull in the get accessor and returning null but... When the dataset structure is modified (Still developing) my changes (unsurprisingly) are gone.

What is the best way to handle this situation? It seems I am stuck with dealing with it at the dataset level. Is there some sort of attribute that can tell the auto code generator to leave the changes in place?

© Stack Overflow or respective owner

Related posts about dbnull

Related posts about nullable