Dinamt Table, cannot export sql query results to excel! Please help

I

IluDeR

Hi all friends,
I'm going crazy with this problem. Let me explain, I've got a search
engine, and the results ar going to a table control, not a datagrid.
The results are from full list of users or the expences they make,
like date, how much, etc.
So the user, first introduces in a textbox, the search key, like
"Google", and a table is build with the a sql query, with all the
results with "Google".

When this query ends, a button appears, and the user has the
possibility to get this result(the table) into a MS- Excel File. I've
done all the code for this, the problem, is that the headers of the
table, are build in the page load, and the rest, in the event of
pressing the search button. So the only thing that appears on the
excel file are the headers!!!!

Please help.

Here is the code for a sample file, to show the problem:

<code>
"aspx"

________________________________________________________________________

<%@ Page language="c#" Codebehind="WebForm2.aspx.cs"
AutoEventWireup="false" Inherits="Smyle.WebForm2" %>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >

<HTML>

<HEAD>

<title>WebForm2</title>

<meta name="GENERATOR" Content="Microsoft Visual Studio 7.0">

<meta name="CODE_LANGUAGE" Content="C#">

<meta name="vs_defaultClientScript" content="JavaScript">

<meta name="vs_targetSchema"
content="http://schemas.microsoft.com/intellisense/ie5">

</HEAD>

<body MS_POSITIONING="GridLayout">

<form id="WebForm2" method="post" runat="server">

<asp:Table id="DisplayTable" style="Z-INDEX: 101; LEFT: 160px;
POSITION: absolute; TOP: 160px" runat="server" Width="304px"
Height="32px"></asp:Table>

<asp:Button id="Button1" style="Z-INDEX: 102; LEFT: 376px; POSITION:
absolute; TOP: 272px" runat="server" Text="Excel export"></asp:Button>

<asp:Button id="Button2" style="Z-INDEX: 103; LEFT: 296px; POSITION:
absolute; TOP: 272px" runat="server" Text="Fill Table"></asp:Button>

</form>

</body>

</HTML>






___________________________________________________________________________
"codebehind"
___________________________________________________________________________




using System;

using System.Collections;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Web;

using System.Web.SessionState;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Web.UI.HtmlControls;

using System.IO;

using System.Data.SqlClient;



namespace Smyle

{

/// <summary>

/// Summary description for WebForm2.

/// </summary>

public class WebForm2 : System.Web.UI.Page

{

#region//variaveis

TableRow tableHeading = new TableRow();

//variaveis de acesso á base de dados

string sql;

public SqlConnection bdconn;

public SqlCommand bdcomm;

public SqlDataReader bdreader;

#endregion

protected System.Web.UI.WebControls.Button Button1;

protected System.Web.UI.WebControls.Button Button2;

protected System.Web.UI.WebControls.Table DisplayTable;



private void Page_Load(object sender, System.EventArgs e)

{

// Put user code to initialize the page here

bdconn = new SqlConnection("User
ID=SmyleUSER;Password=SmyleUSER;Persist Security Info=False;Initial
Catalog=Smyle;Data Source="+ Servidor.Smyle() +"");

bdconn.Open();

if(!Page.IsPostBack)

{

TableHeaderCell Descricao = new TableHeaderCell();

Descricao.Text = "Descrição de Usage";

Descricao.HorizontalAlign = HorizontalAlign.Left;

tableHeading.Cells.Add(Descricao);

DisplayTable.Rows.Add(tableHeading);

}

}



#region Web Form Designer generated code

override protected void OnInit(EventArgs e)

{

//

// CODEGEN: This call is required by the ASP.NET Web Form Designer.

//

InitializeComponent();

base.OnInit(e);

}



/// <summary>

/// Required method for Designer support - do not modify

/// the contents of this method with the code editor.

/// </summary>

private void InitializeComponent()

{

this.Button1.Click += new System.EventHandler(this.Button1_Click);

this.Button2.Click += new System.EventHandler(this.Button2_Click);

this.Load += new System.EventHandler(this.Page_Load);



}

#endregion



private void Button1_Click(object sender, System.EventArgs e)

{

//Response.Clear();

Response.Buffer=true;

Response.Charset = "";

Response.ContentType = "application/vnd.ms-excel";

System.IO.StringWriter stwWriter = new System.IO.StringWriter();

System.Web.UI.HtmlTextWriter htwHtmlTextWriter = new
System.Web.UI.HtmlTextWriter(stwWriter) ;

DisplayTable.RenderControl(htwHtmlTextWriter);

Response.Write(stwWriter.ToString());

Response.End();

}



private void Button2_Click(object sender, System.EventArgs e)

{

sql="Select descricao from usage";

bdcomm=new SqlCommand(sql,bdconn);

bdreader=bdcomm.ExecuteReader();

while(bdreader.Read())

{

TableRow TableRow = new TableRow();

TableCell UsagDescri = new TableCell();

UsagDescri.Text =bdreader["Descricao"].ToString();

TableRow.Cells.Add(UsagDescri);



DisplayTable.Rows.Add(TableRow);

}

bdreader.Close();

bdconn.Close();

}

}

}






</code>
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,836
Messages
2,569,750
Members
45,545
Latest member
rapter____0

Latest Threads

Top