locked
LightSwitch 2012 - new record in DB when App is loaded RRS feed

  • Question

  • Hi,

    in lightswitch2012 I have a table "Events" with columns: "Name", "Type", "Start", "Check", "User" ...
    I would like to insert new record when Application Initialize to the database with the values
    ​​of: (Name) =START, (type) =auto, (Start)= datetime(now), (​​Check) = 1, (User) = logged user ...
    How to do that?

    Partial void Application_Initialize()

    {

    //How to do that in C# ??

    INSERT INTO Events (Name, Type, Start, Check, User)

    VALUES (‘START’, ‘auto’, datetime(now), ‘1’, logged user);

           }

    And similarly when the application shuts down... (Name) =STOP, (type) =auto,(Start)= date(now), (​​Check) = 1, (User) = logged user ...

    Thanks in advance:)

    Monday, September 2, 2013 9:46 AM

Answers

  • Hi,

    Based on my understanding, you  can try the code below:

    partial void Application_Initialize()
            {
            DataWorkspace dataWorkspace = this.CreateDataWorkspace();
         Events p = dataWorkspace.ApplicationData.EventsSet.AddNew();
    
                var _with1 = p;
                _with1.Start = DateTime.Now;
                _with1.Name = "Start";
                _with1.Type = "auto";
                _with1.Check = "1";
                
                _with1.User ="YourLogUserName";
                dataWorkspace.ApplicationData.SaveChanges();
    
            } 
    

    And we can modify the value according to the type in the table.

    In addition, there is a similar question like yours.

    Please check the link below:

    Is it possible to write user code to be executed at application startup or shutdown

    http://social.msdn.microsoft.com/Forums/vstudio/en-US/f5c188ad-98e9-4349-875b-5577d072a84d/is-it-possible-to-write-user-code-to-be-executed-at-application-startup-or-shutdown

    Please let me know if I have misunderstood you.



    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    Wednesday, September 4, 2013 8:48 AM

All replies

  • Hi,

    Based on my understanding, you  can try the code below:

    partial void Application_Initialize()
            {
            DataWorkspace dataWorkspace = this.CreateDataWorkspace();
         Events p = dataWorkspace.ApplicationData.EventsSet.AddNew();
    
                var _with1 = p;
                _with1.Start = DateTime.Now;
                _with1.Name = "Start";
                _with1.Type = "auto";
                _with1.Check = "1";
                
                _with1.User ="YourLogUserName";
                dataWorkspace.ApplicationData.SaveChanges();
    
            } 
    

    And we can modify the value according to the type in the table.

    In addition, there is a similar question like yours.

    Please check the link below:

    Is it possible to write user code to be executed at application startup or shutdown

    http://social.msdn.microsoft.com/Forums/vstudio/en-US/f5c188ad-98e9-4349-875b-5577d072a84d/is-it-possible-to-write-user-code-to-be-executed-at-application-startup-or-shutdown

    Please let me know if I have misunderstood you.



    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    Wednesday, September 4, 2013 8:48 AM
  • Thank you very much, I'll check tomorrow this code :)

    Artur
    Wednesday, September 4, 2013 11:06 PM