Three Tier Architecture
- In WEB.config do the connection
<connectionStrings>
<add name="company"connectionString="Datasource=MDSWIN48;uid=sa;pwd=Admin2011;
Initial Catalog=Company;Integrated Security=SSPI" providerName="System.Data.SqlClient"/>
</connectionStrings>
2. Create the Common classlibrary
a. Rename the Class1.cs as CustomerInfo.cs
b. Create get/set method for the data
c. Then save and Build it.
Code:
namespace COMMON
{
public class CustomerInfo
{
private string username;
private string password;
private string id;
public string Username
{
get
{
return this.username;
}
set
{
try
{
this.username = value;
if (this.username == string.Empty)
{
throw new Exception("Enter the username");
}
}
catch (Exception e)
{
throw new Exception(e.Message.ToString());
}
}
}
public string Password
{
get
{
return this.password;
}
set
{
try
{
this.password = value;
if (this.password == string.Empty)
{
throw new Exception("Enter the password");
}
}
catch (Exception e)
{
throw new Exception(e.Message.ToString());
}
}
}
public string Id
{
get
{
return this.id;
}
set
{
try
{
this.id = value;
if (this.id == string.Empty)
{
throw new Exception("Enter the Id");
}
}
catch (Exception e)
{
throw new Exception(e.Message.ToString());
}
}
}
}
}
3. Create the Data Access Layer Class Library
· Rename the Class1.cs as DAL.cs
· Create reference for common
· Include using COMMON in code
· Create the connection to the database
· Then save and Build it.
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using COMMON;
namespace DAL
{
public class DACustomer
{
public DACustomer()
{ }
public void Add(CustomerInfo customer)
{
SqlConnection con = new SqlConnection();
con.ConnectionString = ConfigurationManager.ConnectionStrings["company"].ConnectionString;
con.Open();
SqlCommand command = new SqlCommand("Storelog", con);
command.CommandType = CommandType.StoredProcedure;
try
{
command.Parameters.AddWithValue("@UserName", customer.Username);
command.Parameters.AddWithValue("@Password", customer.Password);
command.Parameters.AddWithValue("@Id", customer.Id);
command.ExecuteNonQuery();
}
catch
{
throw;
}
finally
{
command.Dispose();
con.Close();
con.Dispose();
}
}
}
}
4 Create the Business Access Layer Class Library
· Rename the Class1.cs as BOCustomer.cs
· Create reference for common, DAL
· Include using COMMON and DAL in code
· Then save and Build it.
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using COMMON;
using DAL;
namespace BAL
{
public class BOCustomer
{
public void AddCust(CustomerInfo customer)
{
DACustomer DACust = new DACustomer();
DACust.Add(customer);
}
}
}
5. In Default.UI
· Create reference for common, BAL
· Include using COMMON and BAL in code
· Then save and Build it.
Code:
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;
using COMMON;
using BAL;
namespace WebApplication1
{
public partial class _Default : System.Web.UI.Page
{
BOCustomer BCUST = new BOCustomer();
CustomerInfo CUST = new CustomerInfo();
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnSubmit_Click(object sender, EventArgs e)
{
try
{
CUST.Username = txtUserName.Text.Trim();
CUST.Password = txtPassword.Text.Trim();
CUST.Id = txtId.Text.Trim();
BCUST.AddCust(CUST);
}
catch
{
throw;
}
}
}
}
Comments
Post a Comment