locked
LightSwitch - Saving Data Programmatically RRS feed

  • Question

  • Hello guys, I am really stumped on this one.

    I have a screen in lightswitch. There is a button on the screen. When the user presses the button, the application is supposed to insert a new record into a table (ReadEmail). The table only has one field (TriggerRandNum).

    In the screen's button event, I write the following code.

    Private Sub fetchEmails_Execute()            
    ' Write your code here.            
    
    Dim em As New ReadEmail            
    em.triggerRandNum = 123            
    em.???
    
    End Sub

    Now, first of all, there is no associated collection dropped down in the Intellisense for the ReadEmail entity. So I have to use the singular entity instead the collection.

    Then, where there is ??? in the code above, I was expecting to put in something like .save or .write or .insert but nothing like that came up in the intellisense. The only data manipulation operation exposed by intellisense is .delete.

    I have recompiled the solution, shaken the PC ... to no avail.

    Thanks!

    Sunday, June 23, 2013 7:20 AM

Answers

  •            Customer c =  this.DataWorkspace.ApplicationData.Customers.AddNew();
               c.LastName = "new person";
               this.DataWorkspace.ApplicationData.SaveChanges();

    this is an example for inserting a new customer programmatically via the DataWorkspace of the screen.

    In case you have a customer visual collection on the screens viewModel, you can do:

    Customer c = this.Customers.AddNew();


    paul van bladel


    Sunday, June 23, 2013 8:12 AM

All replies

  •            Customer c =  this.DataWorkspace.ApplicationData.Customers.AddNew();
               c.LastName = "new person";
               this.DataWorkspace.ApplicationData.SaveChanges();

    this is an example for inserting a new customer programmatically via the DataWorkspace of the screen.

    In case you have a customer visual collection on the screens viewModel, you can do:

    Customer c = this.Customers.AddNew();


    paul van bladel


    Sunday, June 23, 2013 8:12 AM
  • Thanks Paul. Appreciate your time helping out.

    I have read most of the popular posts on various forums and blogs regarding LightSwitch, and posting this question is really my last resort.

    In the code view of my screen in VS2012, for some mysterious reason, the use of the "this" object and the "me" object is not available. That means that they neither list down by Intellisense nor, if I type them down without Intellisense, they will compile. Similarly, although the ApplicationData is available, but it doesn't have member properties.

    The only thing I can access through code is the singular entity ReadEmail, which I can do without accessing ApplicationData, as shown in the above code. I cannot even access the corresponding collection for it which is in plural named ReadEmails.

    For this reason, a lot of the forum posts and blogs that I've researched could not help in this situation. I'm really pulling my hair on this one. I'm really wondering why others have such a sailing time with LightSwitch codes.

    Sunday, June 23, 2013 9:04 AM
  • are you  in the code behind of a screen? What type of screen?

    paul van bladel


    Sunday, June 23, 2013 9:07 AM
  • It's a List + Detail screen, to which I have added a button, and the code is supposed to run in the button's execute event.
    Sunday, June 23, 2013 9:17 AM
  • PS. I'm coding in VB. Maybe that's a wrong choice! Oh dear..

    Flameater

    Sunday, June 23, 2013 9:24 AM
  • PS. I'm coding in VB. Maybe that's a wrong choice! Oh dear..

    Flameater

    Don't worry about coding in VB. I'm sure the vast majority of dev's here are C# developers by default but some stunning applications have been written using VB!
    Sunday, June 23, 2013 12:14 PM
  • Thank you for the assurance Paul! :)

    Having this conversation with you actually prompted me to realize that coding in VB for LightSwitch may be the problem - and unfortunately, it was!

    I solved the problem by recreating the entire project in C# for LightSwitch.

    You see, VB in LightSwitch had those particular problems - no access to the WorkSpace members, no access to collections representing your entities. By no access, I mean not only no Intellisense but also unable to compile if you were to use those objects (typing them in without using Intellisense).

    Here's the code that works in C#, almost exactly like the one you suggested.

    partial void FetchEmails_Execute()        {            // Write your code here            Sync em = this.DataWorkspace.JeevaDB.Syncs.AddNew();            em.randNumber = 111;            DataWorkspace.ApplicationData.SaveChanges();       

    }

    With that, I think the lesson here is, at least for now, if you want to code deeply in LightSwitch, better stick to C#.


    Flameater

    Monday, June 24, 2013 4:43 PM
  • Hi,

    This is rubbish. VB does the same as C#.

    Dim em = me.DataWorkspace.JeevaDB.Syncs.AddNew()

    em.randNumber = 111

    DataWorkspace.ApplicationData.SaveChanges()

    This works. Anything you can do in C#, you can do in VB.

    regards

    Sven


    Sven Elm

    Monday, June 24, 2013 7:17 PM
  • BTW For those with very keen eyesight and those who know what they are talking about, you would have noticed the typo in the copy of the SUPPOSEDLY working code. 

    Here's the one with the typo removed

    partial void FetchEmails_Execute()        
    {            
           // Write your code here            
           Sync em = this.DataWorkspace.JeevaDB.Syncs.AddNew();     
           em.randNumber = 111;                   
           DataWorkspace.JeevaDB.SaveChanges();        
    }


    Flameater

    Monday, June 24, 2013 10:52 PM
  • As stated, VB can do everything you have attempted in this post.  Not sure why you were not getting intellisense, it works perfectly for me.

    Tuesday, June 25, 2013 3:24 AM
  • hi

    can I ask you a thing?

    this code is ok but if I try to use it to write a new record in the "order detail" table , it send me an error say that the table is read only

    I think because it is a query of order header so I need a way to write directly to the detail table

    a thing like dim em =  DataWorkspace.JeevaDB.Syncs.AddNew() instead me.dataworkspace......etc

    but it doesn't work

    can you help me

    Thursday, March 27, 2014 5:28 AM