I want import data from sqlserver to excel ;
When I click on the button I have no results,where is the problem
file: default.aspx.cs
using System;
using System.Drawing;
using System.Data;
using System.Data.SqlClient;
public partial class Default4 : System.Web.UI.Page
{
private System.Data.DataTable GetData()
{
SqlConnection conn = new SqlConnection(@"server=(.\SQLEXPRESS);uid=sa;pwd=xxxx;database=examen.mdf;");
SqlDataAdapter adapter = new SqlDataAdapter("select * from ETUDIANT", conn);
DataSet myDataSet = new DataSet();
try
{
adapter.Fill(myDataSet, "ETUDIANT");
}
catch (Exception ex)
{
}
return myDataSet.Tables[1];
}
private void button1_Click(object sender, System.EventArgs e)
{
Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.Application();
int rowIndex = 1;
int colIndex = 0;
excel.Application.Workbooks.Add(true);
DataTable table = GetData();
foreach (DataColumn col in table.Columns)
{
colIndex++;
excel.Cells[1, colIndex] = col.ColumnName;
}
foreach (DataRow row in table.Rows)
{
rowIndex++;
colIndex = 0;
foreach (DataColumn col in table.Columns)
{
colIndex++;
excel.Cells[rowIndex, colIndex] = row[col.ColumnName].ToString();
}
}
excel.Visible = true;
}
}
kannan sPosted Feb 18, 2008, 10:06 AM
Hi,
Can you explain me if the page gets postback (refresh) or not?. If the page does not get postback then check the corresponding page tag
<%
@ Page Language="c#" AutoEventWireup="false" CodeFile="Default4.aspx.cs" Inherits="Default4" %>replace it with following
<%@ Page Language="c#" AutoEventWireup="true" CodeFile="Default4.aspx.cs" Inherits="Default4" %>
Can you send post both html and server side coding also it is also useful to findout the problem for me.
Regards,
Kannan.S