Wednesday 18 March 2015

HOME

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)

 
Default.aspx


<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <table align="center" cellpadding="10">
            <!----- First Name ----------------------------------------->
            <tr>
                <td>
                    FIRST NAME
                </td>
                <td>
                    <asp:TextBox ID="txtFname" runat="server"></asp:TextBox>
                    (max 30 characters a-z and A-Z)
                </td>
            </tr>
            <!----- Last Name ------------------------------------------->
            <tr>
                <td>
                    LAST NAME
                </td>
                <td>
                    <asp:TextBox ID="txtLname" runat="server"></asp:TextBox>
                    (max 30 characters a-z and A-Z)
                </td>
            </tr>
            <!----- Date Of Birth ---------------------------------------->
            <tr>
                <td>
                    DATE OF BIRTH
                </td>
                <td>
                    <asp:DropDownList ID="DropDownDay" runat="server" Height="16px">
                        <asp:ListItem>1</asp:ListItem>
                        <asp:ListItem>2</asp:ListItem>
                        <asp:ListItem>3</asp:ListItem>
                        <asp:ListItem>4</asp:ListItem>
                        <asp:ListItem>5</asp:ListItem>
                        <asp:ListItem>6</asp:ListItem>
                        <asp:ListItem>7</asp:ListItem>
                        <asp:ListItem>8</asp:ListItem>
                        <asp:ListItem>9</asp:ListItem>
                        <asp:ListItem>10</asp:ListItem>
                        <asp:ListItem>11</asp:ListItem>
                        <asp:ListItem>12</asp:ListItem>
                        <asp:ListItem>13</asp:ListItem>
                        <asp:ListItem>14</asp:ListItem>
                        <asp:ListItem>15</asp:ListItem>
                        <asp:ListItem>16</asp:ListItem>
                        <asp:ListItem>17</asp:ListItem>
                        <asp:ListItem>18</asp:ListItem>
                        <asp:ListItem>19</asp:ListItem>
                        <asp:ListItem>20</asp:ListItem>
                        <asp:ListItem>21</asp:ListItem>
                        <asp:ListItem>22</asp:ListItem>
                        <asp:ListItem>23</asp:ListItem>
                        <asp:ListItem>24</asp:ListItem>
                        <asp:ListItem>25</asp:ListItem>
                        <asp:ListItem>26</asp:ListItem>
                        <asp:ListItem>27</asp:ListItem>
                        <asp:ListItem>28</asp:ListItem>
                        <asp:ListItem>29</asp:ListItem>
                        <asp:ListItem>30</asp:ListItem>
                    </asp:DropDownList>
                    <asp:DropDownList ID="DropDownMonth" runat="server">
                        <asp:ListItem>jan</asp:ListItem>
                        <asp:ListItem>feb</asp:ListItem>
                        <asp:ListItem>march</asp:ListItem>
                        <asp:ListItem>april</asp:ListItem>
                        <asp:ListItem>may</asp:ListItem>
                        <asp:ListItem>june</asp:ListItem>
                        <asp:ListItem>july</asp:ListItem>
                        <asp:ListItem>august</asp:ListItem>
                        <asp:ListItem>sept</asp:ListItem>
                        <asp:ListItem>oct</asp:ListItem>
                        <asp:ListItem>nov</asp:ListItem>
                        <asp:ListItem>dec</asp:ListItem>
                    </asp:DropDownList>
                    <asp:DropDownList ID="DropDownYear" runat="server">
                        <asp:ListItem>1990</asp:ListItem>
                        <asp:ListItem>1991</asp:ListItem>
                        <asp:ListItem>1992</asp:ListItem>
                        <asp:ListItem>1993</asp:ListItem>
                        <asp:ListItem>1994</asp:ListItem>
                        <asp:ListItem>1995</asp:ListItem>
                        <asp:ListItem>1996</asp:ListItem>
                        <asp:ListItem>1997</asp:ListItem>
                        <asp:ListItem>1998</asp:ListItem>
                        <asp:ListItem>1999</asp:ListItem>
                        <asp:ListItem>2000</asp:ListItem>
                        <asp:ListItem>2001</asp:ListItem>
                        <asp:ListItem>2002</asp:ListItem>
                        <asp:ListItem>2003</asp:ListItem>
                        <asp:ListItem>2004</asp:ListItem>
                        <asp:ListItem>2005</asp:ListItem>
                    </asp:DropDownList>
                </td>
            </tr>
            <!----- Email Id ------------------------------------------->
            <tr>
                <td>
                    EMAIL ID
                </td>
                <td>
                    <asp:TextBox ID="txtEmail" runat="server"></asp:TextBox>
                </td>
            </tr>
            <!----- Mobile Number -------------------------------------->
            <tr>
                <td>
                    MOBILE NUMBER
                </td>
                <td>
                    <asp:TextBox ID="txtMobile" runat="server"></asp:TextBox>
                </td>
            </tr>
            <!----- Gender --------------------------------------------->
            <tr>
                <td>
                    GENDER
                </td>
                <td>
                    <asp:RadioButton ID="RadioMale" runat="server" Text="Male"
                        GroupName="groupname" />
                    <asp:RadioButton ID="RadioFemale" runat="server" Text="Female"
                        GroupName="groupname" />
                </td>
            </tr>
            <!----- Address ------------------------------------------->
            <tr>
                <td>
                    ADDRESS
                    <br />
                    <br />
                    <br />
                </td>
                <td>
                    <asp:TextBox ID="txtAdd" runat="server" Rows="4" TextMode="MultiLine"></asp:TextBox>
                </td>
            </tr>
            <!----- City -------------------------------------------->
            <tr>
                <td>
                    CITY
                </td>
                <td>
                    <asp:TextBox ID="txtCity" runat="server"></asp:TextBox>
                    (max 30 characters a-z and A-Z)
                </td>
            </tr>
            <!----- State ------------------------------------------>
            <tr>
                <td>
                    STATE
                </td>
                <td>
                    <asp:TextBox ID="txtState" runat="server"></asp:TextBox>
                    (max 30 characters a-z and A-Z)
                </td>
            </tr>
            <!----- Country ---------------------------------------->
            <tr>
                <td>
                    COUNTRY
                </td>
                <td>
                    <asp:TextBox ID="txtCountry" runat="server"></asp:TextBox>
                </td>
            </tr>
            <!----- Pin Code ------------------------------------->
            <tr>
                <td>
                    PIN CODE
                </td>
                <td>
                    <asp:TextBox ID="txtPinCode" runat="server"></asp:TextBox>
                    (6 digit number)
                </td>
            </tr>
            <!----- Hobbies ----------------------------------------->
            <tr>
                <td>
                    HOBBIES
                    <br />
                </td>
                <td>
                    <asp:CheckBox ID="CheckBox1" runat="server" Text="Drawing" />
                    <asp:CheckBox ID="CheckBox2" runat="server" Text="Singing" />
                    <asp:CheckBox ID="CheckBox3" runat="server" Text="Dance" />
                    <asp:CheckBox ID="CheckBox4" runat="server" Text="Sketch" />
                </td>
            </tr>
            <tr>
                <td colspan="2" align="center">
                    &nbsp;
                    <asp:Button ID="btnsubmit" runat="server" Text="SUBMIT"
                        onclick="btnsubmit_Click" />
                    <asp:Button ID="Button2" runat="server" Text="CANCEL"/>
                    <asp:Button ID="btnUpdate" runat="server" Text="UPDATE" OnClick="btnUpdate_Click"/>
                </td>
            </tr>
            <tr>
                <td colspan="2" align="center">
                    <asp:HyperLink ID="HyperLink1" runat="server"
                        NavigateUrl="~/DisplayStudent.aspx" Target="_blank">DisplayStudent</asp:HyperLink>
                </td>
            </tr>
        </table>
    </div>
    <p>
       
    </form>
    <p>
        &nbsp;</p>
