Exporting data from a Microsoft Lightswitch application to local drive using browser is a little bit tricky. At first I thought I can just use a normal thread dispatcher to open a dialog box but it turns out it did not work on web applications.
Fortunately there is a workaround to the problem. Below are the steps on how to export a data from a Microsoft Ligthswitch web application to local drive using CSV format.
1. Know the fields and table you want to export.
2. Create a method/button in your screen. e.g., “ExportAccountToCSV”
3. Create an CSV string create Sub. See sample below:
Private Function GetAccountCSV() As String Dim csv As New StringBuilder() Dim i As Integer = 0 For Each a In mstAccounts If i = 0 Then csv.AppendFormat("AccountNumber" & "," & "AccountName" & "," & "AccountType" & System.Environment.NewLine, a) End If csv.AppendFormat(a.AccountNumber & "," & a.AccountName & "," & a.AccountType & System.Environment.NewLine, a) i = i + 1 Next If csv.Length > 0 Then Return csv.ToString(0, csv.Length - 1) Else Return "" End If End Function
4. Put an execute code in the export button “ExportAccountToCSV”
Private Sub ExportAccountToCSV_Execute() Dim csv As String = GetAccountCSV() AddHandler Me.FindControl("ExportAccountToCSV").ControlAvailable, (Function(object1, eventargs1) Dim btnExport As Button = DirectCast(eventargs1.Control, Button) AddHandler btnExport.Click, (Function(object2, eventargs2) Dim dialog = New SaveFileDialog() dialog.Filter = "CSV (*.csv)|*.csv" If dialog.ShowDialog() = True Then Using stream As New StreamWriter(dialog.OpenFile()) stream.Write(csv) stream.Close() End Using End If End Function) End Function) End Sub
That’s it. Below is my example screen that exports and imports a CSV file.
Update (July 24, 2012): Since there were so many inquiries and questions regarding this blog I’ve created and uploaded a working project: http://www.mediafire.com/?6ga3lyl54purwgf
Update (August 16, 2012): Otis Ranger, made it so that we could eliminate the annoying Silverlight message “Dialog not yet initialized” every time we re-click the export button. Below is the equivalent VB.NET code of Otis Ranger, I made some minor adjustments to make it simpler so that also we could re-use our CSV function.
Private Sub EditableTable1ItemsGrid_Created() AddHandler Me.FindControl("Export").ControlAvailable, AddressOf exportAvailable End Sub Private Sub exportAvailable(sender As Object, e As ControlAvailableEventArgs) Dim Button = DirectCast(e.Control, Button) RemoveHandler Me.FindControl("Export").ControlAvailable, AddressOf exportAvailable AddHandler Button.Click, AddressOf exportClicked End Sub Private Sub exportClicked(sender As Object, e As System.Windows.RoutedEventArgs) Dim csv As String = GetTextCSV() Dim dialog = New SaveFileDialog() dialog.Filter = "CSV (*.csv)|*.csv" If dialog.ShowDialog() = True Then Using stream As New StreamWriter(dialog.OpenFile()) stream.Write(csv) stream.Close() End Using End If End Sub
If you find this blog helpful, you can make the donation by clicking the Paypal button below.