connectseekers.com

Friday, 28 July 2017

Select Data From DataBase Using Entity Framework in Asp.Net MVC

7/28/2017 - By Pranav Singh 0

This article will show you how you can select or retrieve data from data base using entity frame work using linq, C# in asp.net mvc. Data read using linq from SQL server database table.

First we will create a sql table and add some data in it.

CREATE TABLE [dbo].[StudentDetail](
       [Id] [int] IDENTITY(1,1) NOT NULL,
       [Name] [varchar](50) NULL,
       [Address] [varchar](50) NULL,
       [Section] [varchar](50) NULL,
       [Marks] [int] NULL
) ON [PRIMARY]

After adding some data table look as.


So for this article will first we will create a new asp.net mvc application and add entity .edmx file. And include the table.



Now we will add a new model class file and add the below code in it.

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

namespace MVC_Demos.Models
{
    public class StudentModel
    {
        public List<Student> StudentList { get; set; }
    }
    public class Student
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public string Address { get; set; }
        public string Section { get; set; }
        public string Marks { get; set; }
    }
}

After this we will add a controller class file and add the below .

using MVC_Demos.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace MVC_Demos.Controllers
{
    public class HomeController : Controller
    {
        //
        // GET: /Home/
        [HttpGet]
        public ActionResult Index()
        {
            StudentModel _studentModel = new StudentModel();
            _studentModel.StudentList = GetStudentList();
            return View(_studentModel);
        }

        ///

        /// Get student detail from data base list
        ///
        public List<Student> GetStudentList()
        {
            List<Student> _student = new List<Student>();
            //Create instance for entity class
            DemoEntities _demoEntities = new DemoEntities();

            var stuData = from data in _demoEntities.StudentDetails
                          select data;
            foreach (var item in stuData)
            {
                _student.Add(new Student { Id = item.Id, Name = item.Name, Marks = item.Address, Address = item.Address, Section = item.Section });
            }

            return _student;
        }
    }
}

        
Now from above code just check the below code.

        public List<Student> GetStudentList()
        {
            List<Student> _student = new List<Student>();
            //Create instance for entity class
            DemoEntities _demoEntities = new DemoEntities();

            var stuData = from data in _demoEntities.StudentDetails
                          select data;
            foreach (var item in stuData)
            {
                _student.Add(new Student { Id = item.Id, Name = item.Name, Marks = item.Address, Address = item.Address, Section = item.Section });
            }

            return _student;
        }


Here in above code I have created the object for the entity class and then selected data from table all which present in table. After that I have applied look to pull all the data in the list for further processing.

Have a look of the entity class file


Now we will create a view and add the below code in it.

@model MVC_Demos.Models.StudentModel
@{
    ViewBag.Title = "select data from database using entity framework in asp.net mvc";
}

<style type="text/css">
    .gridtable {
        font-family: verdana,arial,sans-serif;
        font-size: 11px;
        color: #333333;
        border-width: 1px;
        border-color: #666666;
        border-collapse: collapse;
    }

        .gridtable th {
            border-width: 1px;
            padding: 8px;
            border-style: solid;
            border-color: #666666;
            background-color: #dedede;
        }

        .gridtable td {
            border-width: 1px;
            padding: 8px;
            border-style: solid;
            border-color: #666666;
            background-color: #ffffff;
        }

        .gridtable tr:hover td,
        .gridtable tr.even:hover td.active,
        .gridtable tr.odd:hover td.active {
            background: #b6ff00;
        }
</style>

@using (Html.BeginForm("Index", "Home"))
{
    <table width="100%" cellpadding="5" cellspacing="2" border="0" style="background-color: White;" class="gridtable">
        <thead>
            <tr>
                <th>Id</th>
                <th>Name</th>
                <th>Marks</th>
                <th>Address</th>
                <th>Section</th>
            </tr>

        </thead>
        @foreach (var item in Model.StudentList)
        {
            <tr>
                <td>@item.Id</td>
                <td>@item.Name</td>
                <td>@item.Marks</td>
                <td>@item.Address</td>
                <td>@item.Section</td>
            </tr>
        }
       
    </table>
}

In above code I have render the model class and bind the detail with table to display the records. Now we have done run the application to check the output.



About the Author

We are the group of people who are expertise in different Microsoft technology like Asp.Net,MVC,C#.Net,VB.Net,Windows Application,WPF,jQuery,Javascript,HTML. This blog is designed to share the knowledge.

Get Updates

Subscribe to our e-mail newsletter to receive updates.

Share This Post

0 comments:

Please let me know your view

Free Ebooks


About Us

We are the group of people who are expertise in different Microsoft technology like Asp.Net,MVC,C#.Net,VB.Net,Windows Application,WPF,jQuery,Javascript,HTML. This blog is designed to share the knowledge.

Contact Us

For writing article in this website please send request by your

GMAIL ID: dotnetpools@gmail.com
Email Id : contact@aspdotnet-pools.com

Bugs and Suggestions

As we all know that this website is for sharing knowledge and providing proper solution. So while reading the article is you find any bug or if you have any suggestion please mail us at contact@aspdotnet-pools.com.

Partners


Top jQuery Plugins

Global Classified : Connectseekers.com
© 2014 aspdotnet-pools.com Designed by Bloggertheme9.
back to top