Saturday, 5 November 2016

Creating Gridview in MVC using separate Buisness Layer

This is What we want to Achieve

Tables Used

select * from EmployeeTest


Step 1 : Create new MVC Project


Step 2 : Change the Name, Location of Project


Step 3 : Choose Empty MVC Application


Step 4 : Create class library BuisnessLayer


Step 5 : Add EntityFramework and System.Configuration reference in BuisnessLayer Class Library

Step 6 : Add Employee class and EmployeeBuisnessLayer class in BuisnessLayer Class Library

Step 7 : Add BuisnessLayer class library reference in MVCExample Project

Step 8 : This is how our solution explorer will look like

Step 9 :  In Employee class file present in BuinessLayer

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.ComponentModel.DataAnnotations;

namespace BuisnessLayer
{
    public interface IEmployee
    {
        int EmpID { get; set; }
        string EmpDesignation { get; set; }
    }
    public class Employee : IEmployee
    {
        public int EmpID { get; set; }
        [Required]
        public string EmpName { get; set; }
        [Required]
        public string EmpDesignation { get; set; }

    }
}

Step 10 :  In Web.config file add the connection string

  <connectionStrings>
    <add name="EmplyoeeDB" connectionString="data Source=.;database=employee; integrated security=SSPI" providerName="Sql.Data.SqlClient" />
  </connectionStrings>

 Step 11 : In EmployeeBuisnessLayer class present in BuisnessLayer

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

namespace BuisnessLayer
{
   public class EmployeeBuisnessLayer
    {
        public IEnumerable<Employee> Employees
        {
            get
            {
                List<Employee> employeeList = new List<Employee>();
                String CS = ConfigurationManager.ConnectionStrings["EmplyoeeDB"].ConnectionString;
                using (SqlConnection sqlcon = new SqlConnection(CS))
                {
                    SqlCommand sqlcmd = new SqlCommand("Select * from EmployeeTest", sqlcon);
                    sqlcon.Open();
                    SqlDataReader sqlreader = sqlcmd.ExecuteReader();
                    while (sqlreader.Read())
                    {
                        Employee employee = new Employee();
                        employee.EmpID = Convert.ToInt32(sqlreader["EmpID"]);
                        employee.EmpName = sqlreader["EmpName"].ToString();
                        employee.EmpDesignation = sqlreader["EmpDesignation"].ToString();
                        employeeList.Add(employee);
                    }
                    sqlcon.Close();
                    return employeeList;
                }
            }
        }
}
}

Step 12 : Add Employee Controller
In EmployeeController.cs

using BuisnessLayer;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace MVCExample.Controllers
{
    public class EmployeeController : Controller
    {
        public ActionResult Index()
        {
            EmployeeBuisnessLayer employeeBuisnessLayer = new EmployeeBuisnessLayer();
            List<Employee> employees = employeeBuisnessLayer.Employees.ToList();
            return View(employees);
        }
}
}

Step 13 : Right click on Index function and Select Add View


Step 14 : In Index.cshtml file

@model IEnumerable<BuisnessLayer.Employee>

@{
    ViewBag.Title = "Index";
}

<h2>Index</h2>

<p>
    @Html.ActionLink("Create New", "Create")
</p>
<table class="table" border="1">
    <tr>
        <th>
            @Html.DisplayNameFor(model => model.EmpID)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.EmpName)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.EmpDesignation)
        </th>
        <th>
            Action
        </th>
    </tr>

    @foreach (var item in Model)
    {
        <tr>
            <td>
                @Html.DisplayFor(modelItem => item.EmpID)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.EmpName)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.EmpDesignation)
            </td>
            <td>
                @Html.ActionLink("Edit", "Edit", new { /* id=item.PrimaryKey */ }) |
                @Html.ActionLink("Details", "Details", new { /* id=item.PrimaryKey */ }) |
                <input type="submit" value="Delete" onclick="return confirm('Are you sure you wat to delete employee: @item.EmpName')" />
            </td>
        </tr>
    }

</table>


Step 15 : Run the Application 


0 comments:

Post a Comment