Creating and Consuming Overloaded Method in Web Service


Introduction: Function overloading means more than one function with same name and different parameters. In this article, I have used the concept of function overloading and created a web service to check the  amount of users depending on different parameters. First we create a Database and insert some records in Database table.

Creating Database:

create
table transreport
(

 id int,
 TranType varchar(10),
 TransactionDate date,
 
Amount money

)

Now insert some values in
transreport table.(I am executing select command to show some record of my Database table. So that I have used the record in the code.)

select * from transreport

function overloading in web service

Creating Web Service:

Create ASP.NET Web Service page and write the following code in the .asmx.cs file.

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

namespace
WebService1
{
    /// <summary>
    /// Summary description for Service1
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.None )]
    [System.ComponentModel.ToolboxItem(false)]
    // 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 Service1 : System.Web.Services.WebService
    {

   //(MessageName = "methodofaddingthreeinteger",Description="bb")

        [WebMethod(MessageName="TotalAmount",Description="Know Yout Total Amount")]
        public double amount(int accno)
        {
            SqlDataAdapter da=new SqlDataAdapter("select sum(Amount) as Total,TranType  from transreport where id="+accno+" group by TranType order
            by TranType desc"
,@"Data Source=SERVER_NAME;Initial Catalog=EMP;Integrated Security=True");
            DataSet ds=new DataSet();
            da.Fill(ds);
            double i = double.Parse(ds.Tables[0].Rows[0][0].ToString());
            double j = double.Parse(ds.Tables[0].Rows[1][0].ToString());
            return i - j;
        }
        [WebMethod(MessageName = "AmountBetweentwoDates",Description="checked Your Amount Between Dates")]
        public DataSet amount(int accno, string startdate, string enddate)
        {
            SqlDataAdapter da = new SqlDataAdapter("select * from transreport where id="+accno+" and transactiondate between '"+startdate+"' and
            '"+enddate+"' ", @"Data Source=SERVER_NAME;Initial Catalog=EMP;Integrated Security=True");
            DataSet ds = new DataSet();
            da.Fill(ds);
            return ds;
        }
     }
}
 

Run the service.

Output:

function overloading in web service

Click on amount to check it's working. A new page will be open. Write the accno, startdate and end date (It is basically for showing total transaction between two dates).

function overloading in web service

Click at Invoke to i
nvoke the method. It will show the record.

function overloading in web service

Same as you can check another method. Now we use this service in our web application. Follow the given steps.

Consuming Web Service:

Now, we consume (use) the web service in our web application. Follow the given steps for doing this.

  • Take a web application.
  • Go to Solution Explorer and right click at your project. Click at Add Web Reference.
  • Now paste the URL of your service and click at Go button.
  • Click at Add reference. Now service has added into your project.
  • Go to design page and take some interface control in a ordered form.( I am giving a screen shot of my design page.)




    function overloading in web service

     
  • Write the following code in the .aspx.cs file.

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)
    {
        TextBox1.Visible = false;
        TextBox2.Visible = false;
        TextBox3.Visible = false;
        Label1.Visible = false;
        Label2.Visible = false;
        Label3.Visible = false;
        Label4.Visible = false;
        lbl1.Visible = false;
        GridView1.Visible = false;
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        TextBox1.Visible = true;
        Label1.Visible = true;
       
TextBox2.Text = "";


    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        TextBox1.Visible = true;
        TextBox2.Visible = true;
        TextBox3.Visible = true;
        Label1.Visible = true;
        Label2.Visible = true;
        Label3.Visible = true;
        Label4.Visible = true;
    }
    protected void Button3_Click(object sender, EventArgs e)
    {
        localhost.Service1 ob = new localhost.Service1();
        if (TextBox2.Text=="")
        {
            lbl1.Text = "Your Current Amount is " + ob.amount(int.Parse(TextBox1.Text)).ToString();
            lbl1.Visible = true;
        }
        else
        {
            GridView1.Visible = true;
            GridView1.DataSource = ob.amount(Int32.Parse(TextBox1.Text),Convert.ToString(TextBox2.Text),Convert.ToString (TextBox3.Text)).Tables[0];
            GridView1.DataBind();
        }
    }
}

Run the web application.

Output:

function overloading in web service

Now a user can check the current amount by giving Account No. or check total transaction between two dates. Actually I have set visibility false to other controls. It will get visible according to requirement of user. So, Click at "Total Amount" Button and write the account number. Look at below figure.

function overloading in web service

Click at Button. It shows total amount in account. Like as below figure.

and click at Button

To know transaction between two dates. Click at "Amount Between Dates" -> Write the A/c no., StartDate and EndDate. Look at below figure.

function overloadig in web service


Click the "Button".

Output:

function overloadig in web service


Up Next
    Ebook Download
    View all
    Learn
    View all