Section 15.11. StockTickerComplete

15.11. StockTickerComplete

Running the StockTickerComplete example produces the test page shown in Figure 15-10.

Figure 15-10. StockTickerComplete

The complete source code for the code-behind file, App_Code/Service.cs , is listed in Example 15-18. The code is included here to show how all the snippets of code presented fit together.

Example 15-18. Service.csStockTickerComplete code-behind file
 using System.Web; using System.Web.Services; using System.Web.Services.Protocols; using System;                         // necesary for String class using System.EnterpriseServices;      // necessary for transactions using System.Collections;             // necssary for ArrayLists using System.Data;                    // necessary for DataSet using System.Data.SqlClient;          // necessary for DataSet [WebService(Description = "A stock ticker using C#.",            Name = "StockTicker",            Namespace = "www.LibertyAssociates.com")] [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1,                    EmitConformanceClaims = true)] [WebServiceBinding(Name = "OverloadedGetValue",                    ConformsTo = WsiProfiles.BasicProfile1_1,                    EmitConformanceClaims = true)] public class Service : System.Web.Services.WebService {    //  Construct and fill an array of stock symbols and prices.    //  Note: the stock prices are as of 5/1/05.    string[,] stocks =       {          {"MSFT","Microsoft","25.30"},          {"DELL","Dell Computers","34.83"},          {"HPQ","Hewlett Packard","20.47"},          {"YHOO","Yahoo!","34.50"},          {"GE","General Electric","36.20"},          {"IBM","International Business Machine","76.38"},          {"GM","General Motors","26.68"},          {"F","Ford Motor Company","9.11"}       };    [WebMethod(Description =                  "Returns the stock price for the input stock symbol.")]    public double GetPrice(string StockSymbol)    //  Given a stock symbol, return the price.    {       //  Iterate through the array, looking for the symbol.       for (int i = 0; i < stocks.GetLength(0); i++)       {          //  Do a case-insensitive string compare.          if (String.Compare(StockSymbol, stocks[i, 0], true) == 0)             return Convert.ToDouble(stocks[i, 2]);       }       return 0;    }    [WebMethod(Description =                  "Returns the stock name for the input stock symbol.")]    public string GetName(string StockSymbol)    //  Given a stock symbol, return the name.    {       //  Iterate through the array, looking for the symbol.       for (int i = 0; i < stocks.GetLength(0); i++)       {          //  Do a case-insensitive string compare.          if (String.Compare(StockSymbol, stocks[i, 0], true) == 0)             return stocks[i, 1];       }       return "Symbol not found.";    }    [WebMethod]    public void SetStockExchange(string Exchange)    {       Application["exchange"] = Exchange;    }    [WebMethod]    public string GetStockExchange(  )    {       return Application["exchange"].ToString(  );    }    [WebMethod(Description = "Number of hits per session.",               EnableSession = true)]    public int HitCounter(  )    {       if (Session["HitCounter"] == null)       {          Session["HitCounter"] = 1;       }       else       {          Session["HitCounter"] = ((int)Session["HitCounter"]) + 1;       }       return ((int)Session["HitCounter"]);    }    [WebMethod(Description = "Returns the value of the users holdings " +                  "in a specified stock symbol.",               MessageName = "GetValuePortfolio")]    public double GetValue(string StockSymbol)    {       /* Put code here to get the username of the current user, fetch both          the current price of the specified StockSymbol and number of          shares held by the current user, multiply the two together, and          return the result.       */       return 0;    }    [SoapDocumentMethod(Binding = "OverloadedGetValue")]    [WebMethod(Description = "Returns the value of a specified " +                      "number of shares in a specified stock symbol.",               MessageName = "GetValueStock")]    public double GetValue(string StockSymbol, int NumShares)    {       /*  Put code here to get the current price of the specified          StockSymbol, multiply it times NumShares, and return the result.       */       return 0;    }    [WebMethod(Description = "Sets the value of the users holdings " +               "in a specified stock symbol.",               TransactionOption=TransactionOption.RequiresNew)]    public double SetValue(string StockSymbol)    {       /*  Put code here to set the value of the specified          StockSymbol.          This method is starts a transaction.       */       return 0;    }    [WebMethod(Description = "Returns all the stock symbols whose firm " +                   "matches the input string as *str*.")]    public ArrayList GetList(string MatchString)    {       ArrayList a = new ArrayList(  );       //  Iterate through the array, looking for matching firm names.       for (int i = 0; i < stocks.GetLength(0); i++)       {          //  Search is case sensitive.          if (stocks[i, 1].ToUpper(  ).IndexOf(MatchString.ToUpper(  )) >= 0)             a.Add(stocks[i, 1]);       }       a.Sort(  );       return a;    }    [WebMethod(Description="Returns a data set from the Northwind db.")]    public DataSet GetDataset(  )    {       // connect to the Northwind database       string connectionString =            "server=MyServer; uid=sa;pwd=secret;database= Northwind";       // get records from the Customers table       string commandString =            "Select CompanyName,ContactName, City, Country from Customers";       // create the data set command object and the DataSet       SqlDataAdapter dataAdapter = new SqlDataAdapter(commandString,                                                       connectionString);       DataSet dataSet = new DataSet(  );       // fill the data set object       dataAdapter.Fill(dataSet,"Customers");       return dataSet;    }    [WebMethod(Description="Returns stock history for " +                "the stock symbol specified.")]    public Stock GetHistory(string StockSymbol)    {       Stock stock = new Stock(  );       //  Iterate through the array, looking for the symbol.       for (int i = 0; i < stocks.GetLength(0); i++)       {          //  Do a case-insensitive string compare.          if (String.Compare(StockSymbol, stocks[i,0], true) == 0)          {             stock.StockSymbol = StockSymbol;             stock.StockName = stocks[i,1];             stock.Price = Convert.ToDouble(stocks[i,2]);             stock.Broker = "Dewy, Cheatum, & Howe";             //  Populate the StockHistory data.             stock.History[0] = new StockHistory(  );             stock.History[0].TradeDate = Convert.ToDateTime("5/1/2005");             stock.History[0].Price = Convert.ToDouble(23.25);             stock.History[1] = new StockHistory(  );             stock.History[1].TradeDate = Convert.ToDateTime("6/1/2005");             stock.History[1].Price = Convert.ToDouble(28.75);             return stock;          }       }       stock.StockSymbol = StockSymbol;       stock.StockName = "Stock not found.";       return stock;    } }   //  close for class Service public class Stock {    public string StockSymbol;    public string StockName;    public double Price;    public StockHistory[] History =          new StockHistory[2];    private string strBroker;    public string Broker    {       get       {          return strBroker;       }       set       {          strBroker = value;       }    } } public class StockHistory {    public DateTime TradeDate;    public double Price; } 



Programming ASP. NET
Programming ASP.NET 3.5
ISBN: 0596529562
EAN: 2147483647
Year: 2003
Pages: 173

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net