Search code examples
visual-studio-lightswitchlightswitch-2013

How to implement duplicate functionality for items in lightswitch


I'm using LightSwitch VS 2013 to create a CRUD application. I want to create a new Data screen where the user can create a copy of any old item and be able to change some details too to create a new Item easily.

I'm trying to achieve this scenario through a local property that is bound to a textbox such that the user writes the old item id and clicks copy. then the fields in the screen will be filled with the old item details but there is an exception that occurs that I explained in this question :

Object reference not set to an instance of an object in LightSwitch

How can I achieve this scenario ?


Solution

  • You find your old object by looking it up true the dataworkspace Ofcourse you need more checks here to avoid textbox.text being wrong

    this.ItemProperty = Dataworkspace.ApplicationData.YourCollection_Single(Txtbox_property_containing_id)
    

    create a new item and take over the properties

    this.ItemProperty = Dataworkspace.ApplicationData.YourCollection.AddNew();
    this.ItemProperty.Name= _oldObject.Name
    this.ItemProperty.Age= _oldObject.Age
    

    Of course you need to make sure the _single method doesnt fail.