Click here to Skip to main content
15,890,438 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have two Export to CSV:

ASP.NET
<td align="right" width="50px">
                                    <asp:LinkButton ID="lnkExportCSV" runat="server"  OnClick="lnkExportCSV_Click"> <table border="0" cellpadding="0" cellspacing="0" width="40px" ><tr align="center"><td><img alt="Export to CSV" src="../Images/Export34.gif" width="32" height="32" /></td></tr><tr align="center"><td>CSV </td></tr></table></asp:LinkButton>
                                </td>
                                <td align="right" width="100px">
                                    <asp:LinkButton ID="lnkExportCSVWO" runat="server"  OnClick="lnkExportWOCSV_Click"> <table border="0" cellpadding="0" cellspacing="0" width="100px" ><tr align="center"><td><img alt="Export to CSV" src="../Images/Export34.gif" width="32" height="32" /></td></tr><tr align="center"><td>CSV for Work Order</td></tr></table></asp:LinkButton>
                                </td>


And here are their codes:
C#
protected void lnkExportCSV_Click(object sender, EventArgs e)
    {
        var dateRange = string.Format("{0:MMdd}", Convert.ToDateTime(Session["DateFrom"])) + "_" + string.Format("{0:MMdd}", Convert.ToDateTime(Session["DateTo"]));
        //lblGroup.Text = Convert.ToString(ddlGroups.SelectedItem);
        //lblLocation.Text = Convert.ToString(ddlLocations.SelectedItem);
        //lblDateRange.Text = "From " + txtDateFrom.Text + " to " + txtDateTo.Text;
        if (grvExport.Rows.Count > 0)
        {
            Response.Clear();
            Response.Buffer = true;
            Response.AddHeader("content-disposition", "attachment;filename=StatementPaymentsReport_" + dateRange + ".csv");
            //Response.Charset = "ISO-8859-1";
            Response.ContentEncoding = System.Text.Encoding.UTF8;
            Response.ContentType = "text/csv; charset=utf-8";

            StringBuilder sb = new StringBuilder();
            sb.Append("\uFEFF");

            sb.Append("\"Region: " + HttpUtility.HtmlDecode(Convert.ToString(ViewState["Region"])).Replace("\"", "\"\"") + "\",");
            sb.Append("\r\n");
            sb.Append("\"Location: " + HttpUtility.HtmlDecode(Convert.ToString(ViewState["Location"])).Replace("\"", "\"\"") + "\",");
            sb.Append("\r\n");
            sb.Append("\"Group: " + HttpUtility.HtmlDecode(Convert.ToString(ViewState["Group"])).Replace("\"", "\"\"") + "\",");
            sb.Append("\r\n");
            sb.Append("\"From " + HttpUtility.HtmlDecode(Convert.ToString(Session["DateFrom"])).Replace("\"", "\"\"") + " to " + HttpUtility.HtmlDecode(Convert.ToString(Session["DateTo"])).Replace("\"", "\"\"") + "\",");
            sb.Append("\r\n"); 

            for (int i = 0; i < grvExport.Columns.Count; i++)
            {
                sb.Append('"' + grvExport.Columns[i].HeaderText + '"' + ',');
            }
            sb.Append("\r\n");

            for (int a = 0; a < grvExport.Rows.Count; a++)
            {
                for (int col = 0; col < grvExport.Columns.Count; col++)
                {
                    string value;
                    //if (col == 4)
                    //    value = '"' + HttpUtility.HtmlDecode(((Label)grvExport.Rows[a].Cells[col].FindControl("lblAmount")).Text.Replace(Session["GeneralCurrency"].ToString(), string.Empty)) + '"' + ',';
                    //else
                    // value = '"' + HttpUtility.HtmlDecode(grvExport.Rows[a].Cells[col].Text) + "\","
                              value =  HttpUtility.HtmlDecode(grvExport.Rows[a].Cells[col].Text);
                    //string value = '"' + HttpUtility.HtmlDecode(((Label)grvExport.Rows[a].Cells[0].FindControl("lblDate")).Text) + '"' + ',' +
                    //               '"' + HttpUtility.HtmlDecode(((Label)grvExport.Rows[a].Cells[1].FindControl("lblReferrer")).Text) + '"' + ',' +
                    //               '"' + HttpUtility.HtmlDecode(((Label)grvExport.Rows[a].Cells[2].FindControl("lblLocationName")).Text) + '"' + ',' +
                    //               '"' + HttpUtility.HtmlDecode(((Label)grvExport.Rows[a].Cells[3].FindControl("lblInvoice")).Text) + '"' + ',' +
                    //               '"' + HttpUtility.HtmlDecode(((Label)grvExport.Rows[a].Cells[4].FindControl("lblOrderType")).Text) + '"' + ',' +
                    //               '"' + HttpUtility.HtmlDecode(((Label)grvExport.Rows[a].Cells[5].FindControl("lblAmount1")).Text.Replace(Session["GeneralCurrency"].ToString(), string.Empty)) + '"' + ',';

                    // value = value.Replace("\"", "\"\"");
                        value = value.Replace("\"", "\"\"");

                        sb.Append('"' + value.Trim() + '"' + ',');
                   // sb.Append(value);
                   
                }
                sb.Append("\r\n");
            }

            //footer 09262016
            for (int i = 0; i < grvExport.Columns.Count; i++)
            {
                string value = grvExport.FooterRow.Cells[i].Text.Replace(" ", string.Empty);
                sb.Append('"' + value + "\",");
            }
            Response.Output.Write(sb.ToString());
            Response.Flush();
            Response.End();
        }
        else
        {
            ScriptManager.RegisterStartupScript(this, GetType(), "showalert", "alert('No records to export!');", true);
        }

    }

    protected void lnkExportWOCSV_Click(object sender, EventArgs e)
    {
        var dateRange = string.Format("{0:MMdd}", Convert.ToDateTime(Session["DateFrom"])) + "_" + string.Format("{0:MMdd}", Convert.ToDateTime(Session["DateTo"]));
        //lblGroup.Text = Convert.ToString(ddlGroups.SelectedItem);
        //lblLocation.Text = Convert.ToString(ddlLocations.SelectedItem);
        //lblDateRange.Text = "From " + txtDateFrom.Text + " to " + txtDateTo.Text;
        if (grvExport.Rows.Count > 0)
        {
            Response.Clear();
            Response.Buffer = true;
            Response.AddHeader("content-disposition", "attachment;filename=StatementPaymentsReport_" + dateRange + ".csv");
            //Response.Charset = "ISO-8859-1";
            Response.ContentEncoding = System.Text.Encoding.UTF8;
            Response.ContentType = "text/csv; charset=utf-8";

            StringBuilder sb = new StringBuilder();
            sb.Append("\uFEFF");

            sb.Append("\"Region: " + HttpUtility.HtmlDecode(Convert.ToString(ViewState["Region"])).Replace("\"", "\"\"") + "\",");
            sb.Append("\r\n");
            sb.Append("\"Location: " + HttpUtility.HtmlDecode(Convert.ToString(ViewState["Location"])).Replace("\"", "\"\"") + "\",");
            sb.Append("\r\n");
            sb.Append("\"Group: " + HttpUtility.HtmlDecode(Convert.ToString(ViewState["Group"])).Replace("\"", "\"\"") + "\",");
            sb.Append("\r\n");
            sb.Append("\"From " + HttpUtility.HtmlDecode(Convert.ToString(Session["DateFrom"])).Replace("\"", "\"\"") + " to " + HttpUtility.HtmlDecode(Convert.ToString(Session["DateTo"])).Replace("\"", "\"\"") + "\",");
            sb.Append("\r\n");

            for (int i = 0; i < grvExport.Columns.Count; i++)
            {
                sb.Append('"' + grvExport.Columns[i].HeaderText + '"' + ',');
            }
            sb.Append("\r\n");

            for (int a = 0; a < grvExport.Rows.Count; a++)
            {
                for (int col = 0; col < grvExport.Columns.Count; col++)
                {
                    string value;
                    //if (col == 4)
                    //    value = '"' + HttpUtility.HtmlDecode(((Label)grvExport.Rows[a].Cells[col].FindControl("lblAmount")).Text.Replace(Session["GeneralCurrency"].ToString(), string.Empty)) + '"' + ',';
                    //else
                    // value = '"' + HttpUtility.HtmlDecode(grvExport.Rows[a].Cells[col].Text) + "\","
                    value = HttpUtility.HtmlDecode(grvExport.Rows[a].Cells[col].Text);
                    //string value = '"' + HttpUtility.HtmlDecode(((Label)grvExport.Rows[a].Cells[0].FindControl("lblDate")).Text) + '"' + ',' +
                    //               '"' + HttpUtility.HtmlDecode(((Label)grvExport.Rows[a].Cells[1].FindControl("lblReferrer")).Text) + '"' + ',' +
                    //               '"' + HttpUtility.HtmlDecode(((Label)grvExport.Rows[a].Cells[2].FindControl("lblLocationName")).Text) + '"' + ',' +
                    //               '"' + HttpUtility.HtmlDecode(((Label)grvExport.Rows[a].Cells[3].FindControl("lblInvoice")).Text) + '"' + ',' +
                    //               '"' + HttpUtility.HtmlDecode(((Label)grvExport.Rows[a].Cells[4].FindControl("lblOrderType")).Text) + '"' + ',' +
                    //               '"' + HttpUtility.HtmlDecode(((Label)grvExport.Rows[a].Cells[5].FindControl("lblAmount1")).Text.Replace(Session["GeneralCurrency"].ToString(), string.Empty)) + '"' + ',';

                    // value = value.Replace("\"", "\"\"");
                    value = value.Replace("\"", "\"\"");

                    sb.Append('"' + value.Trim() + '"' + ',');
                    // sb.Append(value);

                }
                sb.Append("\r\n");
            }

            //footer 09262016
            for (int i = 0; i < grvExport.Columns.Count; i++)
            {
                string value = grvExport.FooterRow.Cells[i].Text.Replace(" ", string.Empty);
                sb.Append('"' + value + "\",");
            }
            Response.Output.Write(sb.ToString());
            Response.Flush();
            Response.End();
        }
        else
        {
            ScriptManager.RegisterStartupScript(this, GetType(), "showalert", "alert('No records to export!');", true);
        }
    }


