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.


4 thoughts on “How to import a CSV file using Microsoft Lightswitch?

  1. 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. 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 )

Google+ photo

You are commenting using your Google+ 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 )


Connecting to %s