Click here to Skip to main content
15,892,199 members
Home / Discussions / ASP.NET
   

ASP.NET

 
AnswerRe: ASP 3.0 Question: Chart and Graphs suggestion Pin
Anurag Gandhi24-Jul-11 21:28
professionalAnurag Gandhi24-Jul-11 21:28 
QuestionTrouble with UpdatePanel and OnClick event attribute Pin
Dominick Marciano20-Jul-11 9:54
professionalDominick Marciano20-Jul-11 9:54 
QuestionRelated to GridView Pin
Gautam Vishwakarma20-Jul-11 5:10
Gautam Vishwakarma20-Jul-11 5:10 
AnswerRe: Related to GridView Pin
R. Giskard Reventlov20-Jul-11 6:58
R. Giskard Reventlov20-Jul-11 6:58 
AnswerRe: Related to GridView Pin
Prasanta_Prince20-Jul-11 19:47
Prasanta_Prince20-Jul-11 19:47 
Questionhow can i pass values by using post not get method? Pin
buffering8320-Jul-11 5:00
buffering8320-Jul-11 5:00 
Answersession variables Pin
David Mujica20-Jul-11 5:11
David Mujica20-Jul-11 5:11 
QuestionCode for Downloading from website problem Pin
kbalias19-Jul-11 23:22
kbalias19-Jul-11 23:22 
Hello

I am using Visual Web Developer 2010 Express (with C# as code-behind) to develop a website.

On one of the pages the user can create a report of the data and I would like the user to be able to download the data as a .csv file.

I have written the method to download the data, but the HTML of the page is appended to the data in the downloaded file.

When searching for solutions people say the Response.Clear() and Response.End() must be used in the code, but I have that and it still appends the HTML.

The download method is:
public void ExportCSV(DataTable data, string fileName)
{
    try
    {
        HttpContext context = HttpContext.Current;

        context.Response.Clear();
        context.Response.ContentType = "text/csv";
        context.Response.AddHeader("Content-Disposition", "attachment; filename=" + fileName + ".csv");

        //write column header names
        for (int i = 0; i < data.Columns.Count - 1; i++)
        {
            if (i > 0)
            {
                context.Response.Write(",");
            }
            context.Response.Write(data.Columns[i].ColumnName);
        }
        context.Response.Write(Environment.NewLine);

        //Write data
        foreach (DataRow row in data.Rows)
        {

            for (int i = 0; i < data.Columns.Count - 1; i++)
            {
                if (i > 0)
                {
                    context.Response.Write(",");
                }
                context.Response.Write(row[i]);
            }
            context.Response.Write(Environment.NewLine);
        }

        context.Response.End();
    }
    catch (Exception ex)
    {
        throw ex;
    }
}


Here is part of the file that is downloaded:

HospNumber,LastName,FirstName,DOB,Gender,RaceName,DateDied,AgeAtDeath,HospShort,RTID,
RTNum,RegistrationDate,AgeAtRegDate,OSurvival,Last seen,Next appt,Firm,Clinic,Organ,Part,Histology,Intent,Modality,StGp1,StGp2,T,N,M,Study - Other
90000000,Temp,,18/04/1955 12:00:00 AM,,,,,GSH,61227,2010.1103,14/01/2010 12:00:00 AM,,-1,,,,,,,,,,,,,,,
90001245,TestWeb,,23/02/1983 12:00:00 AM,,,23/06/2011 12:00:00 AM,028,04,00,GSH,,,,,,,,,,,,,,,,,,,,
90003214,TestWeb,,18/01/1983 12:00:00 AM,,,,,GEO,,,,,,,,,,,,,,,,,,,,
900223341,TestWeb,,23/03/1983 12:00:00 AM,,,,,GEO,,,,,,,,,,,,,,,,,,,,
90023541,TestWeb,,23/02/1983 12:00:00 AM,,,,,GEO,,,,,,,,,,,,,,,,,,,,
<script type="text/javascript" language="javascript">alert('Thread was being aborted.');</script>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head><title>

</title><link href="../Styles/Site.css" rel="stylesheet" type="text/css" />
</head>
<body>
    <form method="post" action="../Error.aspx" id="Form2">


Any help will be greatly appreciated. Thank you.

Kobus
AnswerRe: Code for Downloading from website problem [modified] Pin
Viral Upadhyay20-Jul-11 1:50
Viral Upadhyay20-Jul-11 1:50 
AnswerRe: Code for Downloading from website problem Pin
Not Active20-Jul-11 2:07
mentorNot Active20-Jul-11 2:07 
AnswerRe: Code for Downloading from website problem Pin
Prasanta_Prince20-Jul-11 19:49
Prasanta_Prince20-Jul-11 19:49 
AnswerRe: Code for Downloading from website problem Pin
kbalias21-Jul-11 20:35
kbalias21-Jul-11 20:35 
Questionpublish asp.net-silverlight-WCF in local iis Pin
yesu prakash19-Jul-11 21:05
yesu prakash19-Jul-11 21:05 
AnswerRe: publish asp.net-silverlight-WCF in local iis Pin
Viral Upadhyay20-Jul-11 1:58
Viral Upadhyay20-Jul-11 1:58 
QuestionHow to get Gridview to respond to Select command on Blackberry Browser (Curve 8530) Pin
David Mujica19-Jul-11 4:58
David Mujica19-Jul-11 4:58 
SuggestionRe: How to get Gridview to respond to Select command on Blackberry Browser (Curve 8530) Pin
Anurag Gandhi19-Jul-11 20:40
professionalAnurag Gandhi19-Jul-11 20:40 
QuestionJquery code is not working in IE [modified] Pin
meghamaharshi19-Jul-11 1:16
meghamaharshi19-Jul-11 1:16 
AnswerRe: Jquery code is not working in IE Pin
Shameel19-Jul-11 4:49
professionalShameel19-Jul-11 4:49 
AnswerRe: Jquery code is not working in IE Pin
Not Active19-Jul-11 7:49
mentorNot Active19-Jul-11 7:49 
QuestionASP.NET Login Control always logged in Pin
Dominick Marciano18-Jul-11 18:18
professionalDominick Marciano18-Jul-11 18:18 
AnswerRe: ASP.NET Login Control always logged in Pin
Dominick Marciano18-Jul-11 18:44
professionalDominick Marciano18-Jul-11 18:44 
QuestionHelp with page.header.controls.add() throw NullReferenceException. [modified] Pin
josehidalgor18-Jul-11 12:44
josehidalgor18-Jul-11 12:44 
AnswerRe: Help with page.header.controls.add() throw NullReferenceException. Pin
Parwej Ahamad19-Jul-11 8:33
professionalParwej Ahamad19-Jul-11 8:33 
QuestionGridview To Export Excel sheet Pin
vishnukamath18-Jul-11 0:07
vishnukamath18-Jul-11 0:07 
SuggestionRe: Gridview To Export Excel sheet Pin
RaviRanjanKr18-Jul-11 1:23
professionalRaviRanjanKr18-Jul-11 1:23 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.