What are Web services?

Web services are basically a XML or Json based information exchanged system used over internet within different programming platforms. Web service provide bridge between two platforms for example, if we want to connect our Android application with MS SQL database, asp.net asmx web service help us to do this. Web services exchange data over internet using SOAP (Simple Object Access Protocol).

This blog will explain you how to create Web service in asp.net using c# with XML response and performing CRUD (Create, Read, Update and Delete) operations using it.

Creating empty web site and MS SQL database

Step 1. Create an empty website from Visual Studio. (Open Visual Studio then select File-> New Web Site OR File-> New-> Web Site)

Step 2. Create database with single table with required columns ie. UserID, FullName, EmailAddress, Gender, Hobbies.

Creating Web services

Step 1. Right click on your website and select Add New Item.

Step 2. Select "Web Service" from the list. Give proper name to it and Add it to your web site by clicking Add button. Please remember to select Visual C# from left side of this window.

You can see two files has been added to your website 1. crudService.asmx and 2. crudService.cs inside App_Code folder. crudService.asmx file is basically used to run web services and crudService.cs has c# web methods inside it to perform connectivity tasks.

By default HelloWorld() web method is given.

[WebMethod]

public string HelloWorld() {

    return "Hello World";

}

What is Web Method [WebMethod]?

Web method [WebMethod] is an attribute used at top of methods written in web service. Basically this attribute shows that the method will be accessed over internet by users to perform task like database connectivity. If you do not used [WebMethod] attribute, it will become simple function and can’t be accessed over internet by users. 

Step 3. Create web methods [WebMethod] for operation you have to perform using web service. Here I have created web methods for Inert, Update, Delete and Select operations. You may add more methods as per your requirements. Below is my code.

crudService.cs

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.Services;

using System.Data.SqlClient;

using System.Data;

  

/* // <summary> */

/* // Summary description for crudService */

/* // </summary> */

[WebService(Namespace = "http://tempuri.org/")]

[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]

/* 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 crudService : System.Web.Services.WebService

{

    /* Connection string */

    string strCon = "Data Source=DELL-PC;Initial Catalog=dbWebService;Integrated Security=True";

    public crudService()

    {

        /* Uncomment the following line if using designed components */

        /* InitializeComponent(); */

    }

  

    /* Insert Service */

    [WebMethod]

    public string fnRegistration(string FullName, string EmailAddress, string Gender, string Hobbies)

    {

        SqlConnection con = new SqlConnection(strCon);

        SqlCommand cmd = new SqlCommand("INSERT INTO tblCRUD(FullName,EmailAddress,Gender,Hobbies) VALUES('" + FullName + "','" + EmailAddress + "','" + Gender + "','" + Hobbies + "')", con);

        con.Open();

        int isInserted = cmd.ExecuteNonQuery();

        con.Close();

  

        if (isInserted > 0)

        {

            return "Registration successfully..!!";

        }

        else

        {

            return "Error.. Please try again..!!";

        }

    }

  

    /* Select Service */

    [WebMethod]

    public DataTable fnSelect()

    {

        SqlConnection con = new SqlConnection(strCon);

        SqlDataAdapter da = new SqlDataAdapter("SELECT * FROM tblCRUD", con);

        DataTable dt = new DataTable("tblCRUD"); /* Table name is must be given. */

        da.Fill(dt);

        return dt;

    }

  

    /* Update Service */

    [WebMethod]

    public string fnUpdate(int UserID, string FullName, string EmailAddress, string Gender, string Hobbies)

    {

        SqlConnection con = new SqlConnection(strCon);

        SqlCommand cmd = new SqlCommand("UPDATE tblCRUD SET FullName='" + FullName + "',EmailAddress='" + EmailAddress + "',Gender='" + Gender + "',Hobbies='" + Hobbies + "' WHERE UserID='" + UserID + "'", con);

        con.Open();

        int isUpdated = cmd.ExecuteNonQuery();

        con.Close();

  

        if (isUpdated > 0)

        {

            return "Updated successfully..!!";

        }

        else

        {

            return "Error.. Please try again..!!";

        }

    }

  

    /* Delete Service */

    [WebMethod]

    public string fnDelete(int UserID)

    {

        SqlConnection con = new SqlConnection(strCon);

        SqlCommand cmd = new SqlCommand("DELETE FROM tblCRUD WHERE UserID='" + UserID + "'", con);

        con.Open();

        int isDeleted = cmd.ExecuteNonQuery();

        con.Close();

  

        if (isDeleted > 0)

        {

            return "Deleted successfully..!!";

        }

        else

        {

            return "Error.. Please try again..!!";

        }

    }

}

Run and Test web services

Step 1. To run our web service right click on crudService.asmx file from Solution Explorer where our web site is located. Select option View in Browser.

Step 2. Service will be run in browser window and it will look like,

All the [WebMethod] of you .cs page will be display here.

Step 3. Invoke it by click on method.

Step 4. Fill the detail and click Invoke button. Please note that, parameter passed in [WebMethod] will be converted into textbox at running time.

When you click on Invoke button, it will gives you output in xml format.

Output

Hope you will like this blog. Feel free to ask question. Thanks You… :)