Last post Jul 09, 2009 04:39 PM by mandrews1234
Jul 09, 2009 03:06 PM|mandrews1234|LINK
I have the following code that generates a csv file on a button click.
'this is if they click the export button link
'it will generate a csv file with all of the current
'data, prompt them to download it, and then delete
'that file once they have saved it.
Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
'this will write the column headers
Dim csvFile As String = Server.MapPath("Exports\Parts.csv")
Dim outFile As IO.StreamWriter = My.Computer.FileSystem.OpenTextFileWriter(csvFile, False)
outFile.WriteLine("Part Id, Part Number, Description, Price")
'connect to the database to get the data to write
'to the csv file
Dim conn As New SqlConnection
Dim query As New SqlCommand
conn.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings("orthmanConnectionString").ToString
query.Connection = conn
query.CommandText = "Select partid, partnumber, description, price from parts"
Dim lrd As SqlDataReader = query.ExecuteReader()
outFile.WriteLine(Chr(34) & lrd.Item(0).ToString & Chr(34) & "," & _
Chr(34) & lrd.Item(1).ToString & Chr(34) & "," & _
Chr(34) & lrd.Item(2).ToString & Chr(34) & "," & _
Chr(34) & lrd.Item(3).ToString & Chr(34))
'close the file
'write the file
What I want to do is open a download dialog box that will allow the user to open or save that file it has just created. I have absolutely no idea how to work with the headers correctly to do this. Could someone please help me out? Thanks,
Jul 09, 2009 04:28 PM|PavanPadavala|LINK
try adding this line at the end
Jul 09, 2009 04:39 PM|mandrews1234|LINK
That did it. For some reason that function is creating the file named the way it is supposed to but windows still thinks it is a text file, how do I specficially tell it that this is a csv file?