This is a migrated thread and some comments may be shown as answers.

Export to Excel not working in Try Catch block

1 Answer 97 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Siva
Top achievements
Rank 1
Siva asked on 13 Dec 2013, 06:10 AM
Hi ,
      I use the following code to export the Radgrid to Excel.It works well before.. but When I used the Try Catch block , the grid is not exporting..  In the Open,Save Cancel whatever I press it didnt respond.If I remove the Try catch it works fine.I used Need datasource too..Please help me. Thanks in Advance.

try

{

RadGrid grdexp = new RadGrid();

RadGridExp.Visible =

true;

           

 

DataSet ds = Search();

RadGridExp.DataSource = ds;

RadGridExp.DataBind();

DateTime date = DateTime.Now;

RadGridExp.MasterTableView.Controls.Add(

new LiteralControl("This report was generated on " + date));

           

RadGridExp.ExportSettings.FileName =

 

"Test";

RadGridExp.ExportSettings.OpenInNewWindow =

true;

RadGridExp.MasterTableView.ExportToExcel();

}

catch (Exception ex)

{

string rtnstr;

rtnstr =

ExceptionObject.Builderrormsg(this.Page.ToString(), "btnExport_Click", ex.Message);

Exceptionlog.Error(rtnstr);

Session[

"Errmsg"] = ex.Message;

Response.Redirect(

"NoAccess.aspx?Type=2");

}

 

1 Answer, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 2
answered on 16 Dec 2013, 05:23 AM
Hi Siva,

I guess you are trying to export a RadGrid to Excel, below is a sample i tried using try catch and it works fine at my end. I see that you are binding the RadGrid again on Export, which is not needed. Please try the following code snippet.

ASPX:
<telerik:RadGrid ID="RadGrid1" runat="server" >
   . . . . . . .
</telerik:RadGrid>
<asp:Button ID="Button1" runat="server" Text="Export" OnClick="Button1_Click" />

C#:
protected void Button1_Click(object sender, EventArgs e)
{
    try
    {        
        DateTime date = DateTime.Now;
        RadGrid1.MasterTableView.Controls.Add(new LiteralControl("This report was generated on " + date));
        RadGrid1.ExportSettings.FileName = "Test";
        RadGrid1.ExportSettings.ExportOnlyData = true;       
        RadGrid1.ExportSettings.OpenInNewWindow =true;          
        RadGrid1.MasterTableView.ExportToExcel();
    }
 
    catch (Exception ex)
    {           
        //Your Code
    }
}

Thanks,
Princy
Tags
Grid
Asked by
Siva
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
Share this question
or