A Kitchen Tale

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


Smartphone

ç‹Źćź¶äŒ˜æƒ ć„–é‡‘ 100% 高蟟 1 BTC + 180 慍èŽčæ—‹èœŹ




Sameer Gaikwad CRUD Operations In ASP.NET MVC Using ADO.NET

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.

Add a comment

Related posts:

Git Commit or Wait to Git Wrecked

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


15 Romantic Jokes to Make Your Sweetheart Smile

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


The Harder Path Is Choosing Kindness

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