raji vineeth

raji vineeth

  • 2k
  • 35
  • 189

3 tire Architecture in asp.net

Jul 31 2021 5:46 AM

This is my webform code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using BLLRHS;

namespace RHSWebApplication
{
    public partial class Home : System.Web.UI.Page
    {
        BLLClass obj = new BLLClass();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindGrid();
            }
        }
        public void BindGrid()
        {
            GridView1.DataSource = obj.SelectStudent();
            GridView1.DataBind();
        }
        public void Clear()
        {
            TextBox1.Text = ""; RadioButtonList1.ClearSelection(); TextBox2.Text = ""; DropDownList1.Text = ""; CheckBoxList1.Text = "";
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            obj.InsertData(TextBox1.Text,RadioButtonList1.SelectedItem.Text,TextBox2.Text,DropDownList1.SelectedItem.Text,CheckBoxList1.SelectedItem.Text);
            BindGrid();
            Clear();
        }

        protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
        {
            GridView1.EditIndex = e.NewEditIndex;
            GridView1.DataSource = obj.SelectStudent();
            GridView1.DataBind();
        }

        protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            int Id = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value);
            string Name = (GridView1.Rows[e.RowIndex].FindControl("TextBoxName") as TextBox).Text;
            string Gender = (GridView1.Rows[e.RowIndex].FindControl("RadioButtonListGender") as RadioButtonList).Text;
            string DOB = (GridView1.Rows[e.RowIndex].FindControl("TextBoxDOB") as TextBox).Text;
            string City = (GridView1.Rows[e.RowIndex].FindControl("DropDownListCity") as DropDownList).Text;
            string Qualification = (GridView1.Rows[e.RowIndex].FindControl("CheckBoxListQualification") as CheckBoxList).Text;
            obj.UpdateStudent(Id.ToString(), Name, Gender, DOB, City, Qualification);
            GridView1.EditIndex = -1;
            BindGrid();



        }

        protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            int Id = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value);
            obj.DeleteStudent(Id.ToString());
            BindGrid();
        }

        protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            GridView1.EditIndex = -1;
            BindGrid();
        }

        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            DataRowView dRowView = (DataRowView)e.Row.DataItem;
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                if ((e.Row.RowState & DataControlRowState.Edit) > 0)
                {
                    RadioButtonList rblGender = (RadioButtonList)e.Row.FindControl("RadioButtonListGender");
                    DropDownList ddlCity = (DropDownList)e.Row.FindControl("DropDownListCity");
                    CheckBoxList chkQua = (CheckBoxList)e.Row.FindControl("CheckBoxListQualification");
                    rblGender.SelectedValue = dRowView[2].ToString();
                    ddlCity.SelectedValue = dRowView[4].ToString();
                    chkQua.SelectedValue = dRowView[5].ToString();

                }
            }
        }
    }
}

This is my Data Access Layer

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using System.Data.SqlClient;


namespace DALRHS
{
    public class DALClass
    {
        SqlConnection cn = new SqlConnection("Data Source=.;Initial Catalog=RHS;Integrated Security=True");
       
        public void InsertDetails(string Name, string Gender, string DOB,string City, string Qualification)
        {
            SqlCommand cmd = new SqlCommand("Sp_SaveData1", cn);
            cmd.CommandType = System.Data.CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@Name", Name);
            cmd.Parameters.AddWithValue("@Gender", Gender);
            cmd.Parameters.AddWithValue("@DOB",DateTime.Parse (DOB));
            cmd.Parameters.AddWithValue("@City", City);
            cmd.Parameters.AddWithValue("@Qualification", Qualification);
            cn.Open();
            try
            {
                cmd.ExecuteNonQuery();
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
            finally
            {
                cn.Close();
            }
        }
        public void UpdateDetail(string Id,string Name, string Gender, string DOB, string City, string Qualification)
        {
            SqlCommand cmd = new SqlCommand("spUpdate", cn);
            cmd.CommandType = System.Data.CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@Id", Id);
            cmd.Parameters.AddWithValue("@Name", Name);
            cmd.Parameters.AddWithValue("@Gender", Gender);
            cmd.Parameters.AddWithValue("@DOB",DateTime.Parse(DOB));
            cmd.Parameters.AddWithValue("@City", City);
            cmd.Parameters.AddWithValue("@Qualification", Qualification);
            cn.Open();
            cmd.ExecuteNonQuery();
            cn.Close();
        }
        public object getDetail()
        {
            SqlDataAdapter da = new SqlDataAdapter("sp_GetAllStudent", cn);
            DataTable dt = new DataTable();
            da.Fill(dt);
            return dt;
        }
        public void deleteDetail(string Id)
        {
            SqlCommand cmd = new SqlCommand("Sp_Delete", cn);
            cmd.CommandType = System.Data.CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@Id", Id);
            cn.Open();
            cmd.ExecuteNonQuery();
            cn.Close();
        }
    }
}

Bussiness Layer is

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DALRHS;

namespace BLLRHS
{
    public class BLLClass
    {
        DALClass obj = new DALClass();

        public void InsertData(string Name, string Gender, string DOB, string City, string Qualification)
        {
            obj.InsertDetails(Name, Gender, DOB, City, Qualification);
        }
        public void UpdateStudent(string Id,string Name, string Gender, string DOB, string City, string Qualification)
        {
            obj.UpdateDetail(Id,Name, Gender, DOB, City, Qualification);
        }
        public object SelectStudent()
        {
           return  obj.getDetail();
        }
        public void DeleteStudent(string Id)
        {
            obj.deleteDetail(Id);
        }
    }
}

Please check my code standrad. Any changes needed?


Attachment: New_folder_(2).rar

Answers (3)