Skip to main content

Featured Post

How to use Tabs in ASP.NET CORE

I want to show Components in a tabs , so first of all create few components. In this project we have three components, First View Component  public class AllViewComponent : ViewComponent     {         private readonly UserManager<ApplicationUser> _userManager;         public AllViewComponent(UserManager<ApplicationUser> userManager)         {             _userManager = userManager;         }         public async Task<IViewComponentResult> InvokeAsync()         {             List<StudentViewModel> allUsers = new List<StudentViewModel>();             var items = await _userManager.Users.ToListAsync();             foreach (var item in items)             {                 allUsers.Add(new StudentViewModel {Id=item.Id, EnrollmentNo = item.EnrollmentNo, FatherName = item.FatherName, Name = item.Name, Age = item.Age, Birthdate = item.Birthdate, Address = item.Address, Gender = item.Gender, Email = item.Email });             }            

Access data from database table in MVC

In MVC, we can access data from database easily. When we send any request to the server, respond generated by the controller. Data access from the Model and render by the View. First to make a database table using visual studio 2010 or higher. we have database table that is "Employee", in which have some attributes, these are

CREATE TABLE [dbo].[Employee]
(
[Id] INT NOT NULL PRIMARY KEY IDENTITY,
[Name] NVARCHAR(50) NULL,
[Age] INT NULL
)


  1. First to install entity framework by the "Nuget packages", which is available in tools-->Library Package Manager
  2. This is already available in 4.5 framework or 4.5.1.
ntity framework by the "Nuget packages
  1. Now, add the employee class in model folder with some public properties  like
  2.  
     namespace WebApplication10.Models
    {
    public class Employee
    {
    public int ID { get; set; }
    public string Name { get; set; }
    public int age { get; set; }
    }
    }
    
    
  3. Now, add another class which is EmployeeContext.cs class, which is inherit from DBContext class. DBContext class is available in System.Data.Entity namespace. This class is used for making connection. Now, your code look like
  4.  
    using System.Data.Entity;namespace WebApplication10.Models
    {
    public class EmployeeContext: DbContext
    {
    public DbSet<Employee> empl { get; set; }}
    }
    
    Here, empl is the public property, by which we can create the connection to the database.
  5. Now, add the ConnectionString in the web.config file that is
    <add name="EmployeeConnection" connectionString="Data Source=(LocalDb)\v11.0;AttachDbFilename=|DataDirectory|\Database1.mdf;Initial Catalog=Database1;Integrated Security=True"
    providerName="System.Data.SqlClient" />
    
    
  6. Map the database table with the Employee class using Table class which is available in  System.ComponentModel.DataAnnotations.Schema; namespace, after mapping now your code look like
    using System.ComponentModel.DataAnnotations.Schema;namespace WebApplication10.Models
    {[Table("Employee")]
    public class Employee
    {
    public int ID { get; set; }
    public string Name { get; set; }
    public int age { get; set; }
    }
    }
    
  7. Now, create a EmployeeController  in controller folder, Which is inherit from Controller class. This Controller class is exists in System.Web.Mvc namespace. Also create a public method, which return ActionResult class. Now your code look like
     using System.Linq;
    using System.Web.Mvc;
    using WebApplication10.Models;
    
    namespace WebApplication10.Controllers
    {
    public class EmployeeController : Controller
    {
    //
    // GET: /Employee/
    public ActionResult Index()
    {
    EmployeeContext context = new EmployeeContext();
    Employee emp = context.pl.Single();
    
    return View(emp);
    }
    }
    }
    
    Here we create a instance of EmployeeContext class and invoke the public property of that class.
  8. Now create the view for this program. Before adding the view with their presentation must build the project. Learn How to create the new view
 create the view for this program

  1. Prepare the view and access all the properties of the employee class by the @Model properties.
     @model WebApplication10.Models.Employee
    
    @{
    ViewBag.Title = "Employee Details";
    }
    
    <h2>Employee Details are:</h2>
    Employee Id : <p>@Model.ID</p>
    Employee Name : <p>@Model.Name</p>
    Employee Age : <p>@Model.age</p>
    
    
  2. Now run the code and check your desired output
Access data from database table in MVC

Comments

Popular Post

Polynomial representation using Linked List for Data Structure in 'C'

Polynomial representation using Linked List The linked list can be used to represent a polynomial of any degree. Simply the information field is changed according to the number of variables used in the polynomial. If a single variable is used in the polynomial the information field of the node contains two parts: one for coefficient of variable and the other for degree of variable. Let us consider an example to represent a polynomial using linked list as follows: Polynomial:      3x 3 -4x 2 +2x-9 Linked List: In the above linked list, the external pointer ‘ROOT’ point to the first node of the linked list. The first node of the linked list contains the information about the variable with the highest degree. The first node points to the next node with next lowest degree of the variable. Representation of a polynomial using the linked list is beneficial when the operations on the polynomial like addition and subtractions are performed. The resulting polynomial can also

How to use Tabs in ASP.NET CORE

I want to show Components in a tabs , so first of all create few components. In this project we have three components, First View Component  public class AllViewComponent : ViewComponent     {         private readonly UserManager<ApplicationUser> _userManager;         public AllViewComponent(UserManager<ApplicationUser> userManager)         {             _userManager = userManager;         }         public async Task<IViewComponentResult> InvokeAsync()         {             List<StudentViewModel> allUsers = new List<StudentViewModel>();             var items = await _userManager.Users.ToListAsync();             foreach (var item in items)             {                 allUsers.Add(new StudentViewModel {Id=item.Id, EnrollmentNo = item.EnrollmentNo, FatherName = item.FatherName, Name = item.Name, Age = item.Age, Birthdate = item.Birthdate, Address = item.Address, Gender = item.Gender, Email = item.Email });             }            

Memory representation of Linked List Data Structures in C Language

                                 Memory representation of Linked List              In memory the linked list is stored in scattered cells (locations).The memory for each node is allocated dynamically means as and when required. So the Linked List can increase as per the user wish and the size is not fixed, it can vary.                Suppose first node of linked list is allocated with an address 1008. Its graphical representation looks like the figure shown below:       Suppose next node is allocated at an address 506, so the list becomes,   Suppose next node is allocated with an address with an address 10,s the list become, The other way to represent the linked list is as shown below:  In the above representation the data stored in the linked list is “INDIA”, the information part of each node contains one character. The external pointer root points to first node’s address 1005. The link part of the node containing information I contains 1007, the address of