dbnull int32 -please help

Discussion in 'ASP .Net' started by Miro, Oct 5, 2010.

  1. Miro

    Miro Guest

    Hi,

    I have been struggling with this for a long time now and I cant seem to find
    a solution.
    I have a typed dataset with an int32 column ( that I just added to an access
    database ).

    So now within the database I have dbnull's in all the old records that did
    not have this int32.
    So if I read my field I get an exception error.

    I put this simple code infront of the code as I have been googling around:
    Dim IDNullError As Boolean = False

    If ShoppingCartBibCustomRow.IsFrontColorIDNull Then
    IDNullError = True
    Else
    IDNullError = False
    End If

    but it always returns FALSE - never true.
    In the "Immediate Window" for debugging:
    ? ShoppingCartBibCustomRow.IsFrontColorIDNull
    False

    but if I look at the value in the Locals tab...
    + FrontNameColorID {"The value for column 'FrontNameColorID' in table
    'ShoppingCartBibCustom' is DBNull."} Integer


    so If I actaully do this in the "Immediate window",
    ? ShoppingCartBibCustomRow.FrontNameColorID
    ill get the exception:
    A first chance exception of type 'System.InvalidCastException' occurred in
    Microsoft.VisualBasic.dll
    A first chance exception of type 'System.Data.StrongTypingException'
    occurred in App_Code.rd6a6lcl.dll

    So why does it not return that it is a null value in the first place.

    Thank you,

    Miro
    Miro, Oct 5, 2010
    #1
    1. Advertising

  2. Miro

    Mr. Arnold Guest

    On 10/4/2010 9:56 PM, Miro wrote:
    > Hi,
    >
    > I have been struggling with this for a long time now and I cant seem to
    > find a solution.
    > I have a typed dataset with an int32 column ( that I just added to an
    > access database ).
    >
    > So now within the database I have dbnull's in all the old records that
    > did not have this int32.
    > So if I read my field I get an exception error.
    >
    > I put this simple code infront of the code as I have been googling around:
    > Dim IDNullError As Boolean = False
    >
    > If ShoppingCartBibCustomRow.IsFrontColorIDNull Then
    > IDNullError = True
    > Else
    > IDNullError = False
    > End If
    >
    > but it always returns FALSE - never true.
    > In the "Immediate Window" for debugging:
    > ? ShoppingCartBibCustomRow.IsFrontColorIDNull
    > False
    >
    > but if I look at the value in the Locals tab...
    > + FrontNameColorID {"The value for column 'FrontNameColorID' in table
    > 'ShoppingCartBibCustom' is DBNull."} Integer
    >
    >
    > so If I actaully do this in the "Immediate window",
    > ? ShoppingCartBibCustomRow.FrontNameColorID
    > ill get the exception:
    > A first chance exception of type 'System.InvalidCastException' occurred
    > in Microsoft.VisualBasic.dll
    > A first chance exception of type 'System.Data.StrongTypingException'
    > occurred in App_Code.rd6a6lcl.dll
    >
    > So why does it not return that it is a null value in the first place.
    >
    > Thank you,
    >
    > Miro


    I think you're going to need to check the object for being null.

    If ShoppingCartBibCustomRow.FrontNameColorID == null.

    Everything in .NET is an object. So you should check for something being
    an object. It's not an object if it's null.
    Mr. Arnold, Oct 5, 2010
    #2
    1. Advertising

  3. Miro

    Miro Guest

    The problem is that you cannot.

    The second you do anything with this line:
    ShoppingCartBibCustomRow.FrontNameColorID
    the typed dataset throws an exception.
    You cannot change in the typed dataset 'not to' throw an exception.
    It only allows you to set the "on Null return empty / null " on strings.

    So I would like to check for null - but I cant even check as the exception
    comes out.

    M.

    "Mr. Arnold" <> wrote in message
    news:#...
    > On 10/4/2010 9:56 PM, Miro wrote:
    >> Hi,
    >>
    >> I have been struggling with this for a long time now and I cant seem to
    >> find a solution.
    >> I have a typed dataset with an int32 column ( that I just added to an
    >> access database ).
    >>
    >> So now within the database I have dbnull's in all the old records that
    >> did not have this int32.
    >> So if I read my field I get an exception error.
    >>
    >> I put this simple code infront of the code as I have been googling
    >> around:
    >> Dim IDNullError As Boolean = False
    >>
    >> If ShoppingCartBibCustomRow.IsFrontColorIDNull Then
    >> IDNullError = True
    >> Else
    >> IDNullError = False
    >> End If
    >>
    >> but it always returns FALSE - never true.
    >> In the "Immediate Window" for debugging:
    >> ? ShoppingCartBibCustomRow.IsFrontColorIDNull
    >> False
    >>
    >> but if I look at the value in the Locals tab...
    >> + FrontNameColorID {"The value for column 'FrontNameColorID' in table
    >> 'ShoppingCartBibCustom' is DBNull."} Integer
    >>
    >>
    >> so If I actaully do this in the "Immediate window",
    >> ? ShoppingCartBibCustomRow.FrontNameColorID
    >> ill get the exception:
    >> A first chance exception of type 'System.InvalidCastException' occurred
    >> in Microsoft.VisualBasic.dll
    >> A first chance exception of type 'System.Data.StrongTypingException'
    >> occurred in App_Code.rd6a6lcl.dll
    >>
    >> So why does it not return that it is a null value in the first place.
    >>
    >> Thank you,
    >>
    >> Miro

    >
    > I think you're going to need to check the object for being null.
    >
    > If ShoppingCartBibCustomRow.FrontNameColorID == null.
    >
    > Everything in .NET is an object. So you should check for something being
    > an object. It's not an object if it's null.
    >
    Miro, Oct 5, 2010
    #3
  4. Miro

    Cubaman Guest

    On Oct 5, 3:56 am, "Miro" <> wrote:
    > Hi,
    >
    > I have been struggling with this for a long time now and I cant seem to find
    > a solution.
    > I have a typed dataset with an int32 column ( that I just added to an access
    > database ).
    >
    > So now within the database I have dbnull's in all the old records that did
    > not have this int32.
    > So if I read my field I get an exception error.
    >
    > I put this simple code infront of the code as I have been googling around:
    >                 Dim IDNullError As Boolean = False
    >
    >                 If ShoppingCartBibCustomRow.IsFrontColorIDNull Then
    >                     IDNullError = True
    >                 Else
    >                     IDNullError = False
    >                 End If
    >
    > but it always returns FALSE - never true.
    > In the "Immediate Window" for debugging:
    > ? ShoppingCartBibCustomRow.IsFrontColorIDNull
    > False
    >
    > but if I look at the value in the Locals tab...
    > +               FrontNameColorID        {"The value for column 'FrontNameColorID' in table
    > 'ShoppingCartBibCustom' is DBNull."}       Integer
    >
    > so If I actaully do this in the "Immediate window",
    > ? ShoppingCartBibCustomRow.FrontNameColorID
    > ill get the exception:
    > A first chance exception of type 'System.InvalidCastException' occurred in
    > Microsoft.VisualBasic.dll
    > A first chance exception of type 'System.Data.StrongTypingException'
    > occurred in App_Code.rd6a6lcl.dll
    >
    > So why does it not return that it is a null value in the first place.
    >
    > Thank you,
    >
    > Miro

    The DBNull type is a singleton class, which means only one DBNull
    object exists. The DBNull.Value member represents the sole DBNull
    object. DBNull.Value can be used to explicitly assign a nonexistent
    value to a database field, although most ADO.NET data providers
    automatically assign values of DBNull when a field does not have a
    valid value. You can determine whether a value retrieved from a
    database field is a DBNull value by passing the value of that field to
    the DBNull.Value.Equals method. However, some languages and database
    objects supply methods that make it easier to determine whether the
    value of a database field is DBNull.Value. These include the Visual
    Basic IsDBNull function, the Convert.IsDBNull method, the
    DataTableReader.IsDBNull method, and the IDataRecord.IsDBNull method.

    Do not confuse the notion of null in an object-oriented programming
    language with a DBNull object. In an object-oriented programming
    language, null means the absence of a reference to an object. DBNull
    represents an uninitialized variant or nonexistent database column.

    http://msdn.microsoft.com/en-us/library/system.dbnull.aspx

    Best regards
    Cubaman, Oct 6, 2010
    #4
  5. Miro

    Jason Keats Guest

    Miro wrote:
    >
    > The second you do anything with this line:
    > ShoppingCartBibCustomRow.FrontNameColorID
    > the typed dataset throws an exception.
    > You cannot change in the typed dataset 'not to' throw an exception.
    > It only allows you to set the "on Null return empty / null " on strings.
    >
    > So I would like to check for null - but I cant even check as the
    > exception comes out.
    >


    Doesn't every nullable column in a typed dataset have an
    Is[ColumnName]Null() method?

    Alternatively, you can annotate your XSD using the nullValue attribute...

    http://ondotnet.com/pub/a/dotnet/2003/03/31/typeddatasetannotations.html
    Jason Keats, Oct 6, 2010
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Robert V. Hanson
    Replies:
    2
    Views:
    503
    Robert Hanson
    Jul 1, 2003
  2. Martin
    Replies:
    2
    Views:
    431
    Martin
    Feb 2, 2004
  3. =?Utf-8?B?SGlmbmkgU2hhaHphcmQ=?=

    Invalid cast from System.Int32 to System.Byte[].

    =?Utf-8?B?SGlmbmkgU2hhaHphcmQ=?=, Feb 15, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    3,794
    William F. Robertson, Jr.
    Feb 15, 2005
  4. Simon Harris
    Replies:
    2
    Views:
    2,611
    Simon Harris
    Apr 27, 2005
  5. keithb
    Replies:
    3
    Views:
    18,575
    =?ISO-8859-1?Q?G=F6ran_Andersson?=
    May 7, 2006
Loading...

Share This Page