Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Introduction to Web Service in ASP.NET



How to use Web service in Asp.net ?
Web services
Introduction: - It is the communication between two different or  same platform  applications  that allows to use their web method. Client application can call by making HTTP requests across the Web. ASP.NET enables you to create custom Web services or to use built-in application.

 
So let us How to use web service using a template
  1. "Start" - "All Programs" - "Microsoft Visual Studio 2013"
  2. "File" - "New Project" – “Templates”-“Web”-“Visual Studio 2012”
  3. Select ASP.NET Web Service Application
 

4.Go to Solution Explorer open Service1.asmx

 
5.Create a web Method converttodays
[WebMethod]
        public int converttodays(int day, int month, int year)
        {
            DateTime dt = new DateTime(year, month, day);
            int datetodays = DateTime.Now.Subtract(dt).Days;
            return datetodays;
        }

 

6.Now Run the code

 
7.Click on method coverttoday , a new window will come which will show the few textbox in which you need to enter the parameter value and click on Invoke Button.

 

8.Result will show the difference in days as compare to current date.



How to use the web service in Asp.net Application
1.       1.Open Asp.net web application 
         
2.Add Textbox and button on the web page.
@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>

DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

html xmlns="http://www.w3.org/1999/xhtml">
head runat="server">
    title>title>
head>
body>
   form id="form1" runat="server">
         div>
            h3>Using the Web Serviceh3>
             br />
             table style="width:100%;">
                 tr>
                     td>Datetd>
                     td>
                         asp:TextBox ID="txtDate" runat="server">asp:TextBox>
                     td>
                     td> td>
                 tr>
                 tr>
                     td>Monthtd>
                     td>
                         asp:TextBox ID="txtMonth" runat="server">asp:TextBox>
                     td>
                     td> td>
                 tr>
                 tr>
                     td>Yeartd>
                     td>
                         asp:TextBox ID="txtYear" runat="server">asp:TextBox>
                     td>
                     td> td>
                 tr>
                 tr>
                     td> td>
                     td>
            asp:Button ID="btService" runat="server" onclick="btnservice_Click"  Text="Calculate" style="width:99px" />
           
            asp:Label ID="lblmsg" runat="server">asp:Label>
           
                     td>
                     td> td>
                 tr>
             table>
         div>
      form>
body>
html>

3.Go to Solution explorer add the Reference of the web service in it.

 
4.Copy the url of the webservice and paste it in URL and click on go
http://localhost:25105/Service1.asmx

 
5.Click on Add reference . Open solution explorer you will see reference has been added in it.
6. Add the Reference of the Webservice in Aspx.cx
using localhost;
protected void btnservice_Click(object sender, EventArgs e)
    {
        Service1 objservice = new Service1();
        int date = Convert.ToInt32(txtDate.Text);
        int month = Convert.ToInt32(txtMonth.Text);
        int year = Convert.ToInt32(txtYear.Text);
        lblmsg.Text="Diffence in Days: "+ objservice.converttodays(date, month, year);
    }

7. Run your application
NOTE: Make sure your both application are in running mode means your Web application and Web service otherwise it will though an error.





This post first appeared on Pivot In SQL Server, please read the originial post: here

Share the post

Introduction to Web Service in ASP.NET

×

Subscribe to Pivot In Sql Server

Get updates delivered right to your inbox!

Thank you for your subscription

×