SHASHANGKA SHEKHAR'S BLOG

Hi, I am Shashangka Shekhar, Working with Microsoft Technologies. This is my personal blog where I share some of my experiences.

CRUD – ASP.Net MVC, Web Api-2, Windows Form

In this article we will learn basic CRUD operation using Web Api-2, and Stored Procedure with a sample Desktop Application.

Download Source Code

0

Sample Database:

Let’s Create a Sample database named ‘SampleDB’ with SQL Management Studio. Using the ‘SampleDB’ now create a Table name ‘tblCustomer’.

Script:

Stored Procedure:

Now the step we will go through to perform CRUD operations with stored procedure:

  1. First we will create a stored procedure (SP) to RETRIVE record from Customer table
  2. Now we will create a stored procedure( SP) to INSERT record into Customer table
  3. Now we will create another procedure (SP) to UPDATE existing data in our Customer table.
  4. The last step we will create a stored procedure to DELETE existing record from customer table.

 

Stored Procedure to RETRIVE Record:

Stored Procedure to INSERT Record:

Stored Procedure to UPDATE Record:

Stored Procedure to DELETE Record:

Stored Procedure to VIEW Single Record Details:

 

Let’s Start:

Open Visual Studio 2015, Click File> New > Project. In this window give a name the project and solution.

0

Click ok and another window will appear with project template, choose Web API:

9

Click ok and the visual studio will create and load a new ASP.NET application template.

 

In this app we are going to apply CRUD operation on a single table named Customer. To do first we need to create apiController for the operations. To add a new Controller file we need to click right mouse an option menu will appear Click Add > Controller.

11

Let’s name it CustomerController. In the controller we will create action methods to perform CRUD operations:

4

Api Controller for CRUD operations:

 

Data Service:

As we know earlier that we will use ADO.NET, Stored Procedure so to connect data base we need to modify our config file to add Connection String for database connection:

Now we need to create another class to use connection string and open our database connection, let’s name it dbConnector.

To perform CRUD operations we will create a separate class called CrudDataService. In this class we have five methods that will interact with database to perform CRUD operations.

// Database Service

Publish the Site in IIS:

6

Let’s assign a port to access, In this case the site base url is: http://localhost:8081/

7

Let’s Create Windows Form Application:

Open Visual Studio 2015, Click File> New > Project. In this window give a name the project and solution. This time we will create a Windows Form Application.

1

In our new application let’s create a new Form, name it CRUDForm.cs2Our new form will look like this screen:

3

In CRUD form we have a data grid which will load all data from database through api controller.

Form Submission Code:

Desktop Application:

8

In this stage we need to input the HTTP Url to perform CRUD Operation through api controller.

Web Application:

10

OUTPUT:

Finally displaying data in both Web & Desktop Application at the same time using Web API.

Hope this will help someone  …Thanks.

2 Comments

Post a comment