---
title: "How to use Stored Procedure with Web Service Application in C#.Net"  
description: "In this article, I am saving a record into the database using a stored procedure with the help of web service application.  To implement this, first o"  
author: "mohan kumar"  
published: 2012-03-21  
updated: 2020-07-10  
canonical: https://www.mindstick.com/articles/894/how-to-use-stored-procedure-with-web-service-application-in-c-sharp-dot-net  
category: "web services"  
tags: ["web services"]  
reading_time: 3 minutes  

---

# How to use Stored Procedure with Web Service Application in C#.Net

In this article, I am saving a record into the database using a [stored procedure](https://www.mindstick.com/articles/36/stored-procedure-in-microsoft-sql-server) with the help of [web service](https://www.mindstick.com/articles/45/web-services-in-asp-dot-net) application. To implement this, first of all you’ve to create a database and stored procedure. Let’s have look on it.\

##### Creating Database:

Here, I’m creating a table with ‘**userdet’** name which has four columns: ‘**userid’, ‘[username](https://www.mindstick.com/forum/12798/there-is-no-viewdata-item-of-type-ienumerable-selectlistitem-that-has-the-key-username)’, ‘city’ and ‘age’.**

```
create table userdet
(
UserId int primary key,
UserName varchar(30),
city varcahar(20),
age int 
)
<![if !supportLineBreakNewLine]> 
<![endif]>
```

Creating Stored Procedure:

##### I’m creating a stored procedure with name ‘myprocedure’.

```
ALTER PROCEDURE myprocedure
(
@puserid int,
@pusername varchar(30), 
@pcity varchar(20),
@page int 
) 
AS
BEGIN
insert into userdet values(@puserid,@pusername,@pcity,@page)
END 
<![if !supportLineBreakNewLine]> 
<![endif]>
```

##### To create a web service application, follow the following given steps.

Step 1: Go to [Visual Studio](https://www.mindstick.com/articles/12378/visual-studio-for-mac-is-out-of-beta-preview-now-officially-available) 2010 and create a New Project.

Step 2: Select an ASP.NET [Web Application](https://www.mindstick.com/articles/13069/progressive-web-application-pwas-all-you-need-to-know-about).

Step 3: Go to Solution explorer and right click at your project.

Step 4: Add a Web Service Application.

Step 5: Give it a name and click ok button.

Step 6: Replace the given code with following code.

```
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Data.SqlClient;
 
 
namespace usingparameterwebservice
{
    
{
    ///<summary>
    /// Summary description for mywebservice
    ///</summary>
    [WebService(Namespace = "mystoredprocedure.org")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel. [WebServiceBinding(ConformsTo =WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    //[System.Web.Script.Services.ScriptService]
    public class mywebservice : System.Web.Services.WebService
    {
        string constring= "Database=emp;server=.;user=sa;password=Password$2";
        SqlConnection conn;
        SqlCommand comm;        
        [WebMethod(Description= SqlConnection conn;
        SqlCommand comm;
        
        [WebMethod(Description="Simple Example")]
        
        public string data(int id,string name,string city,int age)
        {
            conn = 
        {
            conn = new SqlConnection(constring);
            conn.Open();           
            comm = 
            conn.Open();
           
            comm = new SqlCommand();
           
comm.Connection=conn;
           
comm.CommandType = System.Data.
           
comm.Connection=conn;
           
comm.CommandType = System.Data.CommandType.StoredProcedure;
           
comm.CommandText =       
comm.CommandText = "myprocedure";
           
comm.Parameters.AddWithValue(         
comm.Parameters.AddWithValue("@puserid", id);
           
comm.Parameters.AddWithValue(          
comm.Parameters.AddWithValue("@pusername", name);
           
comm.Parameters.AddWithValue(           
comm.Parameters.AddWithValue("@pcity", city);
           
comm.Parameters.AddWithValue(           
comm.Parameters.AddWithValue("@page", age);
            
            try
     
      {
               
comm.ExecuteNonQuery();
                return "Record Saved";
            }
            
            }
            catch (Exception)
            {
                
            {
                return "Not  Saved";
            }
            
            }
            finally
           
{
               
conn.Close();
            }
        }
    }
}
```

Now run the application.

Output: **\** \
![How to use Stored Procedure with Web Service Application in C#.Net](https://www.mindstick.com/mindstickarticle/1bee1b0e-3b6c-4c73-bdfc-871f75dccbc9/images/b152e411-0f72-44d0-8b68-90300dec72ef.png) \

Click on link "data" to go on the test page. Fill appropriate information’s in text fields which you want to insert in database table, after that click on button ‘Invoke’.

##### Output:

\
![How to use Stored Procedure with Web Service Application in C#.Net](https://www.mindstick.com/mindstickarticle/1bee1b0e-3b6c-4c73-bdfc-871f75dccbc9/images/93da8be8-bba5-4333-8b2d-cd20269f536e.png) \
If user entered right information in form, then records will be saved otherwise it will not be saved into [database table](https://www.mindstick.com/forum/159366/how-to-test-regular-expressions-in-sql-without-using-database-table).\

##### \
Output:

\
![How to use Stored Procedure with Web Service Application in C#.Net](https://www.mindstick.com/mindstickarticle/1bee1b0e-3b6c-4c73-bdfc-871f75dccbc9/images/a7190ac5-7622-4ef6-8472-a80ed853ec1b.png) \
\
The record will not be saved because id has been taken as primary key and it can be unique value, here we have been saving another record with id 1.

##### Output:

**\** **![How to use Stored Procedure with Web Service Application in C#.Net](https://www.mindstick.com/mindstickarticle/1bee1b0e-3b6c-4c73-bdfc-871f75dccbc9/images/9101290a-1ccf-4328-afc3-dae43d77821a.png)\
\** Click on ‘invoke’ button. **\** \

##### Output:

\
![How to use Stored Procedure with Web Service Application in C#.Net](https://www.mindstick.com/mindstickarticle/1bee1b0e-3b6c-4c73-bdfc-871f75dccbc9/images/2a6717ae-a040-4445-87e1-675a27baab52.png)

\

---

Original Source: https://www.mindstick.com/articles/894/how-to-use-stored-procedure-with-web-service-application-in-c-sharp-dot-net

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
