CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 1 of 1
  1. #1
    Join Date
    Oct 2012

    Problem downloading a file and saving using a SaveFileDialog!!

    I am new to programming. I am trying to download contents from a datatable on to a txt file and also I am trying to save the file using the SaveFileDialog. I am getting errors. The following is the code

    Protected Sub ThreadMethod()
            'Set the Save dialog properties
        End Sub
        Protected Sub exportToFile(ByVal delimiter As String)
            'CREATING A FILE - Open or Create the file
            Dim dt As DataTable
            Dim selectedItems As ArrayList
            Dim sb As New StringBuilder()
            Dim delim As String = delimiter
            If Session("selectedItems") Is Nothing Then
                selectedItems = New ArrayList
                selectedItems = CType(Session("selectedItems"), ArrayList)
            End If
            'get the store codes from the radgrid into an array list
            For Each item As GridItem In RadGrid3.MasterTableView.Items
                If TypeOf item Is GridDataItem Then
                    Dim dataItem As GridDataItem = CType(item, GridDataItem)
                    Dim id As String = dataItem.OwnerTableView.DataKeyValues(dataItem.ItemIndex)("Store Code").ToString()
                End If
            'iterate the arraylist to get the corresponding store information
            For m = 0 To selectedItems.Count - 1
                dt = (FileManager.GetParentCompanyStoreSearchExportFile(selectedItems(m).ToString))
                If m = 0 Then
                    For i As Integer = 0 To dt.Columns.Count - 1
                        sb.Append(dt.Columns(i).ColumnName + delimiter)
                End If
                For j As Integer = 0 To dt.Rows.Count - 1
                    For k As Integer = 0 To dt.Columns.Count - 1
                        sb.Append(dt.Rows(j)(k).ToString() + delimiter)
            Response.ContentType = "text/plain"
            Response.AddHeader("content-disposition", "attachment;filename=PCSInfoFile.txt")
            Dim objSaveFileDialog As New Windows.Forms.SaveFileDialog
            With objSaveFileDialog
                '.InitialDirectory = "C:\Documents and Settings\crodnavar\My Documents\Downloads"
                .DefaultExt = "txt"
                .FileName = "PCSInfoFile.txt"
                .Filter = "Text files (*.txt)|*.txt|All files (*.*)|*.*"
                .FilterIndex = 1
                .OverwritePrompt = True
                .Title = "Export File to"
            End With
            'Show the Save dialog and if the user clicks the Save button,
            'save the file
            If objSaveFileDialog.ShowDialog() = Windows.Forms.DialogResult.OK Then
                Dim FilePath As String
                FilePath = System.IO.Path.Combine(My.Computer.FileSystem.SpecialDirectories.MyDocuments, objSaveFileDialog.FileName)
                'Replace the contents of the file
                My.Computer.FileSystem.WriteAllText(FilePath, "C:\Documents and Settings\crodnavar\My Documents\Downloads\PCSInfoFile.txt", True)
                'MsgBox("Please Search for a store to be exported.", MsgBoxStyle.Exclamation, "Alert")
            End If
            'Catch fileException As Exception
            '    Throw fileException
            '    'Cleanup()
            '    objSaveFileDialog.Dispose()
            '    objSaveFileDialog = Nothing
            'End Try
        End Sub
        Protected Sub BtnOk_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles BtnOk.Click
            RadGrid3.Visible = True
            ExportDelimiterDiv.Visible = False
            ExportDelimiterDiv.Style.Add("display", "none")
            Dim newThread As Thread = New Thread(New ThreadStart(AddressOf ThreadMethod))
            newThread.ApartmentState = ApartmentState.STA
        End Sub
    I am getting error as Null reference at the response.clear(). Not sure why that is happening. Please Help.
    Last edited by GremlinSA; October 28th, 2012 at 01:23 PM. Reason: added code tags

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Windows Mobile Development Center

Click Here to Expand Forum to Full Width

On-Demand Webinars (sponsored)

We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.