Call a Web Service from a Web Form

How to call a web service from a web form:  We have two options for that. One is by calling it through Script Manger by passing a web service reference, and the second is by using Ajax Call. Using Script Manger is a very easy process but sometimes we need to call a web service through Ajax. I will explain both of them.
 
First: Calling Web Service Through Script Manager
 
You have to use script manager in your Asp.net aspx page and have to add a web service reference of the service you want to call. Check out the below image regarding how to add a reference in Script Manager-:
 
 
In the above image I have given the reference of WebService.asmx in which I have a web method which I want to call.
 
Now in that web service I have to uncomment the below line if I want to call that web method, if I don't uncomment the below mentioned line, my web method will not call.
 
 
 
Now we have to call the HelloWorld web method, for that we will create a simple method in which we will call the web service, look at the below image-:
 
In the image you can see that we just write the WebService name and the method we want to call and create two methods, OnSuccess and OnFailure, to get the web service response.
 
Call a Web Service Web Method using Ajax Call
 
We will write a simple Ajax call for using a web service, please check the below mentioned image,
 
 
You can see in the above image, we are using the Ajax call on button click event in Jquery to call a Web Service. We are writing the webservice name and the web method we want to call; we don't have any parameters in that web method so we didn't pass any data in the Ajax call, if we have it we can pass it within the data using any format according to our need.

Make sure that the reference you are passing regarding the web service is right, if the reference path or the data you are passing is in the wrong format it will give you the 500 internal error.

At last from both the methods we will have the following output,

 
Ebook Download
View all
Learn
View all