none
Retrieve data from gridview into excel RRS feed

  • Question

  • I have a gridview which fetches its data from SQL server, The code that i have used is shown below using which i want to export the data into an excel sheet, but it throws the following exception 

    Server Error in '/' Application.

    Control 'ctl00_m_g_e5bb414f_98f1_49bc_a614_59a3719c12e4_ctl00_BlogGridView' of type 'GridView' must be placed inside a form tag with runat=server.

    protected void ExportToExcel(object sender, EventArgs e)
            {
                Response.Clear();
                Response.Buffer = true;
                Response.AddHeader("content-disposition", "attachment;filename=GridViewExport.xls");
                Response.Charset = "";
                Response.ContentType = "application/vnd.ms-excel";
                using (StringWriter sw = new StringWriter())
                {
                    HtmlTextWriter hw = new HtmlTextWriter(sw);

                   
                    BlogGridView.AllowPaging = false;
                    this.gridview();

                    BlogGridView.HeaderRow.BackColor = Color.White;
                    foreach (TableCell cell in BlogGridView.HeaderRow.Cells)
                    {
                        cell.BackColor = BlogGridView.HeaderStyle.BackColor;
                    }
                    foreach (GridViewRow row in BlogGridView.Rows)
                    {
                        row.BackColor = Color.White;
                        foreach (TableCell cell in row.Cells)
                        {
                            if (row.RowIndex % 2 == 0)
                            {
                                cell.BackColor = BlogGridView.AlternatingRowStyle.BackColor;
                            }
                            else
                            {
                                cell.BackColor = BlogGridView.RowStyle.BackColor;
                            }
                            cell.CssClass = "textmode";
                        }
                    }

    BlogGridView.RenderControl(hw); <- Throws exception in this line

                    //style to format numbers to string
                    string style = @"<style> .textmode { mso-number-format:\@; } </style>";
                    Response.Write(style);
                    Response.Output.Write(sw.ToString());
                    Response.Flush();
                    Response.End();
                }
            }

    what shall i do 

                   
    Thursday, April 3, 2014 5:27 AM

Answers

All replies

  • Hi Aman,

    Place the Gridview control undere Form tag,

    <form id= "form1" runat="server">

    <asp:gridview id ="grd" runat="server"/>

    </form>


    Thanks
    Jaison A
    http://infomoss.blogspot.in/

    Thursday, April 3, 2014 5:31 AM
  • I have used ajax modalpopup to show the gridview, i will share that code too.

    <asp:Button ID="BlogView_Button" runat="server" Text="View" OnClick = "ButtonClickFunction"/>
          <asp:LinkButton ID = "Hidden" runat="server" ></asp:LinkButton>
       <cc1:ModalPopupExtender ID="ModalPopupExtender1" CancelControlID="CancelButton" TargetControlID="Hidden" PopupControlID="pnpPopUp" runat="server" BackgroundCssClass="modalpopupBackground">
            </cc1:ModalPopupExtender>
        <asp:Panel ID="pnpPopUp" runat="server" BackColor="White" Height="300px"
        Width="250px" style="display:none" CssClass = "modalPopup" BorderStyle="Dotted">
        <asp:Button ID="CancelButton" runat="server" Text="Cancel" OnClick = "Cancel_Button"/>
        <asp:Button ID="btnExport" runat="server" Text="Export To Excel" OnClick = "ExportToExcel" />
        <asp:UpdatePanel runat="server">
        <ContentTemplate>
       
        <asp:GridView ID="BlogGridView" runat="server" AllowPaging="True"
            CellPadding="4" EnableModelValidation="True" ForeColor="#333333"
            GridLines="None" OnPageIndexChanging="GridView1_PageIndexChanging">
            <AlternatingRowStyle BackColor="White" ForeColor="Black" />
            <EditRowStyle BackColor="White" />
            <EmptyDataRowStyle ForeColor="Black" />
            <FooterStyle BackColor="White" Font-Bold="True" ForeColor="Black" />
            <HeaderStyle BackColor="White" Font-Bold="True" ForeColor="Black"
                Wrap="False" />
            <PagerStyle BackColor="White" ForeColor="Black" HorizontalAlign="Center" />
            <RowStyle BackColor="White" ForeColor="Black" />
            <SelectedRowStyle BackColor="White" Font-Bold="True" ForeColor="Black" />
            </asp:GridView>
             </ContentTemplate>
            </asp:UpdatePanel>
        </asp:Panel> 

    Thursday, April 3, 2014 5:32 AM
  • Hi Jaison,

    I have tried that approach too, no luck

    Thursday, April 3, 2014 5:36 AM
  • Hi Aman,

    Are you trying asynchronous gridview binding?

    http://www.bryanrite.com/update-panel-gridview-and-non-asynchronous-postbacks-in-aspnet-20/

    Thanks
    Jaison A
    http://infomoss.blogspot.in/


    Thursday, April 3, 2014 5:46 AM
  • Hi Aman,

    i found one url,

    http://www.programming-free.com/2012/09/aspnet-export-grid-view-to-excel.html

    may be help to you...


    Thanks
    Jaison A
    http://infomoss.blogspot.in/

    • Marked as answer by Aman Chhatbar Thursday, April 3, 2014 12:44 PM
    Thursday, April 3, 2014 5:55 AM
  • Hi Jaison,

    It worked for me but the excel does not contain any data, its empty.

    Thursday, April 3, 2014 7:07 AM