Connecting Tech Pros Worldwide Forums | Help | Site Map

problem with export data to excel. Helppppp

forumaic
Guest
 
Posts: n/a
#1: Jan 19 '07
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.


Chan Ming Man
Guest
 
Posts: n/a
#2: Jan 20 '07

re: problem with export data to excel. Helppppp


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

chanmm

"forumaic" <forumaiC@gmail.comwrote in message
news:1169220029.450546.73930@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.

Closed Thread