Implement Insert, Update and Delete Functionality in the WebGrid: Part 1

Introduction

This article explains how to implement insert, update and delete functionality in a WebGrid.

This article will work on a Dynamic Database so all the updates done will affect the database, so you can use this application directly in your MVC Project and can solve the problems of showing the data in the Grid manner and can also make changes in it.

I had divided the complete application into three articles, this is the first article of this three Article Series. Next Article or Second Article can be seen over Here:-
"Implement Insert, Update and Delete Functionality in the WebGrid Part 2"

Step 1

First of all you need to create a database in your SQL Database. The thing to remember is that you need to create one of the Columns with AutoIncrement that should also be the primary key, because if your Database does not have a column with a primary key associated with it then MVC will provide you errors when you attach it to a MVC Application and if this column is not AutoIncrement then the new data will have a problem inserting in a sequential manner.

Now you need to attach with your MVC Application. For that go to the Server Explorer of your application and then right-click to "Add new Database".

crud in webgrid

Now you need to go to the Model Folder of you application and add an ADO.NET Entity Data Model.

crud in webgrid

Step 2

On adding it will ask whether to generate from a database or to create an Empty Model, click on the "Generate From Database" and then click on the "Next" button.

crud in webgrid

Now on the next page choose the database from which this Model should be generated.

crud in webgrid

Now if you check in the Model folder then a class will be created, double-click on it and you will get an SQL table like this:

crud in webgrid

Step 3

Now you need to add a class to this Model folder, for that again click on the Model folder and choose to "Add a New Class".

crud in webgrid

Now open this class and write this code on your Modal Class:

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Data.Entity;  
  6. using System.ComponentModel.DataAnnotations;  
  7. using System.Web.Mvc;  
  8. using System.Linq.Expressions;  
  9. namespace MvcApplication30.Models  
  10. {  
  11.     public class Class1  
  12.     {  
  13.         public int Student_ID { getset; }  
  14.         public string Student_Name { getset; }  
  15.         public string Student_Branch { getset; }  
  16.         public string Student_City { getset; }  
  17.         public string Student_State {get;set;}  
  18.     }  
  19.     public static class SortExtension  
  20.     {  
  21.         public static IOrderedEnumerable<TSource> OrderByWithDirection<TSource, TKey>  
  22.             (this IEnumerable<TSource> source,  
  23.              Func<TSource, TKey> keySelector,  
  24.              bool descending)  
  25.         {  
  26.             return descending ? source.OrderByDescending(keySelector)  
  27.                               : source.OrderBy(keySelector);  
  28.         }  
  29.         public static IOrderedQueryable<TSource> OrderByWithDirection<TSource, TKey>  
  30.             (this IQueryable<TSource> source,  
  31.              Expression<Func<TSource, TKey>> keySelector,  
  32.              bool descending)  
  33.         {  
  34.             return descending ? source.OrderByDescending(keySelector)  
  35.                               : source.OrderBy(keySelector);  
  36.         }  
  37.     }  
  38.      public class ModelServices : IDisposable  
  39.     {  
  40.         private readonly StudentEntities1 entities = new StudentEntities1();  
  41.         public bool SaveStudent(string name, string branch, string city, string state)  
  42.         {  
  43.             try  
  44.             {  
  45.                 IT_Student stdnt = new IT_Student();  
  46.                 stdnt.Student_Name = name;  
  47.                 stdnt.Student_Branch = branch;  
  48.                 stdnt.Student_City = city;  
  49.                 stdnt.Student_State = state;  
  50.                 entities.IT_Student.Add(stdnt);  
  51.                 entities.SaveChanges();  
  52.                 return true;  
  53.             }  
  54.             catch  
  55.             {  
  56.                 return false;  
  57.             }  
  58.         }  
  59.         public bool UpdateStudent(int id, string name, string branch, string city, string state)  
  60.         {  
  61.             try  
  62.             {  
  63.                 var stdnt = (from tbl in entities.IT_Student  
  64.                             where tbl.Student_Id == id  
  65.                             select tbl).FirstOrDefault();  
  66.                 stdnt.Student_Name = name;  
  67.                 stdnt.Student_Branch = branch;  
  68.                 stdnt.Student_City = city;  
  69.                 stdnt.Student_State = state;  
  70.                 entities.SaveChanges();  
  71.                 return true;  
  72.             }  
  73.             catch  
  74.             {  
  75.                 return false;  
  76.             }  
  77.         }  
  78.         public bool DeleteStudent(int id)  
  79.         {  
  80.             try  
  81.             {  
  82.                 var stdnt = (from tbl in entities.IT_Student  
  83.                             where tbl.Student_Id == id  
  84.                             select tbl).FirstOrDefault();  
  85.                 entities.IT_Student.Remove(stdnt);  
  86.                 entities.SaveChanges();  
  87.                 return true;  
  88.             }  
  89.             catch  
  90.             {  
  91.                 return false;  
  92.             }  
  93.         }  
  94.         //For Custom Paging  
  95.         public IEnumerable<IT_Student> GetStudentPage(int pageNumber, int pageSize, string sort, bool Dir)  
  96.         {  
  97.             if (pageNumber < 1)  
  98.                 pageNumber = 1;  
  99.             if (sort == "name")  
  100.                 return entities.IT_Student.OrderByWithDirection(x => x.Student_Name, Dir)  
  101.               .Skip((pageNumber - 1) * pageSize)  
  102.               .Take(pageSize)  
  103.               .ToList();  
  104.             else if (sort == "state")  
  105.                 return entities.IT_Student.OrderByWithDirection(x => x.Student_State, Dir)  
  106.               .Skip((pageNumber - 1) * pageSize)  
  107.               .Take(pageSize)  
  108.               .ToList();  
  109.             else if (sort == "city")  
  110.                 return entities.IT_Student.OrderByWithDirection(x => x.Student_City, Dir)  
  111.               .Skip((pageNumber - 1) * pageSize)  
  112.               .Take(pageSize)  
  113.               .ToList();  
  114.             else  
  115.                 return entities.IT_Student.OrderByWithDirection(x => x.Student_Id, Dir)  
  116.              .Skip((pageNumber - 1) * pageSize)  
  117.              .Take(pageSize)  
  118.              .ToList();  
  119.         }  
  120.         public int CountStudent()  
  121.         {  
  122.             return entities.IT_Student.Count();  
  123.         }  
  124.         public void Dispose()  
  125.         {  
  126.             entities.Dispose();  
  127.         }  
  128.      }  
  129.      public class PagedStudentModel  
  130.      {  
  131.          public int TotalRows { getset; }  
  132.          public IEnumerable<IT_Student> It_Student { getset; }  
  133.          public int PageSize { getset; }  
  134.      }  
  135. } 

First of all I had declared some variables in this class that are similar to the column name of my table.

Then a class is created that will be used to sort the data in ascending or descending order.

Now the main work of this class is started, first of all I created a function for saving the data as SaveStudent, in this function some variables are declared that will be holding the values for various data needed to be inserted in the database. In this function I had created an object of my Database Table class, actually this database class is automatically generated, you just start writing your table name and Intellisense will provide you a class similar to your database table.

crud in webgrid

Similarly, I created the functions for update and delete as well, they both will work on the ID of the data.

At the end you can see that I created a class named PagedStudentModel, this class will be used to call all these classes and functions created in the View, in this class three objects are created, one for the rows, the second for the automatically created class and the third for the page size. You can say that this class will work as a bridge between the View and the automatically generated class.

Our work on the Model class is now completed. Our next work will be to create a class for the Controller and a class for the View and then work on them. That work will be done in my future articles.


Similar Articles