Hi
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
I am getting error as Null reference at the response.clear(). Not sure why that is happening. Please Help.Code:Protected Sub ThreadMethod() exportToFile(RadioButtonListDelimiter.SelectedValue) '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 Else 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() selectedItems.Add(id) End If Next '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) Next End If sb.Append(Environment.NewLine) 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) Next Next Next sb.Append(Environment.NewLine) Response.Clear() Response.ContentType = "text/plain" Response.AddHeader("content-disposition", "attachment;filename=PCSInfoFile.txt") Response.Write(sb.ToString()) 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) Response.Flush() Response.End() 'MsgBox("Please Search for a store to be exported.", MsgBoxStyle.Exclamation, "Alert") 'Else End If 'Try '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 newThread.Start() ' End Sub


Reply With Quote
Bookmarks