locked
Add the same data to 2 tables at once RRS feed

  • Question

  • Hi. I already seen the 'related topics' before i ask this, but i cant figure out how do i do it. The question is, when i create an form to insert data, i need to specify the table witch i want to insert data. But i want to insert the same data in 2 different tables. Can you please tell me some advices? Thanks a LOT.
    Wednesday, September 22, 2010 1:13 AM

Answers

  • As snomis pointed out, Any changes made within the Save interception methods are implicitly saved if the changed entities are contained by the same data source that is currently being saved. 

    You don't need to call SaveChanges. I also found that the Updated event wasn't getting called, so I tried Inserting instead.

    I just tried a similar scenario - created an app, created People and Employee tables, added this code (VB):

    Private Sub Employees_Inserting(ByVal entity As Employee)
      Dim p As New People
      p = Me.DataWorkspace.ApplicationData.PeopleSet.AddNew
      p.Name = entity.Name
      p.EmpID = entity.EmpID
      p.Id = entity.Id
    End Sub
    

    When I create an Employee record, the same data is inserted into my People table.

    Hope this helps,


    Steve Hoag Microsoft aka the V-Bee
    Wednesday, September 22, 2010 11:00 PM
    Moderator
  • ok i did it finnally. I need to have autoincrement on id. Ty very much specially for the pacience. Ty. 1 more question, until where can switchlight reach ? search, create registers in database, edit registers.. etc. in more advanced prespective what can we do with sl?

    Thursday, September 23, 2010 4:19 AM

