Advertisement
umasoodch

C# Main Class

Apr 7th, 2015
186
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.40 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Web.UI;
  6. using System.Web.UI.WebControls;
  7. using Quotes;
  8. using System.Net;
  9. using System.Text;
  10. using System.Threading.Tasks;
  11.  
  12. namespace WebApplication1
  13. {
  14. public partial class WebForm1 : System.Web.UI.Page
  15. {
  16. protected void Page_Load(object sender, EventArgs e)
  17. {
  18.  
  19. }
  20.  
  21. protected void Button1_Click(object sender, EventArgs e)
  22. {
  23. //change stockName value to get quotes for different comapny
  24. string stockName = "brcm";
  25.  
  26. StockPrice sp = FetchStockQuotes(stockName);
  27. Console.WriteLine("Stocks for " + sp.Name + " : " + sp.TradeRate + " at time " + sp.TradeDate);
  28.  
  29. string a = sp.Name;
  30. string b = sp.TradeRate.ToString();
  31. string c = sp.TradeDate;
  32.  
  33. TextBox1.Text = a;
  34. TextBox2.Text = b;
  35. TextBox3.Text = c;
  36.  
  37.  
  38.  
  39.  
  40. }
  41. private static List<StockPrice> ParseStockPrices(string csvData)
  42. {
  43. List<StockPrice> prices = new List<StockPrice>();
  44. if (csvData != null && csvData != "")
  45. {
  46. try
  47. {
  48. string[] rows = csvData.Replace("\r", "").Split('\n');
  49. foreach (string row in rows)
  50. {
  51. if (string.IsNullOrEmpty(row)) continue;
  52.  
  53. string[] cols = row.Split(',');
  54.  
  55. StockPrice p = new StockPrice();
  56. p.Symbol = cols[0].Replace("\"", "");
  57. p.Name = cols[1].Replace("\"", ""); ;
  58. p.TradeRate = Convert.ToDecimal(cols[2].Replace("\"", ""));
  59. p.TradeDate = cols[3].ToString().Replace("\"", "") + " " + cols[4].ToString().Replace("\"", "").ToUpper() + " EST";
  60. p.Change = Convert.ToDecimal(cols[5]);
  61. p.ChangePercent = Convert.ToString(cols[6]).Replace("\"", "");
  62. p.ChangePercent = p.ChangePercent.Replace(" - ", "*");
  63. string[] temp = p.ChangePercent.Split('*');
  64. p.ChangePercent = temp[1].Replace("\"", "");
  65. prices.Add(p);
  66. }
  67. }
  68. catch (Exception ex)
  69. {
  70.  
  71. }
  72. }
  73. return prices;
  74. }
  75.  
  76. private static string GetStockMarketUrl()
  77. {
  78. return "http://finance.yahoo.com/d/quotes.csv?f=snl1d1t1c1c&s=";
  79. //return StockMarketURL;
  80. }
  81.  
  82. public static StockPrice FetchStockQuotes(string symbol)
  83. {
  84. string url = GetStockMarketUrl() + symbol;
  85. string csvData = string.Empty;
  86. List<StockPrice> prices = new List<StockPrice>();
  87. using (WebClient web = new WebClient())
  88. {
  89. try
  90. {
  91. csvData = web.DownloadString(url);
  92. if (csvData != "")
  93. {
  94. prices = ParseStockPrices(csvData);
  95. }
  96. }
  97. catch (Exception ex)
  98. {
  99.  
  100. }
  101.  
  102. }
  103. return prices[0];
  104. }
  105.  
  106.  
  107. }
  108. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement