Export to Excel with paging still getting errors

T

Travis

I have read many articles and I can't seem to figure out what is wrong.
I was able to get just the first page only but I need to get all the
pages. Any help is appreciated. Here is the error I am getting:

Object reference not set to an instance of an object.
Description: An unhandled exception occurred during the execution of
the current web request. Please review the stack trace for more
information about the error and where it originated in the code.

Exception Details: System.NullReferenceException: Object reference not
set to an instance of an object.

Source Error:


Line 17: </table>
Line 18: <form id="Form1" method="post" runat="server">
Line 19: <asp:datagrid id=DataGrid1 style="Z-INDEX: 100; LEFT: 0px;
POSITION: absolute; TOP: 56px" runat="server" DataKeyField="Serial #"
Font-Size="X-Small" GridLines="Vertical"
OnPageIndexChanged="DataGrid1_PageChanger" CellPadding="3"
BackColor="White" BorderWidth="1px" BorderStyle="None"
BorderColor="#999999" DataMember="iDBHAMSselect" DataSource="<%#
dataSet31 %>" Height="176px" Width="896px" AutoGenerateColumns="False"
AllowPaging="true">
Line 20: <FooterStyle ForeColor="Black"
BackColor="#CCCCCC"></FooterStyle>
Line 21: <SelectedItemStyle Font-Bold="True" ForeColor="White"
BackColor="#008A8C"></SelectedItemStyle>


Source File: C:\Inetpub\wwwroot\GlossaryCreation\Forms\WebForm1.aspx
Line: 19

Stack Trace:


[NullReferenceException: Object reference not set to an instance of an
object.]
System.Web.UI.Control.get_BindingContainer()
ASP.WebForm1_aspx.__DataBindDataGrid1(Object sender, EventArgs e) in
C:\Inetpub\wwwroot\GlossaryCreation\Forms\WebForm1.aspx:19
System.Web.UI.Control.OnDataBinding(EventArgs e)
System.Web.UI.WebControls.BaseDataList.OnDataBinding(EventArgs e)
System.Web.UI.WebControls.BaseDataList.DataBind()
GlossaryCreation.Forms.WebForm1.btnExport_Click(Object sender,
EventArgs e) in
c:\inetpub\wwwroot\glossarycreation\forms\webform1.aspx.cs:465
System.Web.UI.WebControls.Button.OnClick(EventArgs e)

System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String
eventArgument)
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler
sourceControl, String eventArgument)
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData)
System.Web.UI.Page.ProcessRequestMain()


And my code:
private void btnExport_Click(object sender, System.EventArgs e)
{
//export to excel
Response.Clear();
Response.Buffer = true;
Response.ContentType = "application/vnd.ms-excel";
Response.AddHeader("Content-Disposition","attachment;
filename=Asset_" +DateTime.Now.TimeOfDay.Ticks.ToString()+ ".xls");
Response.ContentEncoding = System.Text.Encoding.UTF7;
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(this.DataGrid1);

this.DataGrid1.AllowPaging = false;
this.DataGrid1.DataSource = this.dataSet31;
this.DataGrid1.DataBind();
this.DataGrid1.RenderControl(oHtmlTextWriter);

Response.Write(oStringWriter.ToString());

this.DataGrid1.AllowPaging = true;
this.DataGrid1.DataSource = this.dataSet31;
this.DataGrid1.DataBind();
Response.End();
}

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);
try
{
literal.Text =
System.Convert.ToString(control.GetType().GetProperty("SelectedItem").GetValue(control,
null));
}
catch
{
}
control.Parent.Controls.Remove(control);
}
else
{
if (!((control.GetType().GetProperty("Text") == null)))
{
LiteralControl literal = new LiteralControl();
control.Parent.Controls.Add(literal);
literal.Text =
System.Convert.ToString(control.GetType().GetProperty("Text").GetValue(control,
null));
control.Parent.Controls.Remove(control);
}
}
}
return;
}
 

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

Members online

No members online now.

Forum statistics

Threads
473,969
Messages
2,570,161
Members
46,708
Latest member
SherleneF1

Latest Threads

Top