Here is the error that happens when I press lnkExportCSVWO. This is in inspect element in browser:
Uncaught Error: Sys.WebForms.PageRequestManagerParserErrorException: The message received from the server could not be parsed.
    at Function.Error$create [as create] (ScriptResource.axd:237)
    at Sys$WebForms$PageRequestManager$_createPageRequestManagerParserError [as _createPageRequestManagerParserError] (ScriptResource.axd:665)
    at Sys$WebForms$PageRequestManager$_parseDelta [as _parseDelta] (ScriptResource.axd:1435)
    at Sys$WebForms$PageRequestManager$_onFormSubmitCompleted [as _onFormSubmitCompleted] (ScriptResource.axd:1314)
    at Array.<anonymous> (ScriptResource.axd:47)
    at ScriptResource.axd:3484
    at Sys$Net$WebRequest$completed [as completed] (ScriptResource.axd:6373)
    at XMLHttpRequest.Sys$Net$XMLHttpExecutor._onReadyStateChange (ScriptResource.axd:5993)


For try-catch in the program:
ex = {Unable to evaluate expression because the code is optimized or a native frame is on top of the call stack.}


lnkExportCSVWO is not working but lnkExportCSV is working and they both have the same codes. Both link buttons should export a CSV from a Gridview.

What I have tried:

