locked
Delete All Records Lightswitch Intrinsic Table RRS feed

  • Question

  • How do I delete all records in a table.  User are uploading a spreadsheet to the application, but I would like to have the previous upload removed each time a new one is uploaded.  I tried the code below using a button on a editable data grid, but it only counts the 45 records on the grid, then I have to save, refresh, repeat.  Either I need to figure out a way to make the grid bigger or write better code.  Sorry, been only using this for 2 days.  Thanks!!

    Private Sub RemoveZones_Execute()
                ' Write your code here.
                Dim rob As Integer
                Dim i As Integer
                rob = ZoneRates.Count
                For i = 1 To rob
                    ZoneRates(0).Delete()
                   
                Next
            End Sub

    Thursday, May 16, 2013 8:30 PM

Answers

  • Hi,

    The ZoneRates Visual Collection on your screen will load data by page - default size is 45. That's why you can only delete 45 items a time using the Visual Collection. If you want to access all data, you can use the Data Workspace / Data Service and Entity Set to do so.

            Private Sub Method_Execute()
                For Each zoneRate In Me.DataWorkspace.ApplicationData.ZoneRates
                    CType(zoneRate, ZoneRate).Delete()
                Next
                Me.DataWorkspace.ApplicationData.SaveChanges()
            End Sub

    Assuming your entity type is ZoneRate.

    Best regards,
    Huy

    • Marked as answer by Angie Xu Tuesday, June 4, 2013 2:21 AM
    Saturday, May 18, 2013 1:26 AM

All replies

  • Hi,

    The ZoneRates Visual Collection on your screen will load data by page - default size is 45. That's why you can only delete 45 items a time using the Visual Collection. If you want to access all data, you can use the Data Workspace / Data Service and Entity Set to do so.

            Private Sub Method_Execute()
                For Each zoneRate In Me.DataWorkspace.ApplicationData.ZoneRates
                    CType(zoneRate, ZoneRate).Delete()
                Next
                Me.DataWorkspace.ApplicationData.SaveChanges()
            End Sub

    Assuming your entity type is ZoneRate.

    Best regards,
    Huy

    • Marked as answer by Angie Xu Tuesday, June 4, 2013 2:21 AM
    Saturday, May 18, 2013 1:26 AM
  • The example from Huy is excellent. In case you have really a lot of data, it's better to execute this operation server side.

    This example is about creating test data server side via web-api, and can be easily adapted to use Huy's code.


    paul van bladel


    Saturday, May 18, 2013 9:45 AM