Friday, April 25, 2008

Export Gridview to Word file

create new website and add Default page and in it add Gridview and button control
We will use Northwind database


using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

public partial class ExportToWord : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
GridView1.DataSource = BindData();
GridView1.DataBind();
}
}

private string ConnectionString
{

get { return @"Data Source=YASER\SQLEXPRESS;Initial Catalog=Northwind;Integrated Security=True"; }

}



private DataSet BindData()
{
// make the query
string query = "SELECT * FROM Categories";
SqlConnection myConnection = new SqlConnection(ConnectionString);
SqlDataAdapter ad = new SqlDataAdapter(query, myConnection);
DataSet ds = new DataSet();
ad.Fill(ds, "Categories");
return ds;

}

protected void Button1_Click(object sender, EventArgs e)
{

Response.Clear();
Response.AddHeader("content-disposition", "attachment;filename=FileName.doc");
Response.Charset = "";
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.ContentType = "application/vnd.word";

System.IO.StringWriter stringWrite = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);

GridView1.RenderControl(htmlWrite);
Response.Write(stringWrite.ToString());
Response.End();

}

public override void VerifyRenderingInServerForm(Control control)
{

}


}

Export Gridview to Exel file

create new website and add Default page and in it add Gridview and button

In Default.aspx.cs

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
GridView1.DataSource = BindData();
GridView1.DataBind();
}
}

private string ConnectionString
{

get { return @"Data Source=YASER\SQLEXPRESS;Initial Catalog=Northwind;Integrated Security=True"; }

}

private DataSet BindData()
{
// make the query
string query = "SELECT * FROM Products";
SqlConnection myConnection = new SqlConnection(ConnectionString);
SqlDataAdapter ad = new SqlDataAdapter(query, myConnection);
DataSet ds = new DataSet();
ad.Fill(ds, "Products");
return ds;

}

protected void Button1_Click(object sender, EventArgs e)
{
Response.Clear();

Response.AddHeader("content-disposition", "attachment;filename=FileName.xls");

Response.Charset = "";

// If you want the option to open the Excel file without saving than

// comment out the line below

// Response.Cache.SetCacheability(HttpCacheability.NoCache);

Response.ContentType = "application/vnd.xls";

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

System.Web.UI.HtmlTextWriter htmlWrite =
new HtmlTextWriter(stringWrite);

GridView1.RenderControl(htmlWrite);

Response.Write(stringWrite.ToString());

Response.End();

}

public override void VerifyRenderingInServerForm(Control control)
{

// Confirms that an HtmlForm control is rendered for the
//specified ASP.NET server control at run time.

}
}

Get List and Number of Online users

First create website and add Registe , Login and WhoIsOnline page in website

In OnlineUser page add lable (lblSessionCount) and Gridview (OnlineUserList) control which is bound to
(UserName (BoundField), IsOnline (CheckBoxField) , Email (BoundField) )


Im WhoIsOnLine.aspx.cs page add

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class WhoIsOnLine : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//if (!IsPostBack)
//{
MembershipUserCollection OnlineUsers = new MembershipUserCollection();
MembershipUserCollection AllUsers = new MembershipUserCollection();
AllUsers = Membership.GetAllUsers();

foreach (MembershipUser user in AllUsers)
{
if (user.IsOnline)
{
OnlineUsers.Add(user);

}
}
int OnlineUserCount = OnlineUsers.Count;

lblSessionCount.Text = OnlineUserCount.ToString();
OnlineUserList.DataSource = OnlineUsers;
OnlineUserList.DataBind();
//}
}
}