How to bind List with ComboBox for Nullable Column

Posts   
 
    
Posts: 2
Joined: 28-Apr-2010
# Posted on: 28-Apr-2010 21:23:30   

Hello,

This is not entirely related to Code generation but I have a question for understanding the right architecture for using LLBLGen.

Basically lets say I have a Employee table and a Department table. Employees belong to a department. So Employee table has a foreign key called DepartmentID. However DepartmentID is Nullable.

Now when WinForms code is generated (or even by hand), when we create a form and show Departments in a Combo box. To populate we fetch all the departments as a List and bind it to ComboBox DataSource. However the problem is there is no way to specify NULL.

One of the ways I have come up with is, when binding, I create a NullEntity:


    Department nullDept = new Department();
    nullDept.ID = -1;
    deptList = departmentService.GetAll();
    deptList.Add(nullDept);
    // Databind.

Now on Save button's handler:

    btnSave_Clicked() {
          if(empForm.Employee.DepartmentID == -1) empForm.Employee.DepartmentID = null;
          // Save here.
     }

So my question is:

  1. Is this the right way? Or is there any other way?

  2. Maybe the code generators should define a static NullEmployee and NullDepartment so that we can reuse these. I can do these myself if this is the right pattern.

Basically this way we need to manually detect for null entity or NULL value. If there was a better way so that I can do something like this (pseudocode):


     deptList = GetAllIncludingNull();
     // .....

    // At save, simply call Save and it should detect NullEntity.

     Save(employee);

Regards Jack

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 29-Apr-2010 07:32:32   

jacktripper wrote:

  1. Is this the right way? Or is there any other way?

Other way is that when you don't select anything, the selected value is 0.

[quotenick="jacktripper"]2. Maybe the code generators should define a static NullEmployee and NullDepartment so that we can reuse these. I can do these myself if this is the right pattern.

jacktripper wrote:

Basically this way we need to manually detect for null entity or NULL value. If there was a better way so that I can do something like this (pseudocode):

That's not practical in a lot of cases. No needed as well coz there is a lot of workarounds.

David Elizondo | LLBLGen Support Team
Posts: 2
Joined: 28-Apr-2010
# Posted on: 29-Apr-2010 11:48:14   

daelmo wrote:

Other way is that when you don't select anything, the selected value is 0.

But that requires we leave ComboBox with editable values. I would rather have DropDownList style so user knows he only has those choices. Second, if they have selected a value, they need to delete the text to get a 0. This is error prone.

daelmo wrote:

That's not practical in a lot of cases. No needed as well coz there is a lot of workarounds.

Can you please suggest a few?

Please note, we are talking about an application that has about 800 tables. There are a LOT of drop downs. We need to implement a pattern that will be followed. Workarounds may be fragile and can break the application.

Regards Jack.

Walaa avatar
Walaa
Support Team
Posts: 14994
Joined: 21-Aug-2005
# Posted on: 29-Apr-2010 11:55:31   

I'm not sure if this is a web app or a windows app.

But you should be able to add an Empty/Dummy listItem to the dropDownList with value equal to 0.