March 25, 2011

WebService Code Example

Below WebService Example gets the Stock Price based on the CompanyCode Entered by the user

1.) Add an asmx page into solution by choosing the webservice Template
2.) Write the code in StockQuotewebService.cs file in APP_CODE Folder


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

/// <summary>
/// Summary description for StockQuotewebService
/// </summary>
[WebService(Namespace = "")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
// 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 StockQuotewebService : System.Web.Services.WebService {

    public StockQuotewebService () {

        //Uncomment the following line if using designed components 

    public class SecurityInfo
        public String CompnanyCode;
        public String CompnanyName;
        public double StockPrice;

        public SecurityInfo()
            CompnanyCode = "";
            CompnanyName = "";
            StockPrice = 0.0;


    //Private Method for the webService
    public SecurityInfo GetInformation(String Code)
        Random rObj = new Random();
        SecurityInfo obj = new SecurityInfo();
        obj.CompnanyCode = Code;
        obj.StockPrice = rObj.Next(1200);
        obj.CompnanyName = Code + "  " + "PVT LIMITED";

        return obj;


    //Exposed Method from the web service
    public SecurityInfo GetComapnyPrice(string code)

       SecurityInfo result= GetInformation(code);
       return result;



<%@ WebService Language="C#" CodeBehind="~/App_Code/StockQuotewebService.cs" Class="StockQuotewebService" %>

After Executing this webservice you ll see the below Interface to test the Service

click on Invoke will show you the below result

3.) Right click on root folder in Solution Explorer add App_WebReference choose the above created
     webservice and give reference name in my case it is 'localhost' ,click on  'Add Reference'
4.) Add an .aspx page to consume the above web service

Below is the code to Consume Above Web Service


<%@ Page Language="C#" AutoEventWireup="true" CodeFile="UseStockPricewebService.aspx.cs" Inherits="UseStockPricewebService" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">

<html xmlns="">
<head runat="server">
    <form id="form1" runat="server">
        Company code:<asp:TextBox ID="txtCode" runat="server"></asp:TextBox><br />
        <asp:Button ID="btnGetQuote" runat="server" Text="GETSTOCKQUOTE" 
            onclick="btnGetQuote_Click" /><br />
        Code<asp:Label ID="lblCode" runat="server" Text=""></asp:Label><br />
        Name<asp:Label ID="lblName" runat="server" Text=""></asp:Label><br />
        Price<asp:Label ID="lblPrice" runat="server" Text=""></asp:Label><br />


using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using localhost;
public partial class UseStockPricewebService : System.Web.UI.Page
    StockQuotewebService wsObj = new StockQuotewebService();
    protected void Page_Load(object sender, EventArgs e)

    protected void btnGetQuote_Click(object sender, EventArgs e)
        StockQuotewebService .SecurityInfo obj = wsObj.GetComapnyPrice(txtCode.Text);
        lblCode.Text ="   : "+ obj.CompnanyCode;
        lblName.Text = "   : " + obj.CompnanyName;
        lblPrice.Text = "   : " + obj.StockPrice.ToString();


Below is the o/p when you run the above seStockPricewebService.aspx 

Click on GETSTOCKQUOTE Button 'll see the below o/p

No comments:

Post a Comment