Three tier architecture example in asp.net with C#
Basically 3-Tier architecture contains 3 layers
1. Application Layer or Presentation
Layer
2. Business Access Layer(BAL) or Business
Logic Layer(BLL)
3. Data Access Layer(DAL)
BEL.CS
private string _UserName;
private string _Password;
private string _FirstName;
private string _LastName;
private string _Email;
private string _Phoneno;
private string _Location;
private string _Created_By;
public string UserName
{
get
{
return _UserName;
}
set
{
_UserName = value;
}
}
public string Password
{
get
{
return _Password;
}
set
{
_Password = value;
}
}
public string FirstName
{
get
{
return _FirstName;
}
set
{
_FirstName = value;
}
}
public string LastName
{
get
{
return _LastName;
}
set
{
_LastName = value;
}
}
public string Email
{
get
{
return _Email;
}
set
{
_Email = value;
}
}
public string Phoneno
{
get
{
return _Phoneno;
}
set
{
_Phoneno = value;
}
}
public string Location
{
get
{
return _Location;
}
set
{
_Location = value;
}
}
public string Created_By
{
get
{
return _Created_By;
}
set
{
_Created_By = value;
}
BLL.CS(Business Logic layer)
public string InsertUserDetails(BEL objUserDetails)
{
DAL objUserDAL
= new DAL();
try
{
return objUserDAL.InsertUserInformation(objUserDetails);
}
catch (Exception ex)
{
throw ex;
}
finally
{
objUserDAL = null;
}
}
Data Access Layer(DAL) DAL.CS
string ConnectionString
= ConfigurationManager.AppSettings["LocalConnection"].ToString();
public string InsertUserInformation(BEL objBELUserDetails)
{
SqlConnection con
= new SqlConnection(ConnectionString);
con.Open();
SqlCommand cmd
= new SqlCommand("sp_userinformation", con);
cmd.CommandType = CommandType.StoredProcedure;
try
{
cmd.Parameters.AddWithValue("@UserName",objBELUserDetails.UserName);
cmd.Parameters.AddWithValue("@Password",
objBELUserDetails.Password);
cmd.Parameters.AddWithValue("@FirstName",
objBELUserDetails.FirstName);
cmd.Parameters.AddWithValue("@LastName",
objBELUserDetails.LastName);
cmd.Parameters.AddWithValue("@Email",
objBELUserDetails.Email);
cmd.Parameters.AddWithValue("@PhoneNo",
objBELUserDetails.Phoneno);
cmd.Parameters.AddWithValue("@Location",
objBELUserDetails.Location);
cmd.Parameters.AddWithValue("@Created_By",
objBELUserDetails.Created_By);
cmd.Parameters.Add("@ERROR", SqlDbType.Char, 500);
cmd.Parameters["@ERROR"].Direction
= ParameterDirection.Output;
cmd.ExecuteNonQuery();
string strMessage
= (string) cmd.Parameters["@ERROR"].Value;
con.Close();
return strMessage;
}
catch (Exception ex)
{
throw ex;
}
finally
{
cmd.Dispose();
con.Close();
con.Dispose();
}
}
protected void btnsubmit_Click(object sender, EventArgs e)
{
string Output
= string.Empty;
if (txtpwd.Text
== txtcnmpwd.Text)
{
BEL objUserBEL
= new BEL();
objUserBEL.UserName = txtuser.Text;
objUserBEL.Password = txtpwd.Text;
objUserBEL.FirstName = txtfname.Text;
objUserBEL.LastName = txtlname.Text;
objUserBEL.Email = txtEmail.Text;
objUserBEL.Phoneno = txtphone.Text;
objUserBEL.Location = txtlocation.Text;
objUserBEL.Created_By = txtuser.Text;
BLL objUserBLL
= new BLL();
Output = objUserBLL.InsertUserDetails(objUserBEL);
}
else
{
Page.RegisterStartupScript("UserMsg", "<Script language='javascript'>alert('" + "Password
mismatch" +"');</script>");
}
lblErrorMsg.Text = Output;
}