unable to export the excel file from grid view or dataset in asp.net 3.5 with c#
<p>I have tried many things with the code, but the same code I used before isn't working in my current project. I can fetch data from the database; if I change <code>visible=true</code> then the data is also display in the grid view.</p>

<p>Following is the code:</p>

<p>aspx page code</p>

<pre><code>&lt;asp:GridView ID="gvExport" runat="server" Visible="false"
                            AutoGenerateColumns="False" CellPadding="4" ForeColor="#333333"
                            GridLines="None"&gt;
    &lt;RowStyle BackColor="#F7F6F3" ForeColor="#333333" /&gt;
    &lt;Columns&gt;
        &lt;asp:BoundField DataField="artID" HeaderText="ID" /&gt;
        &lt;asp:BoundField DataField="artName" HeaderText="Name" /&gt;
        &lt;asp:BoundField DataField="artType" HeaderText="Category" /&gt;
        &lt;asp:BoundField DataField="artWork" HeaderText="Work Type" /&gt;
        &lt;asp:BoundField DataField="artCont1" HeaderText="Contact Person (I)" /&gt;
        &lt;asp:BoundField DataField="artMob1" HeaderText="Mobile" /&gt;
        &lt;asp:BoundField DataField="artCont2" HeaderText="Contact person (II)" /&gt;
        &lt;asp:BoundField DataField="artMob2" HeaderText="Mobile" /&gt;
        &lt;asp:BoundField DataField="artPhone" HeaderText="Office Phone" /&gt;
        &lt;asp:BoundField DataField="artEmail" HeaderText="Email ID" /&gt;
        &lt;asp:BoundField DataField="artStreet" HeaderText="Street" /&gt;
        &lt;asp:BoundField DataField="artCity" HeaderText="City" /&gt;
        &lt;asp:BoundField DataField="artState" HeaderText="State" /&gt;
        &lt;asp:BoundField DataField="artCountry" HeaderText="Country" /&gt;
    &lt;/Columns&gt;
    &lt;FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" /&gt;
    &lt;PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" /&gt;
    &lt;SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" /&gt;
    &lt;HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" /&gt;
    &lt;EditRowStyle BackColor="#999999" /&gt;
    &lt;AlternatingRowStyle BackColor="White" ForeColor="#284775" /&gt;
&lt;/asp:GridView&gt;
</code></pre>

<p>Following is the back page code</p>

<pre><code>DataBaseConnection.dataBase dConnect = new dataBase();
DataSet ds = new DataSet();
DataTable dt = new DataTable();
static string exceprionString = "", artType = "", searchCity = "", searchType = "",     searchCont = "", searchSugType = "", searchSugCIty = "", fileName = "";
static int i = 0, j = 0, artID = 0;
static List&lt;string&gt; listArt = new List&lt;string&gt;();


 /*Export User Artist File As MS Excel Format*/
protected void btExport_Click(object sender, EventArgs e)
{
    if (((string)Session["userRole"]).Equals("ADMIN") || ((string)Session["userRole"]).Equals("MANAGER"))
    {
            exportFile("artist");
    }
    else
        SMS("User Doed Not Rights To Export File");
}

private void exportFile(string expportType)
{
    if (((string)Session["userRole"]).Equals("ADMIN") || ((string)Session["userRole"]).Equals("MANAGER"))
    {
            ds.Clear();
            if (expportType.Equals("artist"))
            {
                if (((string)Session["userID"]).Equals("admin"))
                    ds = dConnect.artistInfo(0, ((string)Session["userID"]), "", "", "", "", "", "", "", "", "", "", "", "", "", "admin");
                else if (((string)Session["userRole"]).Equals("MANAGER"))
                    ds = dConnect.artistInfo(0, ((string)Session["userID"]), "", "", "", "", "", "", "", "", "", "", "", "", "", "manager");
                else if (((string)Session["userRole"]).Equals("MANAGER"))
                    ds = dConnect.artistInfo(0, ((string)Session["userID"]), "", "", "", "", "", "", "", "", "", "", "", "", "", "select");
                fileName = (string)Session["userID"];
            }

            exceprionString = "";
            exceprionString = dConnect.exceptionMessage();

            if (ds.Tables[0].Rows.Count &gt; 0 &amp;&amp; exceprionString.Equals(""))
            {
                if (expportType.Equals("artist"))
                {
                    gvExport.DataSource = ds.Tables[0];
                    gvExport.DataBind();

                    ExportInExcel(gvExport);
                }
            }
            else
                SMS("File Not Export Due To Data Not Found");
    }
    else
        SMS("user Does Not Rights To Export File");
}

private void ExportInExcel(GridView grid)
{
    HtmlForm form = new HtmlForm();
    string attachment = "attachment; filename=" + fileName + ".xls";
    Response.ClearContent();
    Response.AddHeader("content-disposition", attachment);
    Response.ContentType = "application/ms-excel";
    StringWriter stw = new StringWriter();
    HtmlTextWriter htextw = new HtmlTextWriter(stw);
    form.Controls.Add(grid);
    this.Controls.Add(form);
    form.RenderControl(htextw);
    Response.Write(stw.ToString());
    //Response.Output.Write(stw.ToString());//
    //Response.Flush();//
    //Response.End();

    HttpContext.Current.ApplicationInstance.CompleteRequest();
}
</code></pre>
