locked
Nullable of(Byte) problem when using Typed Dataset Datarow class RRS feed

  • Question

  • User438705957 posted
    The database is Access 2003.
    I have a table with a numeric field (Diabetic_Type) defined as type Numeric Byte which is set as not required, as it is not a mandatory field.
     
    The source for this field on the webform is a radiobutton . These map to an empty string if the field is not completed. This of course does not match to an integer field in the database, so I defined a variable of type 'Nullable of (Byte)' and setup some logic to set it to nothing if the radiobutton is empty.
     
    I then make a call to a BLL layer method for the actual update, passing all of the form fields, and wish to utilise the following syntax in the BLL.
    Note: I first retrieve the row to be updated - survey_dr - which is a datarow class belonging to a typed datatable created off the database schema.
     
    If Not Diabetic_Type.HasValue Then
                survey_dr.Diabetic_Type = SetDiabetic_TypeNull()       
    Else
                survey_dr.Diabetic_Type = Diabetic_Type    ' passed into BLL as a parameter
    End If

    I receive a syntax error on SetDiabetic_TypeNull() saying it doesn't provide a value.

    I am trying to set it to null because the field in the table is set to not required.

    To get it to work, I have to set the field to 'nothing', which then updates the database field with zero. This is not want I want.

    Why can't I use the datarow class method SetDiabetic_TypeNull()

     

    Thanks       

    Friday, June 5, 2009 5:19 AM

Answers

  • User438705957 posted

    I'm afraid that doesn't work as it complains about not being able to convert a type of system DBNull to Byte.I ended up doing a death march through Google and found the problem posted many times and no actual solution. So here it is:

    What is expected is to use the inbuilt datarow class methods to set the field to null before update. The syntax as it applies to my situation is survey_dr.SetDiabetic_TypeNull(). So for any other field it would be survey_dr.Set@FieldnameNull where @Fieldname is the name of the field.
    The full code is:

    If Not Diabetic_Type.HasValue Then
    survey_dr.SetDiabetic_TypeNull()
    Else
    survey_dr.Diabetic_Type = Diabetic_Type
    End If

    Note the use of Diabetic_Type.HasValue to check if the nullable type has a value.

    On extraction there are inbuilt methods to test if the nullable field extracted from the database contains nulls which must be employed to avoid errors.

    If Survey_dt(0).IsDiabetic_TypeNull Then
    Me.rblDiabeticType.SelectedValue = Nothing
    Else
    Me.rblDiabeticType.SelectedValue = Survey_dt(0).Diabetic_Type
    End If

    I had a look at the field properties in the datatable generated by the typed dataset, and under the attribute DataType there is no type listed as Nullable of (Byte) or Nullable of anything for that matter. So it will recieve a conversion error when trying to set it to null. I am guessing this is because the standard types such as integer, byte etc are value types which always have a default value and cannot be null. Nullable types are actually reference types. There is a discussion of nullable types here: http://visualbasic.about.com/od/usingvbnet/a/nullabletypes.htm

     

     

     

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, June 9, 2009 7:45 PM

All replies

  • User-1360095595 posted

    Try seeting it like this:

    survey_dr.Diabetic_Type = DBNull.Value

    Friday, June 5, 2009 7:50 AM
  • User438705957 posted

    I'm afraid that doesn't work as it complains about not being able to convert a type of system DBNull to Byte.I ended up doing a death march through Google and found the problem posted many times and no actual solution. So here it is:

    What is expected is to use the inbuilt datarow class methods to set the field to null before update. The syntax as it applies to my situation is survey_dr.SetDiabetic_TypeNull(). So for any other field it would be survey_dr.Set@FieldnameNull where @Fieldname is the name of the field.
    The full code is:

    If Not Diabetic_Type.HasValue Then
    survey_dr.SetDiabetic_TypeNull()
    Else
    survey_dr.Diabetic_Type = Diabetic_Type
    End If

    Note the use of Diabetic_Type.HasValue to check if the nullable type has a value.

    On extraction there are inbuilt methods to test if the nullable field extracted from the database contains nulls which must be employed to avoid errors.

    If Survey_dt(0).IsDiabetic_TypeNull Then
    Me.rblDiabeticType.SelectedValue = Nothing
    Else
    Me.rblDiabeticType.SelectedValue = Survey_dt(0).Diabetic_Type
    End If

    I had a look at the field properties in the datatable generated by the typed dataset, and under the attribute DataType there is no type listed as Nullable of (Byte) or Nullable of anything for that matter. So it will recieve a conversion error when trying to set it to null. I am guessing this is because the standard types such as integer, byte etc are value types which always have a default value and cannot be null. Nullable types are actually reference types. There is a discussion of nullable types here: http://visualbasic.about.com/od/usingvbnet/a/nullabletypes.htm

     

     

     

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, June 9, 2009 7:45 PM