Problem with Nullable Column

Posts   
 
    
Posts: 30
Joined: 21-Apr-2005
# Posted on: 13-Jun-2007 23:11:44   

The following code works to set the value of an Entity's column to NULL

 Dim Survey = New TransitionDL.EntityClasses.SurveyEntity(SurveyId)

        '-- Make sure we actually have a survey object
        If Not Survey Is Nothing AndAlso Survey.Fields.State = Fetched  Then

            '-- Get all of the survey values 
            Dim StatusCodeId As String = Nothing

            '-- Populate the survey entity
            Survey.StatusCodeId = StatusCodeId

            '-- Save the entity
            Survey.Save()

        End If

However, if you notice, I did not give the 'Survey' variable a type. When I caught this, I modified the code to this:

 Dim Survey As TransitionDL.EntityClasses.SurveyEntity = New TransitionDL.EntityClasses.SurveyEntity(SurveyId)

        '-- Make sure we actually have a survey object
                If Not Survey Is Nothing AndAlso Survey.Fields.State = Fetched  Then

            '-- Get all of the survey values 
            Dim StatusCodeId As String = Nothing

            '-- Populate the survey entity
            Survey.StatusCodeId = StatusCodeId

            '-- Save the entity
            Survey.Save()

        End If

When I do this, I get a SQL foreign key constraint violation. The generated update statement is passing a value of 0 for this column instead of NULL. In the designer, both the 'IsNullable' and 'Generate As Nullable Type' are checked. Also, the database allows NULLS for this column.

What am I missing?

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 14-Jun-2007 05:57:18   

Hi John, This little test works just fine for me:

Dim myEmployee As EmployeesEntity = New EmployeesEntity(1)
myEmployee.ReportsTo = Nothing
myEmployee.Save()

What LLBLGen version are you using? What Runtime Libraries are you using?

David Elizondo | LLBLGen Support Team
jmeckley
User
Posts: 403
Joined: 05-Jul-2006
# Posted on: 14-Jun-2007 14:46:05   

Does VB still allow non-Strict variables (Option Explicit = false or what ever it is)?

In your first example you declare the variable and set it to nothing. then you pass that variable to the Entity's property so everything works.

In the second example you declare WorkTransportation as nothing but set StatusCodeId to a variable that has not been initialized in the provided code.

Maybe VB has to make assumptions about what your doing since the variables do not appear to be explicit. It's best guess is 0 the default for integers.

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 14-Jun-2007 15:31:47   

Is Survey.StatusCodeId an integer or a string?

Posts: 30
Joined: 21-Apr-2005
# Posted on: 14-Jun-2007 19:03:43   

I am using the most recent version of LLBL (downloaded 2 days ago). The database column is defined as an integer so Survey.StatusCodeId is also an integer of nullable type.

(Please note that I changed the second code example from my original post to better reflect what I am trying to do. And this would address jmeckly's comments.)

If I do the following, it will work

 Dim Survey As TransitionDL.EntityClasses.SurveyEntity = New TransitionDL.EntityClasses.SurveyEntity(SurveyId)

        '-- Make sure we actually have a survey object
                If Not Survey Is Nothing AndAlso Survey.Fields.State = Fetched Then

            '-- Populate the survey entity
            Survey.StatusCodeId = Nothing

            '-- Save the entity
            Survey.Save()

        End If

It is only when a string with a value of 'Nothing' is used that the error occurs.

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 15-Jun-2007 09:30:41   

You should always use this: Survey.StatusCodeId = Nothing

I'm not a VB expert, I don't know what should happen when you set an integer to a string with Nothing/null as the value.

This won't even compile in C#.

It appears to me that the integer takes a 0 value in such a situation.

I think it's better to convert the string to an integere to be used to set the Survey.StatusCodeId.

Posts: 30
Joined: 21-Apr-2005
# Posted on: 15-Jun-2007 22:54:01   

The solution that I will use will be to create a helper method that takes a string parameter and returns a Nullable (Of Integer) type.

But I am still curious why my original code worked (a string value of nothing) was interpreted to a NULL database value when I did not type my entity? Can you (or anyone) explain this to me?

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39912
Joined: 17-Aug-2003
# Posted on: 17-Jun-2007 10:09:25   

It's VB.NET voodoo. You pass in a string typed value into an integer and because that integer is nullable, it 'will compile' but of course it doesnt' mean anything.

This code for example shouldn't compile: Dim s As String = Nothing mySurvey.TheIntegerField = s

but apparently it does... This is odd, as setting an int to a string isn't correct, no matter what the value of the string is.

Frans Bouma | Lead developer LLBLGen Pro