Three tier architecture means three layers for the application..
first layer is the
presentation layer with which only user would interact..
second is the
business logic layer where you would apply logics for the application...
third one is the
Data access layer which will only deal with the database...
moreover, you should use stored procedure to make your application more flexible...
here is the demo code for the three tier architecture...
<big>Presentation layer</big> is the server page where you do html coding and use asp.net controls...
<big>Business Logic Layer</big>
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
public partial class signin1 : System.Web.UI.Page
{
#region objects
signin.methodssignin mainobj = new signin.methodssignin();
signin.prpsignin prpobj = new signin.prpsignin();
#endregion
#region userdefinedmethods
public void checkuserstatus(DataTable dtbl)
{
if (dtbl.Rows[0]["user_status"].ToString() == "AC")
{
checkusertype(dtbl);
}
else
{
lblmsg.Text = "Your login has expired";
Clear();
}
}
public void checkusertype(DataTable dtbl)
{
Session["user_id"] = Convert.ToInt32(dtbl.Rows[0]["user_id"]);
Session["user_name"] = dtbl.Rows[0]["first_name"].ToString() + " " + dtbl.Rows[0]["last_name"].ToString();
Session["last_name"] = dtbl.Rows[0]["last_name"].ToString();
Session["user_type"] = dtbl.Rows[0]["user_type"].ToString();
prpobj.userid = Convert.ToInt32(dtbl.Rows[0]["user_id"]);
mainobj.setuserstatusonline(prpobj);
if (dtbl.Rows[0]["user_type"].ToString() == "Admin")
{
Response.Redirect("~/Admin/Home.aspx");
}
else
{
Response.Redirect("~/user/Home.aspx");
}
}
public void Clear()
{
txtusername.Text = string.Empty;
txtpwd.Text = string.Empty;
}
#endregion
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
try
{
Session.Clear();
if (Request.QueryString["sts"] == "login")
{
lblmsg.Text = "You should login to view that page";
}
if (Request.QueryString["sts"] == "logout")
{
lblmsg.Text = "You do not have permission to view this page";
}
if (Request.QueryString["sts"] == "logoutbyuser")
{
lblmsg.Text = "You have successfully logout";
}
if (Request.QueryString["sts"] == "problem")
{
lblmsg.Text = "You do not have permission to view this page";
}
}
catch (Exception ex)
{
Session.Clear();
}
}
}
protected void btnsignin_Click(object sender, EventArgs e)
{
prpobj.username = txtusername.Text.Trim();
prpobj.userpwd = txtpwd.Text.Trim();
if (txtusername.Text.Trim() != string.Empty && txtpwd.Text.Trim() != string.Empty)
{
checkuserpwd(prpobj);
}
else
{
lblmsg.Text = "Invalid username/password";
}
}
public void checkuserpwd(signin.prpsignin prpobj)
{
DataTable dtbl = mainobj.getuserdetail(prpobj);
if (dtbl.Rows.Count > 0)
{
if (dtbl.Rows[0]["user_pwd"].ToString() == txtpwd.Text.Trim())
{
checkuserstatus(dtbl);
}
else
{
lblmsg.Text = "Invalid username/password";
Clear();
}
}
else
{
lblmsg.Text = "Invalid username/password";
Clear();
}
}
protected void btncancel_Click(object sender, EventArgs e)
{
Clear();
lblmsg.Text = string.Empty;
}
}
<big>Data Access Layer</big>
using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
public class signin
{
#region interface
interface intersignin
{
int userid
{
get;
set;
}
string username
{
get;
set;
}
string userpwd
{
get;
set;
}
string userline
{
get;
set;
}
}
#endregion
#region properties
public class prpsignin : intersignin
{
int intuserid;
string strusername, struserpwd,struserline;
public int userid
{
get
{
return intuserid;
}
set
{
intuserid = value;
}
}
public string username
{
get
{
return strusername;
}
set
{
strusername = value;
}
}
public string userpwd
{
get
{
return struserpwd;
}
set
{
struserpwd = value;
}
}
public string userline
{
get
{
return struserline;
}
set
{
struserline = value;
}
}
}
#endregion
#region databasemethods
public class methodssignin : registeration.connection
{
public DataTable getuserdetail(prpsignin prpobj)
{
SqlCommand cmd = new SqlCommand();
cmd.Connection = con;
cmd.CommandText = "sp_getuserdetail";
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@user_name", SqlDbType.VarChar).Value = prpobj.username;
if (con.State == ConnectionState.Closed)
{
con.Open();
}
SqlDataAdapter dad = new SqlDataAdapter(cmd);
DataTable dtbl = new DataTable();
dad.Fill(dtbl);
return dtbl;
}
public void setuserstatusonline(prpsignin prpobj)
{
SqlCommand cmd = new SqlCommand();
cmd.Connection = con;
cmd.CommandText = "sp_setuserstatusonline";
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@user_id", SqlDbType.Int).Value = prpobj.userid;
if (con.State == ConnectionState.Closed)
{
con.Open();
}
cmd.ExecuteNonQuery();
}
public void setuserststatusoffline(prpsignin prpobj)
{
SqlCommand cmd = new SqlCommand();
cmd.Connection = con;
cmd.CommandText = "sp_setuserstatusoffline";
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@user_id", SqlDbType.Int).Value = prpobj.userid;
if (con.State == ConnectionState.Closed)
{
con.Open();
}
cmd.ExecuteNonQuery();
}
}
#endregion
}
hope this would help you..
regard richards.... :)