All replies

  • Hi,

    You should be able to do it by writing code to insert data into the second table in the Updated event of your form. See this post for a general idea: http://social.msdn.microsoft.com/Forums/en-US/lightswitchgeneral/thread/f03034bb-d311-4cff-bb76-ee445eb325e0

    By the way, the "Related Topics" section currently pulls in topics from all forums, so much of what you see there will be "Unrelated Topics". This is something that we are hoping to get fixed.

    Hope this helps,


    Steve Hoag Microsoft aka the V-Bee
    • Proposed as answer by Ben Hayat Wednesday, September 22, 2010 3:25 AM
    Wednesday, September 22, 2010 2:36 AM
    Moderator
  • Ok, thanks. Sorry im a bit noob. I double clicked 'new datascreen' and choose 'view code' and I have this:

    private sub Createnew_saved()

    dim obj as new products

    obj.prtnumber = me.TblEntry.PrtNumber

    end sub

    this code isnt saving anyway but what i want is to save the information that user . what am I missing? Thanks!

     

    .And the updated event is the saved event right?

    Wednesday, September 22, 2010 2:47 AM
  • The Updated event occurs before the data is saved to the database. I'm not clear on what you are trying or how far along you are. Do you have a Products entity, and have you created an entry screen based on that entity? If so, in the Screen Designer for that form you can go to the Write Code dropdown and select <YourScreenName>_Updated. That will take you to the Code Editor, and you would write code there to update your second table based by passing in the values from the screen. The other post should get you started on how to do that.

    Regards,


    Steve Hoag Microsoft aka the V-Bee
    Wednesday, September 22, 2010 3:27 AM
    Moderator
  • I havent that 'Update' option in my WriteCode menu. I have: closed, saved, saving, close etc. But maybe i can do it on save event. Cause i made a breakpoint and it stopped there after save. My question is that i cant put data on my other database with this code. Im doing something wrong.
    Wednesday, September 22, 2010 3:31 AM
  • My bad. The Updated event is on the Entity, not on the Screen. Go to your Products entity and you should see it.
    Steve Hoag Microsoft aka the V-Bee
    Wednesday, September 22, 2010 3:35 AM
    Moderator
  • Ok i found the event, but what i want is in inserted event. Because when i insert and click OK, i want to copy that information to other table. But i cant do it.

    i need to do something like :

    Products_Inserted(byval entity as Products)

    Othertable.prtnumber = entity.PrtNumber

    end sub

    Wednesday, September 22, 2010 3:53 AM
  • i made the same of the example u gave me:

     partial void Orders_Updated(Order entity)
            {
                Delivery delivery = new Delivery();
                delivery.DeliveryDate = entity.DeliveryDate;
            }

    but nothing is saved this way. dont i need an savechages() or something?

    Wednesday, September 22, 2010 4:28 AM
  • Are both entities (tables) in the same Data Source?
    Steve Hoag Microsoft aka the V-Bee
    Wednesday, September 22, 2010 4:37 AM
    Moderator
  • yes they are
    Wednesday, September 22, 2010 4:38 AM
  • yes they are
    Wednesday, September 22, 2010 4:38 AM
  • Okay, assuming that you have two entities, Employees and People, and a screen based on Employees, you could do something like the following in the Employees_Updated method:

    Person p = this.DataWorkspace.ApplicationData.People.AddNew();
    p.Name = Employees.EmployeeName;
    this.DataWorkspace.ApplicationData.SaveChanges();

    Warning - this is "air code"; I don't have access to my LightSwitch box at the moment so you may have to experiment a bit.


    Steve Hoag Microsoft aka the V-Bee
    Wednesday, September 22, 2010 4:51 AM
    Moderator
  • it is invalid to call savechanges on a data service that is currently processing another save changes request. Got this error.

    Wednesday, September 22, 2010 12:11 PM
  • Any changes made within the Save interception methods are implicitly saved if the changed entities are contained by the same data source that is currently being saved.  As you found out, you cannot recursively invoke SaveChanges.

    Regarding your original attempt to create the new entity within the Save interception methods, how did you verify the entity wasn't inserted?  The entity that was newly created on the service tier will not show up on the client until it is refreshed.

    Wednesday, September 22, 2010 12:32 PM
  • i checked on sql management, but i didnt make the save, got that error.This isnt ado entity normal code right? cause in ado u just need to do sometihng like

     

    Dim save = SLProfile.Profile

    Dim profile As New Profile

    profile.AvatarName = "kim"

    profile.AvatarKey = "1234"

    save.AddObject(profile)

    SLProfile.SaveChanges()

     

    here i cant, or im missin' something.

    Wednesday, September 22, 2010 12:33 PM
  • i just want to do save this:

    Person p = this.DataWorkspace.ApplicationData.People.AddNew();
    p.Name = Employees.EmployeeName;

    'this line is wrong and i dont know how to save it, i tried everything and saw all of the forum.
    this.DataWorkspace.ApplicationData.SaveChanges();

     

    Thanks a lot.

    Wednesday, September 22, 2010 10:12 PM
  • As snomis pointed out, Any changes made within the Save interception methods are implicitly saved if the changed entities are contained by the same data source that is currently being saved. 

    You don't need to call SaveChanges. I also found that the Updated event wasn't getting called, so I tried Inserting instead.

    I just tried a similar scenario - created an app, created People and Employee tables, added this code (VB):

    Private Sub Employees_Inserting(ByVal entity As Employee)
      Dim p As New People
      p = Me.DataWorkspace.ApplicationData.PeopleSet.AddNew
      p.Name = entity.Name
      p.EmpID = entity.EmpID
      p.Id = entity.Id
    End Sub
    

    When I create an Employee record, the same data is inserted into my People table.

    Hope this helps,


    Steve Hoag Microsoft aka the V-Bee
    Wednesday, September 22, 2010 11:00 PM
    Moderator
  • ok, i tried to do it and im getting this error when i check in my SQL MANAGEMENT if the new information is there.

    "Unable to save data" - "Please correct data entry errors and try again"

    omg, am I just that bad? :S

    Thursday, September 23, 2010 1:48 AM
  • Check to make sure that you are adding values for all required fields in your second table. Also, create a Search screen for your second table and see if the new record is showing up there - you will need to refresh it before you see the update.
    Steve Hoag Microsoft aka the V-Bee
    Thursday, September 23, 2010 3:38 AM
    Moderator
  • ok i did it finnally. I need to have autoincrement on id. Ty very much specially for the pacience. Ty. 1 more question, until where can switchlight reach ? search, create registers in database, edit registers.. etc. in more advanced prespective what can we do with sl?

    Thursday, September 23, 2010 4:19 AM