Click here to Skip to main content
15,902,198 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
When i was try to export some data from gridview to export

Response.Clear();
Response.AddHeader("content-disposition", "attachment;filename=FileName.xls");
Response.Charset = "";
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);
dgreport.RenderControl(htmlWrite);
Response.Write(stringWrite.ToString());
Response.End();

it gives error please provide some soluation.

Error:-
Control 'ctl00_LeftPanel_dgreport' of type 'GridView' must be placed inside a form tag with runat=server.
Posted

You've placed you asp:GridView tag outside the form tag (with runat="server"). Move the GridView tag inside your form tag and it should work.
 
Share this answer
 
The whole approach is wrong. Don't export grid views or any other controls; export data.

You should have a distinct data layer; bind it with your UI. Even if you cannot use formal data binding, your UI should do the same thing: populate it from your data layer and use its events to modify data in data layer, then refresh the UI.

All the operations with data, like export, printing, etc. should be done with data layer, not UI.

For creation of Excel files, use Microsoft.Office.Interop.Excel. See:
http://msdn.microsoft.com/en-us/office/default.aspx[^],
http://msdn.microsoft.com/en-us/library/bb726434%28v=office.12%29.aspx[^],
http://msdn.microsoft.com/en-us/library/d2tx7z6d.aspx[^],
http://msdn.microsoft.com/en-us/library/microsoft.office.interop.outlook.aspx[^],
http://msdn.microsoft.com/en-us/library/microsoft.office.interop.excel.aspx[^],
http://msdn.microsoft.com/en-us/library/bb386107.aspx[^].

I suggest you learn and analyze applicability of the following architectural patterns (http://en.wikipedia.org/wiki/Architectural_pattern_(computer_science)[^]):

MVVM — Model View View Model,
http://en.wikipedia.org/wiki/Model_View_ViewModel[^],

MVC — Model-View-Controller,
http://en.wikipedia.org/wiki/Model-view-controller[^]),

MVA — Model-View-Adapter,
http://en.wikipedia.org/wiki/Model–view–adapter[^],

MVP — Model-View-Presenter,
http://en.wikipedia.org/wiki/Model-view-presenter[^].
Pay attention for the motivation of those architectures. If you understand it, you would be able to create better design ideas.

—SA
 
Share this answer
 

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



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