Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Using the wizard control - cannot use Response.Write

Posted on 2007-11-27
7
Medium Priority
?
624 Views
Last Modified: 2013-11-26
I have an export routine as follows that works great on an aspx web form.  Essentially a datareader is exported to a csv file with ability to download file onto desktop.
Here is the code snippet.

     using (StringWriter sw1 = new StringWriter(sb1))
                {
                    sw1.Write(thisReader.GetName(4) + "," + thisReader.GetName(5) + "," + thisReader.GetName(7));
                    while (thisReader.Read())
                    {
                        sw1.WriteLine(thisReader["FirstName"] + "," + thisReader["LastName"] + "," + thisReader["Address1"]);
                    }
                    Response.Clear();
                    Response.AddHeader("content-disposition", "attachment;filename=" + filename);
                    Response.Write(sw1.ToString());



Now if I put this code as part of a wizard control - I don't get the popup save dialog box.  How can I fix this?
0
Comment
Question by:mugsey
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
  • 2
  • +1
7 Comments
 
LVL 20

Expert Comment

by:REA_ANDREW
ID: 20357551
Try this:
                    Response.Clear();
                    Response.AddHeader("content-disposition", "attachment;filename=" + filename);
                    Response.Write(sw1.ToString());
 
//try adding the following line afterwards
                    Response.End();

Open in new window

0
 

Author Comment

by:mugsey
ID: 20357888
Thanks Andrew

But this still does not work.  Any more suggestions..?
0
 
LVL 7

Expert Comment

by:Maverick_Cool
ID: 20358012

            Response.ContentType = "whatever";
             Response.AddHeader("content-disposition", "attachment;filename=" + filename);
0
Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

 
LVL 7

Expert Comment

by:Maverick_Cool
ID: 20358025
Try addin contentType

            Response.ContentType = "whatever";
             Response.AddHeader("content-disposition", "attachment;filename=" + filename);
            Response.ContentType = "whatever";
             Response.AddHeader("content-disposition", "attachment;filename=" + filename);

Open in new window

0
 
LVL 20

Expert Comment

by:REA_ANDREW
ID: 20358088
try fludhing the string writer to push all the text into the StringBuilder.


     using (StringWriter sw1 = new StringWriter(sb1))
                {
                    sw1.Write(thisReader.GetName(4) + "," + thisReader.GetName(5) + "," + thisReader.GetName(7));
                    while (thisReader.Read())
                    {
                        sw1.WriteLine(thisReader["FirstName"] + "," + thisReader["LastName"] + "," + thisReader["Address1"]);
                    }
                    sw1.Flush();
                    Response.Clear();
                    Response.AddHeader("content-disposition", "attachment;filename=" + filename);
                    Response.Write(sw1.ToString());

Open in new window

0
 

Author Comment

by:mugsey
ID: 20358149
Sorry guys - this still does not work.
0
 
LVL 1

Accepted Solution

by:
JoeDW earned 2000 total points
ID: 20503620
Not sure you if you have solved this already, but this is the code I use and I know it works for me. Now granted it is for a file that exists on the drive already but I believe it might help
FileInfo file = new FileInfo(svrPath);
 
                if (file.Exists)
                {
                    // clear the current output content from the buffer 
                    Response.Clear();
                    // add the header that specifies the default filename for the Download/SaveAs dialog 
                    Response.AddHeader("Content-Disposition", "attachment; filename=" + file.Name);
                    // add the header that specifies the file size, so that the browser 
                    // can show the download progress 
                    Response.AddHeader("Content-Length", file.Length.ToString());
                    // specify that the response is a stream that cannot be read by the 
                    // client and must be downloaded 
                    Response.ContentType = "application/octet-stream";
                    // send the file stream to the client 
                    Response.WriteFile(file.FullName);
 
                    downloaded = true;
 
                }

Open in new window

0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…

636 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question