Binding Nullable Foreign Key Field

Posts   
 
    
Bruce
User
Posts: 61
Joined: 18-May-2006
# Posted on: 08-Sep-2007 17:50:30   

Using version of LLBLGen downloaded within the last two weeks; Net2.0; Two Class SelfServicing; Visual Basic.Net; SQLServer2000.

I am getting a problem when binding fields that are flagged as (.Net 2.0) Generate as Nullable type.

I first noticed this in relation to an Int Foreign Key field - hence the heading of this post. However after some testing I realised that it was happening with Nullable integer fields even when they were not foreign keys. I then tested Nullable dates and found the same problem.

This is a Windows Forms project and contains an edit form which loads a single entity and binds its fields to textboxes, comboboxes and datetimepickers.

What I am finding is that when I edit one of these nullable fields, as soon as I move the cursor to another field the edited field reverts to its original value. The reversion happens between the Validating and the Validated events both of which occur.

However, I can edit the values in code (so I can work around this problem by not binding the fields but passing the entity values to the form and back to the entity in code).

If I uncheck the Net2.0 Nullable box in the LLBLGenPro designer for these fields and re-generate then the problem disappears.

I set up a very simple form to test this, removed all the relevant tables, lists and entities from the LLBLGenPro designer then added them back again, deleted the whole data project and then re-generated it, and still had the same problem.

Is this a bug in LLBLGenPro or .Net or is there something I have missed?

Since writing the above I have downloaded the new V2.5 and converted this project, re-generated it, and updated the references. The problem still exists.

Posts: 254
Joined: 16-Nov-2006
# Posted on: 09-Sep-2007 00:16:38   

Can you please post the code your using to perform the data binding?

If possible attach the source code file.

Bruce
User
Posts: 61
Joined: 18-May-2006
# Posted on: 10-Sep-2007 12:43:55   

Matt

I've written a simple demo of the problem using Northwind in SQLServer 2000.

It is attached. You'll have to change the connection pathh in the App.config file.

I used LLBLGenPro v2.5 for this, but I'm getting this problem in v2.0 as explained.

Regards Bruce

Bruce
User
Posts: 61
Joined: 18-May-2006
# Posted on: 10-Sep-2007 13:07:29   

Matt

I've written a simple demo of the problem using Northwind in SQLServer 2000.

I've had to delete a lot of stuff out to make it small enough to attach. You'll have to re-generate the DAL which I have in folder NorthwindTester\NullFieldTest.Data. I used LLBLGenPro v2.5 for this.

I deleted most of the stuff out of the NorthwindTester\NullFieldtest.Win\bin\Debug folder but I think that will recreate itself when you run the application.

You'll have to change the connection path in the App.config file.

Regards Bruce

Bruce
User
Posts: 61
Joined: 18-May-2006
# Posted on: 10-Sep-2007 19:44:13   

A lot of people seem to have looked at my post but no answers.

The test project has an LLBLGenPro data project based on Northwind in SQL Server 2000 containing one entity - Product.

The WinForm Project has one form containing two text fields.

My code code for the form is:

Imports NullFieldTest.Data.EntityClasses

Public Class fmTester

    Dim productEnt As ProductEntity

    Private Sub fmTester_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        productEnt = New ProductEntity(1)
        Me.txtCategoryId.DataBindings.Add("Text", productEnt, "CategoryId")
        Me.txtUnitPrice.DataBindings.Add("Text", productEnt, "UnitPrice")
    End Sub
End Class

If you run this and change the CategoryId then move to the UnitPrice box, the category reverts. If you change the UnitPrice and move back to the category box, the price reverts. Both of these fields are Nullable.

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39910
Joined: 17-Aug-2003
# Posted on: 11-Sep-2007 10:21:44   

Indeed very weird. Looking into it. (edit). It's odd, because when I set a breakpoint in ProductEntityBase.CategoryID's setter and change the value, it's never called...

(edit): use a different overload for Databindings.Add(): pass 'True' for formattingEnabled: Me.txtCategoryId.DataBindings.Add("Text", productEnt, "CategoryId", True)

This fixes it. This is a .NET / Winforms issue and the credits solely belong to the fine folks in the Winforms team... (the call doesn't even arrive at the entity's property if you don't specify true for formattingEnabled.

The issue is mainly caused by the fact that Nullable<T> was changed late in the release cycle of .NET 2.0, and the winforms team, apparently missed that memo.

Frans Bouma | Lead developer LLBLGen Pro
Bruce
User
Posts: 61
Joined: 18-May-2006
# Posted on: 11-Sep-2007 16:21:49   

Otis,

Thanks. I just found a blog about the problems with binding nullable types and was going to say that this works:

    Private Sub fmTester_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        productEnt = New ProductEntity(1)
        Me.txtCategoryId.DataBindings.Add("Text", productEnt, "CategoryId", True, DataSourceUpdateMode.OnPropertyChanged)
        Me.txtUnitPrice.DataBindings.Add("Text", productEnt, "UnitPrice", True, DataSourceUpdateMode.OnPropertyChanged)
    End Sub

Only to find you'd already got there.

Please note that when you edit an existing post it seems that no notification email is sent, so it would be helpful if people would add new posts rather than editing existing ones to extend them.

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39910
Joined: 17-Aug-2003
# Posted on: 11-Sep-2007 16:44:07   

About the notifications: noted, I'll make a note to our bug database for HnD to get this updated.

Frans Bouma | Lead developer LLBLGen Pro