Saturday 3 August 2013

Method Overloading in web services Asp Net

Method Overloading in web services Asp Net

 First we create a web services which  name WebService.cs  is   in Website and call it’s another  Website……………….

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;

/// <summary>
/// Summary description for WebService
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]

//By Default  this  code here but when you are using Overload method  ………………………….
//[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]

[WebServiceBinding(ConformsTo = WsiProfiles.None)]

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

public WebService () {

//Uncomment the following line if using designed components
//InitializeComponent();
}

[WebMethod(MessageName = "a1")]
public double AddNo(double a, double b)
{
double c = a + b;
return c;
}
[WebMethod(MessageName = "a2")]
public double AddNo(double a, double b,double c)
{
double d= a + b+c;
return d;
}
}

Take  another Website  and  call above Web Services ……………………………

<%@ 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>
<table width="50%" >
<tr><td>Enter No1</td><td>
<asp:TextBox ID="txtno1" runat="server"></asp:TextBox></td></tr>
<tr><td>Enter No2</td><td><asp:TextBox ID="txtno2" runat="server"></asp:TextBox></td></tr>
<tr><td>Enter No3</td><td><asp:TextBox ID="txtno3" runat="server"></asp:TextBox></td></tr>
<tr><td><asp:Button ID="btnAdd" runat="server" Text="Add" onclick="btnAdd_Click" /></td>
<td><asp:Label ID="lblresult" runat="server"></asp:Label></td></tr>

</table>

 </div>
</form>
</body>
</html>


Code for Default.aspx.cs……………………………………………………………


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}
double result;

localhost.WebService we = new localhost.WebService();

//Call this function here(Overloading in Web Services)

protected void btnAdd_Click(object sender, EventArgs e)
{

if ( txtno3.Text==String.Empty)
{
result=we.AddNo(Convert.ToDouble(txtno1.Text), Convert.ToDouble(txtno2.Text));
lblresult.Text = "Addtion with two No=" + result.ToString();

}
else
{
result = we.AddNo(Convert.ToDouble(txtno1.Text), Convert.ToDouble(txtno2.Text),Convert.ToDouble(txtno3.Text));
lblresult.Text = "Addtion with three No=" + result.ToString();
}

}
}
Addition  with three no ………………………….









0 comments:

Post a Comment