locked
Adding a new record in a Silverlight Custom Control RRS feed

  • Question

  • I'm newish to Lightswitch and think it may help me in a couple of projects, I have added a custom control in a seperate project, and "linked" it in a screen, displaying the data from the application, although the collection binding is of a diffrent type/class in the custom control.

    I can edit and delete exitsing items, what I cannot do is add a new item, this can be acheived in the custom control by two or three methods (doubleclick, context menu etc).

    I cannot seem to create a new record for "saving" in the Lightswitch Application, if I use the construct of ;

    Object = new Object();

    Where the object is a Lightswitch Entity, a collection of which is bound to the screen.

    If I use this then I get the following error message:

    "A new instance of the EntityObject class cannot be initialized because the ambient IDataWorkspace is not available.  Please use the constructor that specifies an EntitySet."

    I have trawled the forums, and searched online but cannot seem to fix this error. Does anyone have any ideas?


    Steve Rouse

    Tuesday, January 15, 2013 5:55 PM

Answers

  • Hi Yann,

    I have stumbled upon the answer (for me at least), I had tried the AddNew() for the collection previously, also in a "Dispatcher", but had a few errors, after examing the objects that were available to me I found the following code to work;

                // Get the Data Context...
                var objDataContext = (IContentItem)this.DataContext;
                // ...The Screen...
                var Screen = (Microsoft.LightSwitch.Client.IScreenObject)objDataContext.Screen;
                // ...The DataWorkspace...
                var dw = (LightSwitchApplication.DataWorkspace)Screen.Details.DataWorkspace;
     
                Object obj = dw.ApplicationData.Objects.AddNew();
               
                Screen.Details.Dispatcher.BeginInvoke(() =>
                {
                    Object1 ppt = e.CreatedObject1 as Object1;
                    UpdateObject(obj, oppt);
                });

    Steve 
    • Proposed as answer by rouses Wednesday, January 16, 2013 10:46 AM
    • Marked as answer by Angie Xu Thursday, January 24, 2013 7:57 AM
    Wednesday, January 16, 2013 10:46 AM

All replies

  • Have you tried collectionName.AddNew(), where collectionName is the name of the collection property that the grid (I assume) is bound to?

    Yann - LightSwitch Central - Click here for FREE Themes, Controls, Types and Commands
     
    Please click "Mark as Answer" if a reply answers your question. Please click "Vote as Helpful" , if you find a reply helpful.
     
    By doing this you'll help others to find answers faster.

    • Proposed as answer by rouses Wednesday, January 16, 2013 10:41 AM
    • Unproposed as answer by rouses Wednesday, January 16, 2013 10:41 AM
    Wednesday, January 16, 2013 2:49 AM
    Moderator
  • Hi Yann,

    I have stumbled upon the answer (for me at least), I had tried the AddNew() for the collection previously, also in a "Dispatcher", but had a few errors, after examing the objects that were available to me I found the following code to work;

                // Get the Data Context...
                var objDataContext = (IContentItem)this.DataContext;
                // ...The Screen...
                var Screen = (Microsoft.LightSwitch.Client.IScreenObject)objDataContext.Screen;
                // ...The DataWorkspace...
                var dw = (LightSwitchApplication.DataWorkspace)Screen.Details.DataWorkspace;
     
                Object obj = dw.ApplicationData.Objects.AddNew();
               
                Screen.Details.Dispatcher.BeginInvoke(() =>
                {
                    Object1 ppt = e.CreatedObject1 as Object1;
                    UpdateObject(obj, oppt);
                });

    Steve 
    • Proposed as answer by rouses Wednesday, January 16, 2013 10:46 AM
    • Marked as answer by Angie Xu Thursday, January 24, 2013 7:57 AM
    Wednesday, January 16, 2013 10:46 AM
  • Well done Steve! :-)

    Yann - LightSwitch Central - Click here for FREE Themes, Controls, Types and Commands
     
    Please click "Mark as Answer" if a reply answers your question. Please click "Vote as Helpful" , if you find a reply helpful.
     
    By doing this you'll help others to find answers faster.

    Thursday, January 17, 2013 3:05 AM
    Moderator
  • Actually, if it's in a separate project, how did you get your code to recognise LightSwitchApplication & ApplicationData.

    Yann - LightSwitch Central - Click here for FREE Themes, Controls, Types and Commands
     
    Please click "Mark as Answer" if a reply answers your question. Please click "Vote as Helpful" , if you find a reply helpful.
     
    By doing this you'll help others to find answers faster.

    Thursday, January 17, 2013 3:20 AM
    Moderator
  • Hi Yann,

    I added the following dll's from the client project;

    MicrosoftLightswitch.dll

    MicrosoftLightswitch.Base.Client.dll

    MicrosoftLightswitchClient.dll

    Application.Common.dll

    And added the appropriate using clauses to the class file

    Steve


    • Edited by rouses Thursday, January 17, 2013 12:24 PM
    Thursday, January 17, 2013 12:20 PM
  • You can also dispatch the "create new behavior" back to the hosting LightSwitch screen. By doing so, you have the entity type strongly typed at your disposal.


    paul van bladel

    Thursday, January 17, 2013 6:01 PM
  • paul,

    Can you expand on this, by maybe a few lines of c# code? Or a link to a sample?

    Thanks in advance

    Friday, January 18, 2013 10:46 AM
  • sure...

    You found already the logic to get to the screen object, so we continue with:

     screen.Details.Dispatcher.BeginInvoke(() =>
      {
     screen.Details.Methods["MyLightSwitchMethodInViewModel"].CreateInvocation(null).Execute();
                        });

    MyLightSwitchMethodInViewModel, is a method that you created with the designer on the viewmodel, and has as such an execute and a canExecute method.

    The above code snippet will call the Execute method of MyLightSwitchMethodInViewModel.

    By doing so, you can just write AddNew() logic as you are used to do.

    In the case your method needs params, the simplest way is to the create the params also in the viewmodel and do:

     screen.Details.Properties["MySpecialValueNeededInTheCreateLogic"].Value = somevalue;


    paul van bladel

    Friday, January 18, 2013 2:43 PM
  • Ahh yes, importing Application.Common.dll will do it.

    Yann - LightSwitch Central - Click here for FREE Themes, Controls, Types and Commands
     
    Please click "Mark as Answer" if a reply answers your question. Please click "Vote as Helpful" , if you find a reply helpful.
     
    By doing this you'll help others to find answers faster.

    Friday, January 18, 2013 3:00 PM
    Moderator
  • Thanks a lot!!! excellent! :)
    Tuesday, March 5, 2013 2:38 AM