locked
How to set default value for a lookup Combobox II RRS feed

  • Question

  • I am using a solution given by Garth F Henderson here. It worked for me in Beta 1 but now I’m getting an error:

    Can not implicitly convert type 'LightSwitchApplication.Company' to 'int'

     

    Here is my code:

    using System;

    using System.Linq;

    using System.IO;

    using System.IO.IsolatedStorage;

    using System.Collections.Generic;

    using Microsoft.LightSwitch;

    using Microsoft.LightSwitch.Framework.Client;

    using Microsoft.LightSwitch.Presentation;

    using Microsoft.LightSwitch.Presentation.Extensions;

    namespace LightSwitchApplication

    {

        public partial class CreateNewEmployee

     

        {

    ...

            partial void CreateNewEmployee_Saving(ref bool handled)

            {

                this.EmployeeProperty.Company.Id = this.DataWorkspace.ApplicationData.EmployeeSet_Single(1);

            }

        }

    Wednesday, August 10, 2011 9:02 PM

Answers

  • Hi

    Try this:

    this.EmployeeProperty.Company = this.DataWorkspace.ApplicationData.CompanySet_SingleOrDefault(1);

    Jaime

    • Marked as answer by san_pedro Thursday, August 11, 2011 1:15 AM
    Wednesday, August 10, 2011 11:23 PM

All replies

    1. Don't try to set the ID, set the Company property.
    2. Use the SingleOrDefault method, not the Single method just in case the item you request doesn't exist. (This returns null/Nothing rather than causing an error.)
    3. Defaults are best set in the Created event for the Entity rather than in the Saving event of the Screen. That way the defaults are always set no mater how the entity is created and the user can see the value (and possibly change it) before saving.

     


    Simon Jones
    Wednesday, August 10, 2011 11:05 PM
  • Hi

    Try this:

    this.EmployeeProperty.Company = this.DataWorkspace.ApplicationData.CompanySet_SingleOrDefault(1);

    Jaime

    • Marked as answer by san_pedro Thursday, August 11, 2011 1:15 AM
    Wednesday, August 10, 2011 11:23 PM
  • Ups!

    Thank you both for responding

     

    Thursday, August 11, 2011 1:19 AM
  • That would never have worked, not even in B1. It's never been possible to assign a value to the id property of an entity. You always have to retrieve a copy of the entity & assign that to the property. If you read Garth's post again, you'll see that's what he was doing.

    It's just the way that Entity Frameworks. And the Id property in LS entities is (& always has been) read-only.

    And I definitely agree with Simon on his points 2 & 3.


    Yann

    (plus ça change, plus c'est la même chose!)

    Thursday, August 11, 2011 1:39 AM
    Moderator
  • Hello Yann,

    Yes you are right about Garth's post… Ups again!

     

    But, I think Simon's third point does not fit my scenario because the company which the new employee belongs depends on a choice made by the user in a previous screen. Am I right?

     

    TY

    Friday, August 12, 2011 5:15 AM
  • If you're passing the value from a previous screen, then you *would* set it in the *Created* method of the screen, not in the "Saving" method. So in that he's absolutely correct.

    But Simon's actually talking about *entity* defaults, not so much values being passed from a previous screen, so again he's still correct.

    Hope that makes some sense.


    Yann

    (plus ça change, plus c'est la même chose!)

    Friday, August 12, 2011 5:42 AM
    Moderator
  • I don't think that a passed parameter is a "default" value if it isn't actually applied/visible until you save the record. If you set/change a value on saving how does the user know what value is going to be saved and what do they do if that isn't correct but don't know it because they can't see it or think they've set one value but the code overrides it with another?
    Simon Jones
    Friday, August 12, 2011 7:15 AM