Search code examples
c#sqlvisual-studio-lightswitch

Execute SQL Delete from LightSwitch C# button


hopefully a straight forward one here..

I have a Visual Studio LightSwitch HTML Client application which uses two Views as its data sources.

Within the application I want to have a Delete button which executes a SQL query, deleting the record from a table which has a matching ID to the one currently selected within the app.

I found this handy MSDN article which seems straight forward, but when I try to implement it I'm receiving intellisense errors. I'm a complete novice in C# so I don't have much in my arsenal to trouble shoot this with.

MSDC has this quote

4.In the Screen Designer, open the shortcut menu for the button node, and then choose Edit Execute Code.

5.Add code that resembles the following example:

partial void UpdateEmployeeInfo_Execute()
{
    DataWorkspace dataWorkspace = new DataWorkspace();
    Employee employee = this.Employees.SelectedItem;

    UpdatePersonalInfoOperation operation = 
        dataWorkspace.ApplicationData.UpdateEmployeePersonalInfoOperations.AddNew();
    operation.EmployeeID = employee.EmployeeID;
    operation.NationalIDNumber = employee.NationalIDNumber;
    operation.BirthDate = employee.BirthDate;
    operation.MaritalStatus = employee.MaritalStatus;
    operation.Gender = employee.Gender;

    dataWorkspace.ApplicationData.SaveChanges();
}

My confusion is that when you choose to EDIT EXECUTE CODE it automatically generates function tags:

myapp.ViewRecordDetails.DeleteRecord_execute = function (screen) {
    // Write code here.

};

Should the MSDN code go within that function, or am I replacing the default entirely?

When inserting the code into the auto generated tags without the partial void declaration, I get errors on dataWorkspace, employee and operation saying Expected';' If I stick the whole unaltered MSDN code within the tags or do the MSDN tags without the auto-generated tags, I also get the same error on the word void

All help appreciated. I haven't even gotten to structuring my query and establishing the connection.. one thing at a time


Solution

  • Lightswitch HTML uses JavaScript for the code behind, not C#. An example of a typical LS delete command:

    screen.Items.deleteSelected();
    

    In your case:

    myapp.ViewRecordDetails.DeleteRecord_execute = function (screen) {
        // Write code here.
        screen.Items.deleteSelected();
    };
    

    I've answered a similar question here: how to debug delete button action in Lightswitch?