</body>

</html>


DisplayStudent.aspx


<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" BackColor="#CCCCCC"
            BorderColor="#999999" BorderStyle="Solid" BorderWidth="3px" CellPadding="4" CellSpacing="2"
            ForeColor="Black">
            <columns>
               <asp:TemplateField HeaderText="FirstName">
                    <ItemTemplate>
                        <%#Eval("FirstName") %>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="LastName">
                    <ItemTemplate>
                        <%#Eval("LastName") %>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="BirthDate">
                    <ItemTemplate>
                        <%#Eval("Dob") %>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Email">
                    <ItemTemplate>
                        <%#Eval("EmailId") %>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Gender">
                    <ItemTemplate>
                        <%#Eval("Gender") %>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="ADDRESS">
                    <ItemTemplate>
                        <%#Eval("Address") %>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="City">
                    <ItemTemplate>
                        <%#Eval("City") %>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="PinCode">
                    <ItemTemplate>
                        <%#Eval("PinCode") %>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="CreateDate">
                    <ItemTemplate>
                        <%#Eval("CreateDate") %>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="EDIT DELETE">
                    <ItemTemplate>
                        <a href="Default.aspx?id=<%#Eval("StudentId") %>">Edit</a>
                                          <asp:Button ID="Button1" runat="server" CommandArgument='<%#Eval("StudentId")%>' Text="Delete" OnClick="deleteclick1" />
         
                    </ItemTemplate>
                </asp:TemplateField>
           </columns>
            <footerstyle backcolor="#CCCCCC" />
            <headerstyle backcolor="Black" font-bold="True" forecolor="White" />
            <pagerstyle backcolor="#CCCCCC" forecolor="Black" horizontalalign="Left" />
            <rowstyle backcolor="White" />
            <selectedrowstyle backcolor="#000099" font-bold="True" forecolor="White" />
            <sortedascendingcellstyle backcolor="#F1F1F1" />
            <sortedascendingheaderstyle backcolor="#808080" />
            <sorteddescendingcellstyle backcolor="#CAC9C9" />
            <sorteddescendingheaderstyle backcolor="#383838" />
        </asp:GridView>
        <asp:Label runat="server" id="tt1" Text="Label"></asp:Label>
    </div>
    </form>
</body>
</html>

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 
THANK YOU...:)
PLEASE COMMENT FOR ANY QUERY