473,466 Members | 1,286 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

problem with export data to excel. Helppppp

Hello,

I am trying to export data to excel from datagrid, and I am getting an
error: "The Controls collection cannot be modified because the control
contains code blocks (i.e. <% ... %>)."
Error details:
System.Web.HttpException was unhandled by user code
Message="The Controls collection cannot be modified because the control

contains code blocks (i.e. <% ... %>)."
Source="System.Web"
ErrorCode=-2147467259
StackTrace:
at System.Web.UI.ControlCollection.Add(Control child)
at datawarehouse.individual._default.ClearControls(Co ntrol control) in
c:\Inetpub\wwwroot\datawarehouse\individual\defaul t.aspx.cs:line 131
at datawarehouse.individual._default.ClearControls(Co ntrol control) in
c:\Inetpub\wwwroot\datawarehouse\individual\defaul t.aspx.cs:line 123
at datawarehouse.individual._default.excelExport(Obje ct sender,
ImageClickEventArgs e) in
c:\Inetpub\wwwroot\datawarehouse\individual\defaul t.aspx.cs:line 173
at System.Web.UI.WebControls.ImageButton.OnClick(Imag eClickEventArgs e)

at System.Web.UI.WebControls.ImageButton.RaisePostBac kEvent(String
eventArgument)
at
System.Web.UI.WebControls.ImageButton.System.Web.U I.IPostBackEventHandler.R*aisePostBackEvent(String

eventArgument)
at System.Web.UI.Page.RaisePostBackEvent(IPostBackEve ntHandler
sourceControl, String eventArgument)
at System.Web.UI.Page.RaisePostBackEvent(NameValueCol lection postData)
at System.Web.UI.Page.ProcessRequestMain(Boolean
includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
---------------------------------------------------------------------------*---------------------

Code that I am using for export:
Code:
private void ClearControls(Control control)
{
for (int i = control.Controls.Count - 1; i >= 0; i--)
{
ClearControls(control.Controls[i]);
}
if (!(control is TableCell))
{
if (control.GetType().GetProperty("SelectedItem") !=
null)
{
LiteralControl literal = new LiteralControl();
control.Parent.Controls.Add(literal); //on this
line I am getting an error
try
{
literal.Text =
(string)control.GetType().GetProperty("SelectedIte m").GetValue(control,

null);
}
catch
{
//error msg
}
control.Parent.Controls.Remove(control);
}
else
if (control.GetType().GetProperty("Text") != null)
{
LiteralControl literal = new LiteralControl();
control.Parent.Controls.Add(literal);
literal.Text =
(string)control.GetType().GetProperty("Text").GetV alue(control, null);
control.Parent.Controls.Remove(control);
}
}
return;
}
public void excelExport(object sender,
System.Web.UI.ImageClickEventArgs e)
{
//export to excel
lblTitle.Text = "All individuals";
Response.Clear();
Response.Buffer = true;
Response.ContentType = "application/vnd.ms-excel";
Response.Charset = "";
this.EnableViewState = false;
System.IO.StringWriter oStringWriter = new
System.IO.StringWriter();
System.Web.UI.HtmlTextWriter oHtmlTextWriter = new
System.Web.UI.HtmlTextWriter(oStringWriter);
this.ClearControls(IndividualList1);
lblTitle.RenderControl(oHtmlTextWriter);
IndividualList1.RenderControl(oHtmlTextWriter);
Response.Write(oStringWriter.ToString());
Response.End();
}
Maybe have anybody ideas how to solve this problem and fix this error.
Thanks is advance.
A.

Jan 19 '07 #1
1 2638
Easy way will be you export the dataset to xml and read the xml file
directly from excel.

chanmm

"forumaic" <fo******@gmail.comwrote in message
news:11*********************@m58g2000cwm.googlegro ups.com...
Hello,

I am trying to export data to excel from datagrid, and I am getting an
error: "The Controls collection cannot be modified because the control
contains code blocks (i.e. <% ... %>)."
Error details:
System.Web.HttpException was unhandled by user code
Message="The Controls collection cannot be modified because the control

contains code blocks (i.e. <% ... %>)."
Source="System.Web"
ErrorCode=-2147467259
StackTrace:
at System.Web.UI.ControlCollection.Add(Control child)
at datawarehouse.individual._default.ClearControls(Co ntrol control) in
c:\Inetpub\wwwroot\datawarehouse\individual\defaul t.aspx.cs:line 131
at datawarehouse.individual._default.ClearControls(Co ntrol control) in
c:\Inetpub\wwwroot\datawarehouse\individual\defaul t.aspx.cs:line 123
at datawarehouse.individual._default.excelExport(Obje ct sender,
ImageClickEventArgs e) in
c:\Inetpub\wwwroot\datawarehouse\individual\defaul t.aspx.cs:line 173
at System.Web.UI.WebControls.ImageButton.OnClick(Imag eClickEventArgs e)

at System.Web.UI.WebControls.ImageButton.RaisePostBac kEvent(String
eventArgument)
at
System.Web.UI.WebControls.ImageButton.System.Web.U I.IPostBackEventHandler.R*aisePostBackEvent(String

eventArgument)
at System.Web.UI.Page.RaisePostBackEvent(IPostBackEve ntHandler
sourceControl, String eventArgument)
at System.Web.UI.Page.RaisePostBackEvent(NameValueCol lection postData)
at System.Web.UI.Page.ProcessRequestMain(Boolean
includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
---------------------------------------------------------------------------*---------------------

Code that I am using for export:
Code:
private void ClearControls(Control control)
{
for (int i = control.Controls.Count - 1; i >= 0; i--)
{
ClearControls(control.Controls[i]);
}
if (!(control is TableCell))
{
if (control.GetType().GetProperty("SelectedItem") !=
null)
{
LiteralControl literal = new LiteralControl();
control.Parent.Controls.Add(literal); //on this
line I am getting an error
try
{
literal.Text =
(string)control.GetType().GetProperty("SelectedIte m").GetValue(control,

null);
}
catch
{
//error msg
}
control.Parent.Controls.Remove(control);
}
else
if (control.GetType().GetProperty("Text") != null)
{
LiteralControl literal = new LiteralControl();
control.Parent.Controls.Add(literal);
literal.Text =
(string)control.GetType().GetProperty("Text").GetV alue(control, null);
control.Parent.Controls.Remove(control);
}
}
return;
}
public void excelExport(object sender,
System.Web.UI.ImageClickEventArgs e)
{
//export to excel
lblTitle.Text = "All individuals";
Response.Clear();
Response.Buffer = true;
Response.ContentType = "application/vnd.ms-excel";
Response.Charset = "";
this.EnableViewState = false;
System.IO.StringWriter oStringWriter = new
System.IO.StringWriter();
System.Web.UI.HtmlTextWriter oHtmlTextWriter = new
System.Web.UI.HtmlTextWriter(oStringWriter);
this.ClearControls(IndividualList1);
lblTitle.RenderControl(oHtmlTextWriter);
IndividualList1.RenderControl(oHtmlTextWriter);
Response.Write(oStringWriter.ToString());
Response.End();
}
Maybe have anybody ideas how to solve this problem and fix this error.
Thanks is advance.
A.

Jan 20 '07 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Matt | last post by:
I have an ASP page that calls ASP routines that I created that execute a database query and return the results to a recordset. I then iterate through the recordset and display the data in a table....
4
by: D | last post by:
I've created a report with many subreports of aggregate data. I want my client to be able to export this data to Excel to make her charts, etc. Only one problem: one of the fields is a "SchoolYear"...
1
by: congngo | last post by:
Hi all Every time I export a table into an excel spreadsheet. It has a leading apostrophe on every cells. This drive me nut. I have to do a work around by export table into a txt file than...
4
by: Prasad Dannani | last post by:
Hi, We have to export our data in sql server table to any excel sheet. We have a template to export, using oledb we are inserting rows to this template and generating the excel output. The...
5
by: al | last post by:
Greetings, I used article Q317109 (http://support.microsoft.com/default.aspx?scid=kb;EN-US;Q317109) to solve the problem of Excel prcocess does not stop after close. The problem I faced with...
13
by: Hemant Sipahimalani | last post by:
The following piece of code is being used to export HTML to excel. HttpContext.Current.Response.ContentType = "application/vnd.ms-excel"...
1
by: smaczylo | last post by:
Hello, I've recently been asked to work with Microsoft Access, and while I feel quite comfortable with Excel, I'm at a complete loss with databases. If someone could help me with this issue I'm...
1
by: CoolFactor | last post by:
MY CODE IS NEAR THE BOTTOM I want to export this Access query into Excel using a command button on an Access form in the following way I describe below. Below you will find the simple query I am...
3
by: =?Utf-8?B?YzY3NjIyOA==?= | last post by:
Hi all, I have a question for you. I have a .csv file which has many lines of data. Each line has many data fields which are delimited by ",". Now I need to extract part of data from this...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.