Link to home
Start Free TrialLog in
Avatar of triplebd69
triplebd69

asked on

Excel Export works part time.

I have a webapp that lets the user select the table and columns to build a select statement, that gets passes to a second web form that fills a datagrid.

All this works fine.

On this form I also have a button to let the user export to excel, this only seems to part of the time.  It appears that on the times it fails the OnInit and page_load never fire.  But I don't see why it only happens part of the time.  Or is it an issue with the data in the grid?

Any help would be great.
ASKER CERTIFIED SOLUTION
Avatar of Daniel Reynolds
Daniel Reynolds
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of triplebd69
triplebd69

ASKER

Here is the code in the page load...

private void Page_Load(object sender, System.EventArgs e)
{
da = (DataObject)Session["DataObject"];
string xSel = Session["Select"].ToString();
DataSet ds = new DataSet();
            
if (Page.IsPostBack)
{
if (Session["myDataset"] != null)
{
ds = (DataSet)Session["myDataset"];
dgAdHoc.DataSource = ds;
dgAdHoc.DataBind();
}
}
else
{
using (OdbcConnection con = new OdbcConnection(Configuration.oConn))
{
con.Open();
string oCMD = Session["Select"].ToString();
OdbcDataAdapter daCMD = new OdbcDataAdapter(oCMD, con);
con.Close();
daCMD.Fill(ds);                              
Session["myDataset"] = ds;

dgAdHoc.DataSource = ds;
dgAdHoc.DataBind();
}                        
}
}

And the export button..

private void btExcelX_Click(object sender, System.EventArgs e)
{
//export to excel
Response.Clear();
Response.Buffer= true;
Response.ContentType = "application/vnd.ms-excel";
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(dgAdHoc);
dgAdHoc.RenderControl(oHtmlTextWriter);
Response.Write(oStringWriter.ToString());
//Response.End();
}