CRUD App In ASP.NET MVC Using ADO.NET | Without Entity Framework | Read Data

 using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;


namespace CRUDAppUsingADO.Models

{

    public class Employee

    {

        public int id { get; set; }

        public string name { get; set; }

        public string gender { get; set; }

        public int age { get; set; }

        public int salary { get; set; }

        public string city { get; set; }

    }

}

Above File is CRUDAppUsingADO\Models\Employee.cs






using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Data.SqlClient;

using System.Data;

using System.Configuration;

using CRUDAppUsingADO.Models;


namespace CRUDAppUsingADO

{

    public class EmployeeDBContext

    {

        string cs = ConfigurationManager.ConnectionStrings["dbcs"].ConnectionString;


        public List<Employee> GetEmployees()

        {

            List<Employee> EmployeeList = new List<Employee>();

            SqlConnection con = new SqlConnection(cs);

            SqlCommand cmd = new SqlCommand("spGetAllEmployees", con);

            cmd.CommandType = CommandType.StoredProcedure;

            con.Open();

            SqlDataReader dr = cmd.ExecuteReader();

            while(dr.Read())

            {

                Employee emp = new Employee();

                emp.id = Convert.ToInt32(dr.GetValue(0).ToString());

                emp.name = dr.GetValue(1).ToString();

                emp.gender = dr.GetValue(2).ToString();

                emp.age = Convert.ToInt32(dr.GetValue(3).ToString());

                emp.salary = Convert.ToInt32(dr.GetValue(4).ToString());

                emp.city = dr.GetValue(5).ToString();

                EmployeeList.Add(emp);

            }

            con.Close();



            return EmployeeList;

        }

    }

}

Above File is CRUDAppUsingADO\EmployeeDBContext.cs







using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.Mvc;

using CRUDAppUsingADO.Models;


namespace CRUDAppUsingADO.Controllers

{

    public class HomeController : Controller

    {

        // GET: Home

        public ActionResult Index()

        {

            EmployeeDBContext db = new EmployeeDBContext();

            List<Employee> obj = db.GetEmployees();


            return View(obj);

        }

    }

}

Above File is CRUDAppUsingADO\Controllers\HomeController.cs








@model IEnumerable<CRUDAppUsingADO.Models.Employee>


@{

    ViewBag.Title = "Index";

}


<h2>Index</h2>


<p>

    @Html.ActionLink("Create New", "Create")

</p>

<table class="table table-bordered table-hover table-striped">

    <tr>

        <th>

            @Html.DisplayNameFor(model => model.id)

        </th>

        <th>

            @Html.DisplayNameFor(model => model.name)

        </th>

        <th>

            @Html.DisplayNameFor(model => model.gender)

        </th>

        <th>

            @Html.DisplayNameFor(model => model.age)

        </th>

        <th>

            @Html.DisplayNameFor(model => model.salary)

        </th>

        <th>

            @Html.DisplayNameFor(model => model.city)

        </th>

        <th>Operations</th>

    </tr>


@foreach (var item in Model) {

    <tr>

        <td>

            @Html.DisplayFor(modelItem => item.id)

        </td>

        <td>

            @Html.DisplayFor(modelItem => item.name)

        </td>

        <td>

            @Html.DisplayFor(modelItem => item.gender)

        </td>

        <td>

            @Html.DisplayFor(modelItem => item.age)

        </td>

        <td>

            @Html.DisplayFor(modelItem => item.salary)

        </td>

        <td>

            @Html.DisplayFor(modelItem => item.city)

        </td>

        <td>

            @Html.ActionLink("Edit", "Edit", new { id=item.id }) |

            @Html.ActionLink("Details", "Details", new { id=item.id }) |

            @Html.ActionLink("Delete", "Delete", new { id=item.id })

        </td>

    </tr>

}


</table>

Above File is CRUDAppUsingADO\Views\Home\Index.cshtml






Comments

Popular posts from this blog

Create a User Registration Form in ASP.NET using SQL Server, Visual Studio 2022 & Bootstrap

Create a Simple Login Form in ASP.NET using Visual Studio 2022

SqlCommand Class ADO.Net | ExecuteNonQuery | ExecuteReader | ExecuteScalar