Note. Check out my previous article, “Basic Implementation Of WebGrid Control In ASP.NET MVC 4.0.”
Link Ref
Just go through the link shown above to get a very basic idea about WebGrid in MVC for how to write the code to show the WebGrid in view part as well as the basic functionality.
New Session About Web Grid
In this article, I will show you how to fetch the data dynamically from the database and bind it to the WebGrid control using the stored procedure in MVC 4.0.
Step 1. We have to create one table.
Table syntax
We have to insert some dummy data in the table.
Result 1
![Dummy data in the table]()
Now, we have to create one stored procedure for select statement.
Stored procedure syntax
In this stored procedure, the table name “Product” is mentioned (that you created earlier.)
To know or show the data, execute the stored procedure. It will show the above-mentioned records that you have inserted earlier.
Step 2. Create a project named “Gridsample”.
![Create project Gridsample]()
You can give any name, per your wish or requirement. To know more, follow my blogs for a better understanding.
Step 3. Put connection string in web.config file to connect to the database and access the data.
Code Ref
Here, you can check that I have mentioned one connection string name “Mycon” . This name is the name mentioned to you to access the database.
![Connection string name]()
Step 4. Create a Connection.cs class file in a Connection folder to connect to the database and execute the stored procedure to show the data.
Code Ref
The above-mentioned ADO.NET Objects In Connection.cs are described clearly below. The connection string's name is “Mycon”. The stored procedure name is “sp_product”.
![ADO.NET Objects In Connection.cs]()
Step 5. Create a model class file named modeldata.cs to declare some entities, which should be the same as the table parameter in the stored procedure to access the data from the database.
Code Ref
![model class file named modeldata.cs]()
Step 6. Create a controller class file GridviewController.cs in the Controllers folder.
Code Ref
![]()
The code mentioned above, described with a red comment line, is for better understanding.
Step 7. Create a view called the grid.cshtml Inside Gridview folder of Views folder.
Code Ref
The code mentioned above in the cshtml file is described in green mark with the comment line. Also, check my previous article, as mentioned in the link URL above.
![]()
Step 8. Now, I have to set the start page at the page load time In Global.asax.cs file.
Code Ref
Here, I have mentioned a new thing: you can set the start page using global.asax.cs like routeconfig.cs file in app_start folder.
In my previous documents, I mentioned the start page using routeconfig.cs file in the app_start folder. Check my previous documents carefully.
![routeconfig.cs file]()
Output
Out of the total of 9 records shown in GridView, each page has four records, as mentioned in grid.cshtml, and the last page shows only one record.
![]()
![]()
![]()
Now, I will show you a new record by inserting a new record in the table “Product”. Check Result 1 in the table output records.
Step 9. Result 2
The new record is created by executing the stored procedure.
![]()
Step 10. Output In GridView is shown below after inserting a new record using the back end.
![]()
Now, the data connection is established, and the records are fetched from the database, as it is working successfully.
Similarly, you can bind the WebGrid with the database, using the stored procedure or using SQL Server in ASP.NET MVC 4.0.
Summary
- How to bind the WebGrid to the database and fetch the data.
- ADO.NET is the concept used in MVC to fetch the records and bind to the WebGrid.
- How to execute the stored procedure by inserting the new records, which are shown in the WebGrid.
- For other functionalities of the WebGrid, you can check my previous documents.
Happy coding.