all groups > asp.net datagrid control > june 2007 >
You're in the

asp.net datagrid control

group:

GridView issue when export to excel



GridView issue when export to excel Gunawan
6/5/2007 12:00:00 AM
asp.net datagrid control: Hi there,
I am using this code that I have found in the internet to generate excel
output.

Response.Clear();
Response.AddHeader("content-disposition",
"attachment;filename=kirim.xls");
Response.Charset = "";

// If you want the option to open the Excel file without saving than
// comment out the line below
// Response.Cache.SetCacheability(HttpCacheability.NoCache);

Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.ContentType = "application/vnd.xls";

System.IO.StringWriter stringWrite = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter htmlWrite =
new HtmlTextWriter(stringWrite);

//gv.RenderControl(htmlWrite);
this.RenderControl(htmlWrite);

Response.Write(stringWrite.ToString());
Response.End();


But I've got this error (Exception) bellow when Response.End() issue.
"Unable to evaluate expression because the code is optimized or a native
frame is on top of the call stack."

How to correct this issue?

Regards,
Gun



Re: GridView issue when export to excel MasterGaurav (www.edujini-labs.com)
6/5/2007 6:36:20 PM
[quoted text, click to view]

It should not be this.RenderControl but gridView.RenderControl(htmlWrite)

It's the GridView that has to be rendered and not the page (this)!!!

--
Happy Hacking,
Gaurav Vaish | www.mastergaurav.com
www.edujini-labs.com
http://eduzine.edujini-labs.com
-----------------------------------------

Re: GridView issue when export to excel Gunawan
6/6/2007 12:00:00 AM
Thank you for your reply

change to this
[quoted text, click to view]

I have this error when rendering GridView
"Control 'gv' of type 'GridView' must be placed inside a form tag with
runat=server."


<form id="frmRencanaKirim" runat="server">
<div>
<asp:GridView ID="gv" runat="server"
AutoGenerateColumns="False"
ForeColor="#333333"
CellPadding="2"
AllowPaging="false"
ShowFooter="True">
<HeaderStyle BackColor="#507CD1"
Font-Bold="True" ForeColor="White"/>
<FooterStyle BackColor="#507CD1"
Font-Bold="True" ForeColor="White" />
<RowStyle BackColor="#EFF3FB" Height="25px" />
<AlternatingRowStyle BackColor="White" />
<Columns>
...
</Columns>
</asp:GridView>
<asp:Label ID="lbMessage" runat="server" Text=" "
ForeColor="Red" Visible="False" />
</div>
</form>


What's wrong?


"MasterGaurav (www.edujini-labs.com)" <gaurav.vaish.nospam@nospam.gmail.com>
[quoted text, click to view]

Re: GridView issue when export to excel MasterGaurav (www.edujini-labs.com)
6/6/2007 12:00:00 AM
[quoted text, click to view]

Ensure that you override the method "VerifyRenderingInServerForm" with no
code:

public override void VerifyRenderingInServerForm(Control control)

{

//don't throw any exception!

}



--
Happy Hacking,
Gaurav Vaish | www.mastergaurav.com
www.edujini-labs.com
http://eduzine.edujini-labs.com
-----------------------------------------

Re: GridView issue when export to excel Gunawan
6/7/2007 12:00:00 AM
I have tried follow your suggestion...

After all, it back to original issue.

"Unable to evaluate expression because the code is optimized or a native
frame is on top of the call stack."


"MasterGaurav (www.edujini-labs.com)" <gaurav.vaish.nospam@nospam.gmail.com>
[quoted text, click to view]

Re: GridView issue when export to excel MasterGaurav (www.edujini-labs.com)
6/10/2007 10:20:42 PM
[quoted text, click to view]


Can you put up the entire code for download somewhere?

--
Happy Hacking,
Gaurav Vaish | www.mastergaurav.com
www.edujini-labs.com
http://eduzine.edujini-labs.com
-----------------------------------------

Re: GridView issue when export to excel Gunawan
6/11/2007 12:00:00 AM
I have, didn't I? On my first post.

Error occurred when issue Response.End();
No matter what data that I would like to show in gridview.
Regards,
Gun

"MasterGaurav (www.edujini-labs.com)" <gaurav.vaish.nospam@nospam.gmail.com>
[quoted text, click to view]

Re: GridView issue when export to excel MasterGaurav (www.edujini-labs.com)
6/13/2007 10:26:00 PM
Ah! Forgot about it :)


ok.... I have a code that looks something liks this:

protected void LinkButton1_Click(object sender, EventArgs e)
{
Response.ClearHeaders();
Response.Clear();

Response.Charset = "";
Response.ContentType = "application/vnd.ms-excel";
Response.AddHeader("Content-Disposition", "attachment;
filename=Customers.xls");

StringWriter strWriter = new StringWriter();
HtmlTextWriter hwriter = new HtmlTextWriter(strWriter);
GridView1.RenderControl(hwriter);
Response.Write(strWriter.ToString());
Response.End();

}


public override void VerifyRenderingInServerForm(Control control)
{
//don't throw any exception!
}




--
Happy Hacking,
Gaurav Vaish | www.mastergaurav.com
www.edujini-labs.com
http://eduzine.edujini-labs.com
-----------------------------------------


[quoted text, click to view]

AddThis Social Bookmark Button