There is nothing like a lazy rainy Sunday evening. Curled up on my sofa with a book. I paused occasionally to catch a glimpse of the rain-drenched greenery outside my window. It was heaven. The onlyâŠ
Sameer Gaikwad CRUD Operation SQL stored procedure mesameergaikwad
In this post, Iâm going to discuss the CRUD (Create, Read, Update, and Delete) Operations in an ASP.NET MVC application by using raw ADO.NET. Most of the new learners, who started to learn MVC asked this frequently. Thatâs the main reason I wrote this tutorial. In this article, Iâm going to explain the step-by-step procedure from DB table creation to all MVC files.
Software Requirements
For this particular application, I have used the following configuration:
Step 1: Execute the following script on your DB.
Step 2: Create an âEmptyâ ASP.NET MVC application in Visual Studio 2015.
Step 3:
Add an âEmptyâ Controller by right-clicking on the âControllerâ folder. Select âAddâ then select âController..â. In the popup select âMVC 5 Controllerâ, then click âAddâ. In the next popup, you should give the name âCRUDControllerâ.
Step 4:
Our DB access code is going to be placed inside the âModelsâ folder. The model is just a âClassâ file. So we are going to create a Model class for our purpose as below:
Step 5:
Till now we created classes for âControllerâ and âModelâ. We didnât create any views till now. In this step, we are going to create a view, which is going to act as a âhomeâ page for our application. In order to create the view:
Step 6:
We donât have a controller named âDefaultâ, which is specified in the âRouteConfig.csâ file. We need to change the controllerâs name to âCRUDâ, in the default route values.
Route.config
After the above change, just press F5 in Visual Studio, to verify that our application works fine without any error.
Step 7:
In order to achieve the complete CRUD operation, Iâm going to add a number of views as described in Step 5. Here is the complete list of views and their purpose.
ViewPurposeHome.cshtmlThis is the default view. Loaded when the application launched. Will display all the records in the tableCreate.cshtmlDisplays controlâs to insert the record. Will be rendered when the âAdd New Recordâ button is clicked on the âHome.cshtmlâ view.Edit.cshtmlDisplays controlâs to edit the record. Will be rendered when the âEditâ button is clicked on the âHome.cshtmlâ view.
Step 8:
The model class contains all the âData Accessâ logic. In other words, it will interact with the Database and give it back to âViewâ through âControllerâ.
CRUDModel.cs
CRUDController.cs
In an MVC application, controller is the entry point. The following code contains all the action methods for the complete CRUD operation.
Views
Views are a combination of markup as well as server-side code. As you noticed the views âHomeâ and âEditâ take the ADO.NET object Datatable as a model. Also, for simplicity, I donât use âLayoutâ.
Home.cshtml
Create.cshtml
Edit.cshtml
Readers, I hope you like this article. Let me know your thoughts as comments.
Commitment can be a scary, anxiety inducing idea for an overwhelming percentage of people. When we think about the concept of commitment in the context of life in general, we tend to associate itâŠ
I definitely think so. And one of my favorite ways of trying to do that is by telling jokes. Clever jokes as often as I can find them, but even more often, my preferred jokes are rather cheesy. SomeâŠ
Today is one of those days where more things are tugging at me trying to capture my light â my happier side. The easy thing would be to slip into my own cocoon and be bitter at everything that isâŠ