bug when I export the contents of a GridView control to an Excel f

Discussion in 'ASP .Net' started by =?Utf-8?B?UGV0ZXI=?=, May 16, 2006.

  1. I experienced a bug, "Control GridView1 of type GridView myst be placed
    inside a form tag with runat=server". But I did so. I do not why, please
    advise. my code as below. Thanks.
    Peter


    <%@ Page language="C#" %>
    <script runat="server">
    protected void Page_Load(object sender, EventArgs e)
    {
    // Response.Write("");
    TheLabel.Text = " ";
    }

    void doExport(object sender, EventArgs e) {

    string connectionString
    = "server=myServer;database=myDatabase;uid=JohnDoe;pwd=xyz";
    System.Data.SqlClient.SqlConnection con
    = new System.Data.SqlClient.SqlConnection(connectionString);
    using (con)
    {
    con.Open();
    TheLabel.Text = "<b>table tblGeoMA</b> ";
    string sql = "select * from tblGeoMA";
    System.Data.SqlClient.SqlDataAdapter da
    = new System.Data.SqlClient.SqlDataAdapter(sql, con);

    System.Data.DataSet ds = new System.Data.DataSet();
    da.Fill(ds,"dsTable1");

    GridView1.DataSource = ds;
    GridView1.DataMember = "dsTable1";


    TheLabel.Text = " export the data in GridView to excel ";
    Response.Clear();
    Response.AddHeader("content-disposition",
    "attachment;filename=peter1.xls");
    Response.Charset = "";
    Response.Cache.SetCacheability(HttpCacheability.NoCache);
    Response.ContentType = "application/vnd.xls";
    System.IO.StringWriter stringWrite = new System.IO.StringWriter();
    System.Web.UI.HtmlTextWriter htmlWrite
    = new System.Web.UI.HtmlTextWriter(stringWrite);
    GridView1.DataBind();
    GridView1.RenderControl(htmlWrite);
    //Response.Write(stringWrite.ToString());
    Response.Write("hello");
    Response.End();
    }
    }
    </script>




    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
    "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">

    <html xmlns="http://www.w3.org/1999/xhtml" >
    <head runat="server">
    <title>GridView2Excel - test 5</title>
    </head>
    <body>
    <form id="form1" runat="server">
    <asp:button runat="server" text="export to excel" onclick="doExport"
    /><br />
    <asp:label runat="server" text="" id="TheLabel" />
    <asp:GridView runat="server" id="GridView1" />
    </form>
    </body>
    </html>
    =?Utf-8?B?UGV0ZXI=?=, May 16, 2006
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?U3JpZGhhcg==?=

    using Microsoft Excel image for Export to Excel button

    =?Utf-8?B?U3JpZGhhcg==?=, Dec 9, 2005, in forum: ASP .Net
    Replies:
    0
    Views:
    3,081
    =?Utf-8?B?U3JpZGhhcg==?=
    Dec 9, 2005
  2. Replies:
    2
    Views:
    3,122
    Jacob
    Mar 7, 2006
  3. =?Utf-8?B?SGVtYW50IFNpcGFoaW1hbGFuaQ==?=

    Export to Excel (Default File Type - Excel)

    =?Utf-8?B?SGVtYW50IFNpcGFoaW1hbGFuaQ==?=, Apr 24, 2006, in forum: ASP .Net
    Replies:
    15
    Views:
    17,466
    syed hassan
    May 21, 2009
  4. =?Utf-8?B?bWlrZQ==?=

    Export GridView to Excel does not open in Browser

    =?Utf-8?B?bWlrZQ==?=, Jun 13, 2006, in forum: ASP .Net
    Replies:
    3
    Views:
    5,600
    malilla_man
    Jun 12, 2007
  5. Grey
    Replies:
    4
    Views:
    1,940
    Mark Rae [MVP]
    Oct 17, 2007
Loading...

Share This Page