problem with export data to excel. Helppppp

F

forumaic

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(Control control) in
c:\Inetpub\wwwroot\datawarehouse\individual\default.aspx.cs:line 131
at datawarehouse.individual._default.ClearControls(Control control) in
c:\Inetpub\wwwroot\datawarehouse\individual\default.aspx.cs:line 123
at datawarehouse.individual._default.excelExport(Object sender,
ImageClickEventArgs e) in
c:\Inetpub\wwwroot\datawarehouse\individual\default.aspx.cs:line 173
at System.Web.UI.WebControls.ImageButton.OnClick(ImageClickEventArgs e)

at System.Web.UI.WebControls.ImageButton.RaisePostBackEvent(String
eventArgument)
at
System.Web.UI.WebControls.ImageButton.System.Web.UI.IPostBackEventHandler.R­aisePostBackEvent(String

eventArgument)
at System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler
sourceControl, String eventArgument)
at System.Web.UI.Page.RaisePostBackEvent(NameValueCollection 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);
}


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("SelectedItem").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").GetValue(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.
 
C

Chan Ming Man

Easy way will be you export the dataset to xml and read the xml file
directly from excel.

chanmm

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(Control control) in
c:\Inetpub\wwwroot\datawarehouse\individual\default.aspx.cs:line 131
at datawarehouse.individual._default.ClearControls(Control control) in
c:\Inetpub\wwwroot\datawarehouse\individual\default.aspx.cs:line 123
at datawarehouse.individual._default.excelExport(Object sender,
ImageClickEventArgs e) in
c:\Inetpub\wwwroot\datawarehouse\individual\default.aspx.cs:line 173
at System.Web.UI.WebControls.ImageButton.OnClick(ImageClickEventArgs e)

at System.Web.UI.WebControls.ImageButton.RaisePostBackEvent(String
eventArgument)
at
System.Web.UI.WebControls.ImageButton.System.Web.UI.IPostBackEventHandler.R­aisePostBackEvent(String

eventArgument)
at System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler
sourceControl, String eventArgument)
at System.Web.UI.Page.RaisePostBackEvent(NameValueCollection 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);
}


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("SelectedItem").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").GetValue(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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top