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


StockQuotewebService.cs

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


/// <summary>
/// Summary description for StockQuotewebService
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[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 
        //InitializeComponent(); 
    }


    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
    [WebMethod]
    public SecurityInfo GetComapnyPrice(string code)
    {


       SecurityInfo result= GetInformation(code);
       return result;
    }




}


StockQuotewebService.asmx

<%@ 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


UseStockPricewebService.aspx

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


<!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>
        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 />
    </div>
    </form>
</body>
</html>



UseStockPricewebService.aspx.cs

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