3-Tier Architecture in asp.net using C# with Crude Operations
3-Tier Architecture in asp.net using C# with Crude Operations
Introduction:-
Here I will explain how to impliment INSERT,UPADATE ,DELETE AND DISPLAY Using
3-Tier Architecture and EF(Entity Framework) Method.
STEP-1(I want to create Registration Form Display Below)
STEP-2(CREATE 3-TIER MODEL)
TAKE 3 PRJOJECTS..IN ONE SINGLE WEB APPLICATION.
CHOOSE CODING LANGUAGE AS C#.
STEP-3(Create ADO.NET DATA ENTITY MODEL)
MAKE DATABASE USING NAMED StudentRegistration.dbo
STEP-3(WORKING WITH Default.aspx.cs FILE)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using InsertUpdateDelete.BL;
using InsertUpdateDelete.DAL;
namespace InsertUpdateDelate
{
public partial class Default :
System.Web.UI.Page
{
public void
Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
btnUpdate.Visible = false;
btnsubmit.Visible = true;
if (Request.QueryString["id"] != null
&& !string.IsNullOrEmpty(Request.QueryString["id"].ToString()))
{
btnUpdate.Visible = true;
btnsubmit.Visible = false;
var objblStudentInfo = new StudentInfo();
var objRegistrationForm = new RegistrationForm();
objRegistrationForm =
objblStudentInfo.GetStudentRegistrationFormById(
Convert.ToInt32(Request.QueryString["id"].ToString()));
txtAdd.Text = objRegistrationForm.Address;
txtCity.Text = objRegistrationForm.City;
txtCountry.Text = objRegistrationForm.Country;
txtEmail.Text = objRegistrationForm.EmailId;
txtFname.Text = objRegistrationForm.FirstName;
txtLname.Text = objRegistrationForm.LastName;
txtMobile.Text = objRegistrationForm.MobileNo;
txtPinCode.Text = objRegistrationForm.PinCode;
txtState.Text = objRegistrationForm.State;
}
bindgrid();
}
}
public void
btnsubmit_Click(object sender, EventArgs e)
{
Session["form"] =
txtFname.Text;
var objinsert = new
RegistrationForm();
var objblStudentInfo = new StudentInfo();
objinsert.FirstName = txtFname.Text;
objinsert.LastName = txtLname.Text;
string birth = DropDownDay.SelectedValue
+ "-" +
DropDownMonth.SelectedValue + "-"
+
DropDownYear.SelectedValue;
objinsert.Dob = Convert.ToDateTime(birth);
objinsert.EmailId = txtEmail.Text;
objinsert.MobileNo = txtMobile.Text;
if (RadioMale.Checked == true)
{
objinsert.Gender = true;
}
else
{
objinsert.Gender = false;
}
objinsert.Address = txtAdd.Text;
objinsert.City = txtCity.Text;
objinsert.PinCode = txtPinCode.Text;
objinsert.State = txtState.Text;
objinsert.Country = txtCountry.Text;
string hob = "";
if (CheckBox1.Checked)
{
hob = CheckBox1.Text + ",";
}
if (CheckBox2.Checked)
{
hob = hob + CheckBox2.Text + ",";
}
if (CheckBox3.Checked)
{
hob = hob + CheckBox3.Text + ",";
}
if (CheckBox4.Checked)
{
hob = hob + CheckBox4.Text;
}
objinsert.Hobbies = Convert.ToString(hob);
objblStudentInfo.InsertStudent(objinsert);
ClearControl();
}
public void bindgrid()
{
var ent = new StudentRegistrationEntities();
List<RegistrationForm>
registrationForms = (from a in ent.RegistrationForms select
a).ToList();
}
public void
deleteclick(object sender, EventArgs e)
{
var btn =(Button)(sender);
var objStudentInfo = new
StudentInfo();
objStudentInfo.DeleteStudent(Convert.ToInt32(btn.CommandArgument));
bindgrid();
}
public void
ClearControl()
{
txtAdd.Text = string.Empty;
txtFname.Text = string.Empty;
txtLname.Text = string.Empty;
txtCity.Text = string.Empty;
txtCountry.Text = string.Empty;
txtEmail.Text = string.Empty;
txtMobile.Text = string.Empty;
txtPinCode.Text = string.Empty;
txtState.Text = string.Empty;
CheckBox1.Checked = false;
}
protected void
btnUpdate_Click(object sender, EventArgs e)
{
StudentInfo objblStudentInfo = new StudentInfo();
RegistrationForm objRegistrationForm = new RegistrationForm();
objRegistrationForm.StudentId = Convert.ToInt32(Request.QueryString["id"].ToString());
objRegistrationForm.FirstName = txtFname.Text;
objRegistrationForm.LastName = txtLname.Text;
if (RadioMale.Checked == true)
{
objRegistrationForm.Gender = true;
}
else
{
objRegistrationForm.Gender = false;
}
string birth = DropDownDay.SelectedValue + "-" + DropDownMonth.SelectedValue + "-" +
DropDownYear.SelectedValue;
objRegistrationForm.Dob = Convert.ToDateTime(birth);
objRegistrationForm.EmailId = txtEmail.Text;
objRegistrationForm.MobileNo = txtMobile.Text;
objRegistrationForm.Address = txtAdd.Text;
objRegistrationForm.City = txtCity.Text;
objRegistrationForm.PinCode = txtPinCode.Text;
objRegistrationForm.State = txtState.Text;
objRegistrationForm.Country = txtCountry.Text;
string hob = "";
if (CheckBox1.Checked)
{
hob = CheckBox1.Text + ",";
}
if (CheckBox2.Checked)
{
hob = hob + CheckBox2.Text + ",";
}
if (CheckBox3.Checked)
{
hob = hob + CheckBox3.Text + ",";
}
if (CheckBox4.Checked)
{
hob = hob + CheckBox4.Text;
}
objRegistrationForm.Hobbies = Convert.ToString(hob);
objblStudentInfo.UpdateEmployee(objRegistrationForm);
ClearControl();
}
}
}
working with :- DisplayStudent.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using InsertUpdateDelete.BL;
using InsertUpdateDelete.DAL;
namespace InsertUpdateDelate
{
public partial class DisplayStudent
: System.Web.UI.Page
{
protected void
Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
bindgrid();
if (Session["form"]
!= "" || Session["form"] != null)
{
tt1.Text = Session["form"].ToString();
}
}
}
public void
bindgrid()
{
var ent = new StudentRegistrationEntities();
List<RegistrationForm>
registrationForms = (from a in ent.RegistrationForms select
a).ToList();
GridView1.DataSource = registrationForms;
GridView1.DataBind();
}
public void
Editclick1 (object sender, EventArgs e)
{
}
public void
deleteclick1 (object sender, EventArgs e)
{
var btn = (Button)(sender);
var objStudentInfo = new
StudentInfo();
objStudentInfo.DeleteStudent(Convert.ToInt32(btn.CommandArgument));
bindgrid();
}
}
}
working with :- InsertUpdateDelete.BL
CREATE CLASS IN .BL PROJECT :- StudentInfo.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using InsertUpdateDelete.DAL;
namespace InsertUpdateDelete.BL
{
public class StudentInfo
{
//Insert Studentinfo
public void
InsertStudent(RegistrationForm
obRegistrationForm)
{
var ent = new StudentRegistrationEntities();
RegistrationForm objinsert = new RegistrationForm();
objinsert.StudentId =
obRegistrationForm.StudentId;
objinsert.FirstName = obRegistrationForm.FirstName;
objinsert.LastName = obRegistrationForm.FirstName;
objinsert.Dob = obRegistrationForm.Dob;
objinsert.EmailId = obRegistrationForm.EmailId;
objinsert.MobileNo = obRegistrationForm.MobileNo;
objinsert.Gender = obRegistrationForm.Gender;
objinsert.Address = obRegistrationForm.Address;
objinsert.City = obRegistrationForm.City;
objinsert.PinCode = obRegistrationForm.PinCode;
objinsert.State = obRegistrationForm.State;
objinsert.Country = obRegistrationForm.Country;
objinsert.Hobbies = obRegistrationForm.Hobbies;
objinsert.CreateDate = obRegistrationForm.CreateDate;
ent.RegistrationForms.AddObject(objinsert);
ent.SaveChanges();
}
public void
DeleteStudent(int id)
{
var ent =
new StudentRegistrationEntities();
RegistrationForm objRegistrationForm =
(from a in
ent.RegistrationForms where a.StudentId == id select a).FirstOrDefault();
if (objRegistrationForm != null)
{
ent.RegistrationForms.DeleteObject(objRegistrationForm);
ent.SaveChanges(0);
}
}
public RegistrationForm
GetStudentRegistrationFormById(int id)
{
var ent = new StudentRegistrationEntities();
RegistrationForm objRegistrationForm =
(from a in
ent.RegistrationForms where a.StudentId == id select a).FirstOrDefault();
return objRegistrationForm;
}
public void
UpdateEmployee(RegistrationForm
objRegistrationForm)
{
var ent = new StudentRegistrationEntities();
RegistrationForm objUpdatestudent =
(from a in
ent.RegistrationForms where a.StudentId ==
objRegistrationForm.StudentId select a)
.FirstOrDefault();
if (objUpdatestudent != null)
{
objUpdatestudent.StudentId = objRegistrationForm.StudentId;
objUpdatestudent.FirstName = objRegistrationForm.FirstName;
objUpdatestudent.LastName = objRegistrationForm.LastName;
objUpdatestudent.Address = objRegistrationForm.Address;
objUpdatestudent.City = objRegistrationForm.City;
objUpdatestudent.State = objRegistrationForm.State;
objUpdatestudent.MobileNo = objRegistrationForm.MobileNo;
objUpdatestudent.Gender = objUpdatestudent.Gender;
objUpdatestudent.Country = objRegistrationForm.Country;
objUpdatestudent.PinCode =
objRegistrationForm.PinCode;
objUpdatestudent.CreateDate = objRegistrationForm.CreateDate;
objUpdatestudent.Dob = objRegistrationForm.Dob;
objUpdatestudent.EmailId = objRegistrationForm.EmailId;
objUpdatestudent.Hobbies = objRegistrationForm.Hobbies;
ent.SaveChanges();
}
}
public List<RegistrationForm> btnSearch_Click (int id)
{
var ent = new StudentRegistrationEntities();
var objRegistrationForm =
(from a in
ent.RegistrationForms where a.StudentId == id select a).ToList();
return objRegistrationForm;
}
}
}
Download source
PLEASE COMMENT FOR ANY QUERY