
January 20th, 2004, 03:42 AM
|
|
Junior Member
|
|
Join Date: Jan 2004
Posts: 1
Time spent in forums: < 1 sec
Reputation Power: 0
|
|
Little Bug in ASP.NET, cannot export to excel well...Please Help, urgent!
Hi all,
I've been posting a lot of forums, about this problem, and until now, i didn't get any solution.
The problem is this, i've got to get the values of a table, created dynamicly, by pressing a button, from a sql query.
Here's the code, that will demosntrate my problem for you can test it  C#, ASP.NET) 
Code:
ASPX File
__________________________________________________ ________________________________
<%@ 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="512px" Height="32px"></asp:Table>
<asp:Button id="Button1" style="Z-INDEX: 102; LEFT: 376px; POSITION: absolute; TOP: 216px" runat="server" Text="Excel export"></asp:Button>
<asp:Button id="Button2" style="Z-INDEX: 103; LEFT: 296px; POSITION: absolute; TOP: 216px" runat="server" Text="Fill Table"></asp:Button>
</form>
</body>
</HTML>
__________________________________________________ ________________________________
CodeBehind File
__________________________________________________ ________________________________
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
Response.Buffer=true;
Response.Clear();
Response.Charset = "";
bdconn = new SqlConnection("User ID=****;Password=****;Persist Security Info=False;Initial Catalog=Smyle;Data Source="+ Servidor.***() +"");
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);
TableRow TableRow = new TableRow();
TableCell UsagDescri = new TableCell();
UsagDescri.Text ="teste";
TableRow.Cells.Add(UsagDescri);
DisplayTable.Rows.Add(TableRow);
}
#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();
}
}
}
I know that this could be boring, but it's a litle bug in ASP.NET, that could appear to any one. Thanks, i will wait for you help, in front of the screen.
Thanks
|