How to import a CSV file using Microsoft Lightswitch?

In this example we will be using the dialog box similar to what we used in exporting a CSV File.  The first step is to add a handler to a button. To add a handler you need to modify the created method of the screen to which the button resides.

Private Sub SampleScreen_Created()
     AddHandler Me.FindControl("ImportButton").ControlAvailable, AddressOf ImportButtonAvailable
End Sub

Private Sub ImportButtonAvailable(sender As Object, e As ControlAvailableEventArgs)
     Dim Button = DirectCast(e.Control, Windows.Controls.Button)
     RemoveHandler Me.FindControl("ImportButton").ControlAvailable, AddressOf ImportButtonAvailable
     AddHandler Button.Click, AddressOf ImportButtonClicked
End Sub

The second step is to add the click event. The click event will (1) open the dialog box, (2) get the file stream picked by the user, (3) parse the stream and (4) save to the table/grid.

Private Sub ImportButtonClicked(sender As Object, e As System.Windows.RoutedEventArgs)
     '(1) open the dialog box
     Dim dialog = New OpenFileDialog()
     dialog.Filter = "CSV (*.csv)|*.csv"

     If dialog.ShowDialog() = True Then

           '(2) get the stream
           Dim myStream As System.IO.FileStream = dialog.File.OpenRead
           Dim reader As New StreamReader(myStream)
           Dim inputLine As String = reader.ReadLine

           '(3) parse the stream
           Dim headers As String() = inputLine.Split(",").Select(Function(header As String) header.Trim).ToArray()
           Dim count As Int16 = headers.Count
           Dim data As String()

           inputLine = reader.ReadLine
           While Not inputLine Is Nothing
                data = inputLine.Split(",")
                If data.Count = count Then
                     Dim Field1 As String = ""
                     Dim Field2 As String = ""
                     For index As Integer = 0 To headers.Count - 1
                           Select Case headers(index)
                               Case "CSVField1"
                                   Field1 = data(index)
                               Case "CSVField2"
                                   Field2 = data(index)
                           End Select

                     '(4) save to the table
                     Dim NewRecord As SampleTable = Me.DataWorkspace.SampleData.SampleTables.AddNew()
                     NewRecord.Field1 = Field1
                     NewRecord.Field2 = Field2
                End If
                inputLine = reader.ReadLine
           End While
     End If
End Sub

Thats it.

If you find this blog helpful, you can make the donation by clicking the Paypal button below.


Software developer living in the Philippines.

Posted in Lightswitch
4 comments on “How to import a CSV file using Microsoft Lightswitch?
  1. ronnie valero says:

    Good day! thanks for sharing Glenn. I started digging LS but I was always haunted by the RADest nature of ACCESS. Example of export and import in ACCESS using VBA.

    DoCmd.TransferText acExportDelim, “ExportSpecificationNameAsYouSavedIt”, “TableNameThatYouWantToExport”, “C:\FolderName\FileName.csv”,-1

    DoCmd.TransferText TransferType:=acLinkDelim, TableName:=”tblImport”, _
    FileName:=”C:\MyData.csv”, HasFieldNames:=true

    To my early assessment LS is not really a replacement of ACCESS or the migration path for ACCESS and VFP Developers.. we can feel it.

  2. hgminerva says:

    Yes I agree. In fact, the code above was originally written by me based on the export code that was shared through this blog.

  3. ronnie valero says:

    Hi Glenn, What is your development machine specs for LS? I find it very slow in 32 bit 4gb core i3 machine both xp sp3 and win 7 os

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


Harold Glenn P. Minerva
Software Developer / Tech Enthusiast
Living in the Philippines

View Harold Glenn Minerva's profile on LinkedIn


Software Engineer - Seasonal and Range Trading Software. Magenta Trader is a powerful stock market visualization software that increases your probability of trading success.

Software Architect and Founder - is a multi-tenant cloud-based Software-as-a-Service (SaaS) business app that caters to micro, small and medium trading businesses.

CTO and Co-Founder - We give your company the leverage by providing innovative software solutions products such as Point-of-Sales (POS), Financial Information System (FMIS), Payroll and DTR (HRIS), and many more.

%d bloggers like this: