In this article we will learn how to Bind generic list from stored procedure using Dapper ORM. To learn more about dapper ORM please read my following articles
- Installing Dapper ORM Into MVC Project
- CRUD Operations In ASP.NET MVC 5 Using Dapper
- Stored Procedure Parameter Mapping with Dapper
- How To Pass Generic List Using Dapper In MVC
- How To Pass Stored Procedure Using Dapper In MVC
public class EmpModel { [Display(Name = "Id")] public int Empid { get; set; } [Required(ErrorMessage = "First name is required.")] public string Name { get; set; } [Required(ErrorMessage = "City is required.")] public string City { get; set; } [Required(ErrorMessage = "Address is required.")] public string Address { get; set; } }
Now consider following stored procedure using we are getting the records from database
CREATE Procedure [dbo].[GetEmployees] as begin select Id as Empid,Name,City,Address from Employee End
Now following is the function which is used to bind generic list from stored procedure using Dapper
public List<EmpModel> GetAllEmployees() { try { connection(); con.Open(); IList<EmpModel> EmpList = SqlMapper.Query<EmpModel>( con, "GetEmployees").ToList(); con.Close(); return EmpList.ToList(); } catch (Exception) { throw; } }
In the above function
- Connection() is the method which contains the connection string .
- Con is the SqlConnection class object.
- AddNewEmpDetails is the stored procedure.
- EmpModel is model class.
I hope this article is useful for all readers. If you have any suggestion then please contact me.
Read my other MVC articles
- ActionResult in ASP.NET MVC.
- Creating an ASP.NET MVC Application.
- CRUD Operations In ASP.NET MVC 5 Using ADO.NET .
- Convert DataTable To Generic List In ASP.NET MVC .
- How To Pass Stored Procedure Using Dapper.NET In MVC
- Show Confirm Alert Box on ActionLink Click In ASP.NET MVC.
- Convert DataTable To Generic List Using LINQ In ASP.NET MVC.
- How to Change ActionLink Text Color In ASP.NET MVC .
- How to Call Another Controller View Using ActionLink In ASP.NET MVC .
- Convert DataTable To Generic List Using AsEnumerable In ASP.NET MVC .
- How To Pass Generic List Using Dapper In MVC
- CRUD Operations In ASP.NET MVC 5 Using Dapper
Post a Comment