Advertisement
umasoodch

online retrieval code

Apr 9th, 2015
192
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.11 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. using System.IO;
  12.  
  13. namespace WebApplication1
  14. {
  15. public partial class WebForm1 : System.Web.UI.Page
  16. {
  17. protected void Page_Load(object sender, EventArgs e)
  18. {
  19.  
  20. }
  21.  
  22. protected void Button1_Click1(object sender, EventArgs e)
  23. {
  24.  
  25. string stockName = "brcm"; //stock name
  26.  
  27. StockPrice sp = FetchStockQuotes(stockName); //calling method
  28. string a = sp.Name;
  29. string b = sp.TradeRate.ToString();
  30. string c = sp.TradeDate;
  31.  
  32. TextBox1.Text = a;
  33. TextBox2.Text = b;
  34. TextBox3.Text = c;
  35.  
  36.  
  37.  
  38. string stockName1 = "aapl"; //stock name
  39.  
  40. StockPrice sp1 = FetchStockQuotes(stockName1); //calling method
  41. string a2 = sp1.Name;
  42. string b2 = sp1.TradeRate.ToString();
  43. string c2 = sp1.TradeDate;
  44.  
  45. TextBox4.Text = a2;
  46. TextBox5.Text = b2;
  47. TextBox6.Text = c2;
  48.  
  49.  
  50. string stockName2 = "rby"; //stock name
  51.  
  52. StockPrice sp2 = FetchStockQuotes(stockName2); //calling method
  53. string a3 = sp2.Name;
  54. string b3 = sp2.TradeRate.ToString();
  55. string c3 = sp2.TradeDate;
  56.  
  57. TextBox7.Text = a3;
  58. TextBox8.Text = b3;
  59. TextBox9.Text = c3;
  60.  
  61. string stockName3 = "msft"; //stock name
  62.  
  63. StockPrice sp3 = FetchStockQuotes(stockName3); //calling method
  64. string a4 = sp3.Name;
  65. string b4 = sp3.TradeRate.ToString();
  66. string c4 = sp3.TradeDate;
  67.  
  68. TextBox10.Text = a4;
  69. TextBox11.Text = b4;
  70. TextBox12.Text = c4;
  71.  
  72.  
  73. string stockName4 = "goog"; //stock name
  74.  
  75. StockPrice sp4 = FetchStockQuotes(stockName4); //calling method
  76. string a5 = sp4.Name;
  77. string b5 = sp4.TradeRate.ToString();
  78. string c5 = sp4.TradeDate;
  79.  
  80. TextBox13.Text = a5;
  81. TextBox14.Text = b5;
  82. TextBox15.Text = c5;
  83.  
  84.  
  85. string stockName5 = "fdx"; //stock name
  86.  
  87. StockPrice sp5 = FetchStockQuotes(stockName5); //calling method
  88. string a6 = sp5.Name;
  89. string b6 = sp5.TradeRate.ToString();
  90. string c6 = sp5.TradeDate;
  91.  
  92. TextBox16.Text = a6;
  93. TextBox17.Text = b6;
  94. TextBox18.Text = c6;
  95.  
  96. string stockName6 = "f"; //stock name
  97.  
  98. StockPrice sp6 = FetchStockQuotes(stockName6); //calling method
  99. string a7 = sp6.Name;
  100. string b7 = sp6.TradeRate.ToString();
  101. string c7 = sp6.TradeDate;
  102.  
  103. TextBox19.Text = a7;
  104. TextBox20.Text = b7;
  105. TextBox21.Text = c7;
  106.  
  107.  
  108. string stockName7 = "bac"; //stock name
  109.  
  110. StockPrice sp7 = FetchStockQuotes(stockName7); //calling method
  111. string a8 = sp7.Name;
  112. string b8 = sp7.TradeRate.ToString();
  113. string c8 = sp7.TradeDate;
  114.  
  115. TextBox22.Text = a8;
  116. TextBox23.Text = b8;
  117. TextBox24.Text = c8;
  118.  
  119. string stockName8 = "usb"; //stock name
  120.  
  121. StockPrice sp8 = FetchStockQuotes(stockName8); //calling method
  122. string a9 = sp8.Name;
  123. string b9 = sp8.TradeRate.ToString();
  124. string c9 = sp8.TradeDate;
  125.  
  126. TextBox25.Text = a9;
  127. TextBox26.Text = b9;
  128. TextBox27.Text = c9;
  129.  
  130.  
  131.  
  132.  
  133. }
  134.  
  135.  
  136.  
  137.  
  138.  
  139.  
  140.  
  141.  
  142.  
  143. private static List<StockPrice> ParseStockPrices(string csvData)
  144. {
  145. List<StockPrice> prices = new List<StockPrice>();
  146. if (csvData != null && csvData != "")
  147. {
  148. try
  149. {
  150. string[] rows = csvData.Replace("\r", "").Split('\n');
  151. foreach (string row in rows)
  152. {
  153. if (string.IsNullOrEmpty(row)) continue;
  154.  
  155. string[] cols = row.Split(',');
  156.  
  157. StockPrice p = new StockPrice();
  158. p.Symbol = cols[0].Replace("\"", "");
  159. p.Name = cols[1].Replace("\"", ""); ;
  160. p.TradeRate = Convert.ToDecimal(cols[2].Replace("\"", ""));
  161. p.TradeDate = cols[3].ToString().Replace("\"", "") + " " + cols[4].ToString().Replace("\"", "").ToUpper() + " EST";
  162. p.Change = Convert.ToDecimal(cols[5]);
  163. p.ChangePercent = Convert.ToString(cols[6]).Replace("\"", "");
  164. p.ChangePercent = p.ChangePercent.Replace(" - ", "*");
  165. string[] temp = p.ChangePercent.Split('*');
  166. p.ChangePercent = temp[1].Replace("\"", "");
  167. prices.Add(p);
  168. }
  169. }
  170. catch (Exception ex)
  171. {
  172.  
  173. }
  174. }
  175. return prices;
  176. }
  177.  
  178. private static string GetStockMarketUrl()
  179. {
  180. return "http://finance.yahoo.com/d/quotes.csv?f=snl1d1t1c1c&s=";
  181. //return StockMarketURL;
  182. }
  183.  
  184. public static StockPrice FetchStockQuotes(string symbol)
  185. {
  186. string url = GetStockMarketUrl() + symbol;
  187. string csvData = string.Empty;
  188. List<StockPrice> prices = new List<StockPrice>();
  189. using (WebClient web = new WebClient())
  190. {
  191. try
  192. {
  193. csvData = web.DownloadString(url);
  194. if (csvData != "")
  195. {
  196. prices = ParseStockPrices(csvData);
  197. }
  198. }
  199. catch (Exception ex)
  200. {
  201.  
  202. }
  203.  
  204. }
  205. return prices[0];
  206. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement