Here we will understand models with a different library, in other words In MVC. When we run an application we need to specify the correct controller name with an Action method. If we pass the wrong name then it gives an error.
But with a model this is different, in other words a model is optional and we can put it anywhere a library can be put.
Step 1
Create a database table, so for that execute the following code:
- Create Table StudentInfo (
- StudentID int identity primary key,
- StudentName nvarchar(50),
- Gender nvarchar(50),
- SchoolName nvarchar(50)
- ) GO Insert into StudentInfo
- values
- ('Munesh', 'Male', 'VIT') Insert into StudentInfo
- values
- ('Rahul', 'Male', 'ABC') Insert into StudentInfo
- values
- ('Reet', 'Female', 'XYZ') Insert into StudentInfo
- values
- ('Anshuman', 'Male', 'PQR')
Also create a Stored Procedure for retrieving data from the database.
- Create procedure GetAllStudents as Begin
- Select
- StudentID,
- StudentName,
- Gender,
- SchoolName
- from
- StudentInfo End
Step 2
Now add a new library to the solution. So for adding a library to the solution, right-click on the project in the Solution Explorer then select Add -> New Project.
Then the New Project screen will be shown. From that screen we will select "Windows" -> "Class Library" and provide this library a meaningful name such as “BusinessLayer”.
This automatically generates a class such as “Class1.cs”, so change the class name to “Student.cs” and define the database property as we defined in the model classes.

- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- namespace BusinessLayer {
- public class Student {
- public int StudentID {
- get;
- set;
- }
- public string StudentName {
- get;
- set;
- }
- public string Gender {
- get;
- set;
- }
- public string StudentClass {
- get;
- set;
- }
- }
- }
Step 3
Add a another class to this library for writing the business logic code and using that we will retrieve the data from the database. Provide this class the name “BusinessLogic.cs”.
Before writing the code for retrieving the data from the database, first add some references for ADO.Net to this library. Go to the references folder of this library project then right-click on the References folder then select .Net then select "System.Configuration" assembly. Then provide the following code for the class.
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Data;
- using System.Data.SqlClient;
- using System.Configuration;
- namespace BusinessLayer {
- public class BusinessLogic {
- public IEnumerable < Student > student {
- get {
- string connectionString = ConfigurationManager.ConnectionStrings["Conncet"].ConnectionString;
- List < Student > employees = new List < Student > ();
- using(SqlConnection con = newSqlConnection(connectionString)) {
- SqlCommand cmd = new SqlCommand("GetAllStudents", con);
- cmd.CommandType = CommandType.StoredProcedure;
- con.Open();
- SqlDataReader rdr = cmd.ExecuteReader();
- while (rdr.Read()) {
- Student student = new Student();
- student.StudentID = Convert.ToInt32(rdr["StudentID"]);
- student.StudentName = rdr["StudentName"].ToString();
- student.Gender = rdr["Gender"].ToString();
- student.StudentClass = rdr["SchoolName"].ToString();
- employees.Add(student);
- }
- }
- return employees;
- }
- }
- }
- }




Manav PandyaPosted Dec 16, 2016, 3:23 AM
Well explained article sir Munesh Sharma
Santhakumar MunuswamyPosted Jun 29, 2015, 2:44 PM
Thanks for nice article
Munesh SharmaPosted Jun 29, 2015, 5:18 AM
Thank you
Sibeesh VenuPosted Jun 29, 2015, 3:09 AM
Good one...
Gopi ChandPosted Jun 29, 2015, 1:21 AM
Good one