Hi I Have a web method on the Server. Below Is My Web Method.

[WebMethod]
    public string GetAge(int year, int month, int day)
    {
        DateTime birthDate = new DateTime(year, month, day);
        long age = new DateTime(DateTime.Now.Ticks - birthDate.Ticks).Year - 1;
        return "You are " + age.ToString() + " years old.";
    }

    //This method caches the datetime for 4 seconds. 
    //also a simple cache implementation. 
    private const int CacheTime = 4; // seconds
    [WebMethod(CacheDuration = CacheTime,
    Description = "As simple as it gets - the ubiquitous Get Date Time.")]
    public string GetDateTime()
    {
        return DateTime.Now.ToString();
    }

Now I Want To run This Webmethod Through HTML Page which is on my system.

This Is my Code. But I am not getting the application run.

Below I have given my code.

<html>
<head>
    <title>UseSwap</title>
        <script language="JavaScript">
            function InitializeService()
            {
                service.useService("http://localhost:1394/MyWebService.asmx?wsdl",  "GetAgeService");
                
               // http://localhost:3330/EIMBA/ServiceEIMBA.asmx
            }
            var StrYear, StrMonth, StrDay;
            function GetAge()
            {
                StrYear = document.DemoForm.StringYear.value;
                StrMonth = document.DemoForm.StringMonth.value;
                StrDay = document.DemoForm.StringDay.value; 
                
                service.GetAgeService.callService("GetAge", StrYear, StrMonth, StrDay);
            }
            function ShowResult()
            {
                alert(event.result.value);
            }
    </script>
</head>
    <body onload="InitializeService()"  id="service" style="behavior:url(webservice.htc)"  onresult="ShowResult()">
        <form name="DemoForm">
            Year : <input type="text" name="StringYear"/><br />
            Month : <input type="text" name="StringMonth"/><br />
            Day : <input type="text" name="StringDay"/><br /> 
            <button onclick="GetAge()">Get Age</button><br />
        </form>
    </body>
</html>

Can any one tell me how to execute the webservice from my HTML code. any changes needed from my HTML Code.

Hi ,
As per my knowledge you need to add [ScriptService] tag before defining a Web method in service project .

[ScriptService]
public class MyService : System.Web.Services.WebService
    {  
      [WebMethod]

      public string GetAge(int year, int month, int day)

      {

      DateTime birthDate = new DateTime(year, month, day);
      long age = new DateTime(DateTime.Now.Ticks - birthDate.Ticks).Year - 1;
      return "You are " + age.ToString() + " years old.";
      }
       
      //This method caches the datetime for 4 seconds.
      //also a simple cache implementation.
      private const int CacheTime = 4; // seconds

      [WebMethod(CacheDuration = CacheTime,
      Description = "As simple as it gets - the ubiquitous Get Date Time.")]
      public string GetDateTime()
      {

      return DateTime.Now.ToString();

      }
}

After that you need to add namespace of the service in your asp.cs page so that you can call service method thru asp page by using javascript.

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebServiceDemo._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>Untitled Page</title>
    <script type="text/javascript">
        function CallService()
        {
          WebServiceDemo.DemoService.HelloWorld( "Yourself", Callback );
        }
        
        function Callback( result )
        {
          var outDiv = document.getElementById("outputDiv");
          outDiv.innerText = result;
        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
        <asp:ScriptManager ID="ScriptManager1" runat="server" >
            <Services>
                <asp:ServiceReference Path="DemoService.asmx" />
            </Services>
        </asp:ScriptManager>
    <div>
        <asp:Button ID="Button1" runat="server" OnClientClick="CallService();return false;" Text="Button"/>
        <div id="outputDiv" style="width: 100px; height: 100px">
        </div>
    </div>
    </form>
</body>
</html>

For reference go to

http://dotnetbyexample.blogspot.com/2007/10/calling-asmx-web-services-directly-from.html

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.