Asp.Net Training - 3 Web Service

Web Service repo -  C:\Users\vasan\OneDrive\Desktop\Online Class\Projects\Web service\WebApplication3




   string[,] stocks =

        {

          {"RELIND", "Reliance Industries", "1060.15"},

          {"ICICI", "ICICI Bank", "911.55"},

          {"JSW", "JSW Steel", "1201.25"},

          {"WIPRO", "Wipro Limited", "1194.65"},

          {"SATYAM", "Satyam Computers", "91.10"}

        };


        [WebMethod]

        public string GetName(string symbol)

        {

            // It takes the symbol as parameter and 

            // returns name of the stock

            for (int i = 0; i < stocks.GetLength(0); i++)

            {

                if (String.Compare(symbol, stocks[i, 0], true) == 0)

                    return stocks[i, 1];

            }


            return "Stock Not Found";

        }




        [WebMethod]

        public Double GetPrice(string symbol)

        {

            // It takes the symbol as parameter and 

            // returns name of the stock

            for (int i = 0; i < stocks.GetLength(0); i++)

            {

                if (String.Compare(symbol, stocks[i, 0], true) == 0)

                    return Convert.ToDouble(stocks[i, 2]);

            }


            return 0;

        }

add another web web form project and design like this



Adding a Web Service Reference in the ASP.Net Web Application


The most important task when consuming a Web Service in an ASP.Net Web Application is adding the Web Service reference into the ASP.Net web application. So how to add it? Let us see the procedure.

Right-click on the ASP.Net Web Application and click on "Add Service Reference" as in the following:

 

AddServiRef.png

Then after clicking on the above option, the following window will appear, then click on the "Advanced" tab.

Advtab.png

Now after clicking on the Advanced tab, it will show the following window then click on the "Add Web Reference" option as in the following in a circle:

FAddsevice.png
.
After clicking on the Add Web Reference tab, it will show the following window. Now this is a very important step, when adding the web reference to the ASP.Net web Application. Since you see "URL" option in the following window, on that window we need to paste or type the Web Service URL address.

RefScreen.png

So how to add the URL Reference in the preceding URL box, let us see the procedure again.

ServiceUrl.png

As you clearly see there, in the preceding window, it displays the method named "converttodaysweb" as we created in our Web Service, now just you need to copy the preceding URL that I have circled in red and paste it into the Step 4 window URL option, then the Step 4 window will look as in the following:

AddingRef.png

-----What After Pasting the URL in the preceding URL box

After pasting the URL in the preceding window box, click on the green right headed arrow button, it will discover the Web Services available related to that URL address and you see that in that related URL one Web Service is found message is displayed along with the Web Service name, "Web Services" in the preceding right hand side window.

The Name of the Web Service is "WebService" because I have given the class name as Web Service, that's why the name is Web Services, in your case it might be different or the class name is anything so you can use any name for Web Service so don't be confused about it.

  • Web Reference Name

In the right hand corner of the window you have seen the option for the Web reference name; the web reference name is anything you wish and this name will be added in your ASP.Net Web Application as allies name for Web Service. In my article I have given the web reference name as "local host".

Then after adding the Web Service reference in the ASP.Net web application the Solution Explorer will look as in the following:

AddedRefinsolu.png

In the preceding window, you have clearly seen that the Web Service reference named "localhost" is added into the ASP.Net web Application. I hope you understand how to add the Web Service reference into the ASP.Net web application.

Calling the Web Service method from the ASP.Net Web Application

We have added the Web Service reference into our web application. Now next is how to call the Web Service method that we created in our Web Service Application from the ASP.Net Web Application.

The following is the procedure:

1. Go to the Default.aspx page of our ASP.Net Web application and double-click on the button that we have placed on the Default.aspx page.

2. Now write the following code in the button click to create the object of the Web Service class:

localhost.webservice age=new localhost.webservice();

In the code above, I have created the object of Web Service class named "age" followed by the Web reference name ("localhost") and Web Service class ("webservice"), I hope you understand how to create the object of the Web Service class.

The entire code of the Default.aspx.cs page will be as follows:

Cscode.png

Code Explanation

In the code above, I first created the object of the Web Service class named "age" followed by Web reference name ("localhost") and Web Service class ("webservice").

Then I declared the three integer variables "day", "month" and "year" to store the values provided by the user as input from the Textbox1, Textbox2 and Textbox3.

Now, in the next step, as you know our new Web Service method takes three parameters, so I ed the three input parameters "day", "month" and "year" to the Web Service method "converttodaysweb".

Then I declared another integer variable, "a", to store the values returned by the Web Service method "converttodaysweb".

And finally I displayed the values returned by the Web Service method "converttodaysweb" on the label control using variable "a" because, as you know, we have stored the returned values of the method into the variable a, so the final result will be stored in the variable a.

Now, run the ASP.Net web application and provide the input of day, month and year. I will enter my Date of Birth and then I will click on the "Calculate" button, it will show the output as in the following:

outputScreen.png

In the preceding screen, you see that currently, I am 8702 days old, which means that for the last 8702 days, I have been on this earth.

Note:
  • For detailed code please download the zip file attached above.
  • Also refer to my previous article about creating a Web Service.

    

Comments

Popular Posts