Haven't found a solution yet in google that is the same as mine.
Posted
Updated 9-Apr-17 16:58pm
v4
Comments
CHill60 6-Apr-17 3:23am    
You will never find solutions by Googling. Learn to debug.
I can't find anywhere in your code that provides code for lnkExportCSVWO. You do have a function lnkExportWOCSV_Click - is that the one you mean isn't working?
Michelle Anne E. Rigor 6-Apr-17 3:34am    
I know how to debug. And lnkExportCSVWO is the link that does not work. The lnkExportWOCSV_Click is the onclick function.
CHill60 6-Apr-17 5:40am    
Then you need to share the code for lnkExportCSVWO. It is not included in the code you have posted.
ZurdoDev 6-Apr-17 7:45am    
If you know how to debug then you will be able to tell us what IS happening.
Michelle Anne E. Rigor 7-Apr-17 3:21am    
Already updated the question. If there is anything I could provide, kindly tell me so I can have a clear answer for my question. The link buttons do not work even though they have the same code inside the function, the lnkExportCSV's code is working but the lnkExportWOCSV is not working. I am not sure if Response.End() is the problem in lnkExportWOCSV or something else.

1 solution

try adding below statement on page load and give try..
protected void Page_Load(object sender, EventArgs e)
{
System.Web.UI.ScriptManager.GetCurrent(this).RegisterPostBackControl(lnkExportWOCSV); 
}

Solution by: vinod.jangle [^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900