Avatar of ziorrinfotech
ziorrinfotech
 asked on

Asp.net gives error while downloading file "The message received from the server could not be parsed. Common causes for this error are when the response is modified by calls to Response.Write()"

I have a web page and  this contains one button, when user clicks on this button i am calling a fucntion of one of my class.
this function will write a excel file which need to be downloaded.
 ExportDB exportDB = new  ExportDB();
        exportDB.ExportDBToExcel();

After this i am calling this function which exits in my web page itself
        DownloadFile();

When i am runing the code then the excel file is getting created properly but after that when i calll the downloadfile function it exceutes every line after that it gives me this error.

===========================================================================
Sys.WebForms.PageRequestManagerParserErrorException: The message received from the server could not be parsed. Common causes for this error are when the response is modified by calls to Response.Write(), response filters, HttpModules, or server trace is enabled.

Details: Error parsing near 'ÿ <table border="1">'.
===========================================================================

Any one could help me out here.
I am giving the my complete code below.

this is my export db class code
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Text;
 
 
public class ExportDB {
    private DBFunctions _dbFunctions = null;
    private DataSet _dataset = null;
    private HtmlTable _htmlTable = null;
    HtmlTableRow _tableRow = null;
 
	public ExportDB() {
	}
 
    private void PrepareMCHTMLTable() {
        _htmlTable = new HtmlTable();
        _htmlTable.Border = 1;
        _tableRow = GetMCHTMLTableRow();
 
        _tableRow.Cells[0].InnerHtml = "<b>SNo.</b>";
        _tableRow.Cells[1].InnerHtml = "<b>Source</b>";        
        _tableRow.Cells[2].InnerHtml = "<b>ID</b>";
        _tableRow.Cells[3].InnerHtml = "<b>Title</b>";
        _tableRow.Cells[4].InnerHtml = "<b>TRANS_CODE</b>";
        _tableRow.Cells[5].InnerHtml = "<b>MIGRATED</b>";
        _tableRow.Cells[6].InnerHtml = "<b>Parent</b>";
        _tableRow.Cells[7].InnerHtml = "<b>Child</b>";
        _tableRow.Cells[8].InnerHtml = "<b>Issue</b>";
        _tableRow.BgColor = "#D4D0C8"; 
        _htmlTable.Rows.Add(_tableRow);        
    }
 
 
    private HtmlTableRow GetMCHTMLTableRow() {
        HtmlTableCell tableCellMC = null;
        HtmlTableCell tableCellOID = null;
        HtmlTableCell tableCellID = null;
        HtmlTableCell tableCellTitle = null;
        HtmlTableCell tableCellTransCode = null;
        HtmlTableCell tableCellMigrated = null;
        HtmlTableCell tableCellTopicParent = null;
        HtmlTableCell tableCellTopicChild = null;
        HtmlTableCell tableCellTopicIssue = null;
        HtmlTableRow _tableRow = null;
        
        tableCellMC = new HtmlTableCell();
        tableCellMC.Width = "90";
        tableCellMC.InnerText = "MC (Mobile)";
        tableCellOID = new HtmlTableCell();
        tableCellOID.Width = "50";
        tableCellID = new HtmlTableCell();
        tableCellID.Width = "100";
        tableCellTitle = new HtmlTableCell();
        tableCellTitle.Width = "300";
        tableCellTransCode = new HtmlTableCell();
        tableCellTransCode.Width = "100";
        tableCellMigrated = new HtmlTableCell();
        tableCellMigrated.Width = "100";
        tableCellTopicParent = new HtmlTableCell();
        tableCellTopicParent.Width = "150";
        tableCellTopicChild = new HtmlTableCell();
        tableCellTopicChild.Width = "150";
        tableCellTopicIssue = new HtmlTableCell();
        tableCellTopicIssue.Width = "300";
 
        
 
        _tableRow = new HtmlTableRow();        
        _tableRow.Cells.Add(tableCellOID);
        _tableRow.Cells.Add(tableCellMC);
        _tableRow.Cells.Add(tableCellID);
        _tableRow.Cells.Add(tableCellTitle);
        _tableRow.Cells.Add(tableCellTransCode);
        _tableRow.Cells.Add(tableCellMigrated);
        _tableRow.Cells.Add(tableCellTopicParent);
        _tableRow.Cells.Add(tableCellTopicChild);
        _tableRow.Cells.Add(tableCellTopicIssue);        
 
        return _tableRow;
    }
 
    public void ExportDBToExcel() {
        int counter = 0;
        
        DataRow[] selectedDataRow = null;
        DataRow rowParent = null;
 
        _dbFunctions = new DBFunctions();
        if (_dataset == null) { _dataset = new DataSet(); }
        _dbFunctions.GetMCFAQ(_dataset);
        if (_dataset.Tables["Topic"].Rows.Count > 0) { PrepareMCHTMLTable(); }
 
        foreach (DataRow row in _dataset.Tables["Topic"].Rows) {
            _tableRow = GetMCHTMLTableRow();
            counter += 1;
 
            _tableRow.Cells[0].InnerText = counter.ToString();
            if (row["QuestionPkey"] == DBNull.Value) {
                _tableRow.Cells[2].InnerHtml = "&nbsp";
            } else {
                _tableRow.Cells[2].InnerText = row["QuestionPkey"].ToString();
            }
 
            if (row["Title"] == DBNull.Value) {
                _tableRow.Cells[3].InnerHtml = "&nbsp";
            } else {
                _tableRow.Cells[3].InnerText = row["Title"].ToString();
            }
 
            if (row["TRANS_CODE"] == DBNull.Value) {
                _tableRow.Cells[4].InnerHtml = "&nbsp";
            } else {
                _tableRow.Cells[4].InnerText = row["TRANS_CODE"].ToString();
            }
 
            if (row["Migrated"] == DBNull.Value) {
                _tableRow.Cells[5].InnerHtml = "&nbsp";
            } else {
                _tableRow.Cells[5].InnerText = row["Migrated"].ToString();
            }
                        
            if (row["ParentPKey"] == DBNull.Value) {
                _tableRow.Cells[6].InnerHtml = "&nbsp";
            } else { 
                selectedDataRow = _dataset.Tables["TopicParent"].Select("Pkey=" + row["ParentPKey"].ToString());
                if (selectedDataRow.Length > 0) {
                    rowParent = selectedDataRow[0];
                    _tableRow.Cells[6].InnerText = rowParent["TopicName"].ToString();
                } else {
                    _tableRow.Cells[6].InnerHtml = "&nbsp";
                }
            }
 
            if (row["TopicName"] == DBNull.Value) {
                _tableRow.Cells[7].InnerHtml = "&nbsp";
            } else {
                _tableRow.Cells[7].InnerText = row["TopicName"].ToString();
            }
 
            if (row["IssueName"] == DBNull.Value) {
                _tableRow.Cells[8].InnerHtml = "&nbsp";
            } else {
                _tableRow.Cells[8].InnerText = row["IssueName"].ToString();
 
            }
 
            _htmlTable.Rows.Add(_tableRow);
        
        }
 
        PrepareExcelFile();
 
    }
 
    private void PrepareExcelFile() {
        if (_htmlTable == null) { return; }
        StringWriter tw = new StringWriter();
        HtmlTextWriter hw = new HtmlTextWriter(tw);
        _htmlTable.RenderControl(hw);
        string HtmlInfo = tw.ToString().Trim();        
        string DocFileName = "ExportDB.xls";
        string FilePathName = HttpContext.Current.Server.MapPath("Images/ExportDB.xls");        
                
        File.Delete(FilePathName);
        FileStream Fs = new FileStream(FilePathName, FileMode.Create);
        BinaryWriter BWriter = new BinaryWriter(Fs, Encoding.GetEncoding("UTF-8"));
 
        BWriter.Write(HtmlInfo);
        BWriter.Close();
        Fs.Close();
        
        tw = null;
        hw = null;
        Fs = null;
        BWriter = null; 
 
    }
 
    
}
 
 
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 
This is my download file code.
 
 private void DownloadFile() {
        string fileName = "ExportDB.xls";
        string filePathName = HttpContext.Current.Server.MapPath("Images/ExportDB.xls");
        FileStream liveStream = new FileStream(filePathName, FileMode.Open,
                                       FileAccess.Read);
 
        byte[] buffer = new byte[(int)liveStream.Length];
        liveStream.Read(buffer, 0, (int)liveStream.Length);
        liveStream.Close();
 
        Response.Clear();
        Response.ContentType = "application/octet-stream";
        Response.AddHeader("Content-Length", buffer.Length.ToString());
        Response.AddHeader("Content-Disposition", "attachment; filename=" +
                           fileName);
        Response.BinaryWrite(buffer);
        Response.End();
 
    }

Open in new window

ASP.NET

Avatar of undefined
Last Comment
AxleWack

8/22/2022 - Mon
AxleWack

are you using an updatePanel ?
ziorrinfotech

ASKER
yes, there are two update pannel on the page. but this button is out side of both pannel
AxleWack

And the data you are trying to write in  private void DownloadFile() . Is this also in the updatePanel? If it is, just as a test, try removing the UpdatePanel and do it again, and tell me if the error persists
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
ziorrinfotech

ASKER
right now, the button from where i am calling these functions do a normal postback.

this button not related to any update panel yet.
ziorrinfotech

ASKER
yes you were right, I just test the same code in a differnt page which does not have any update pannel on it.

and it worked fine. But I did not get this why it is not working with update pannel?
ASKER CERTIFIED SOLUTION
AxleWack

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.