Advertisement
umasoodch

Saving code for text files

Apr 13th, 2015
213
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.83 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. }
  207.  
  208. protected void Button2_Click(object sender, EventArgs e)
  209. {
  210. string stockName = "brcm"; //stock name
  211.  
  212. StockPrice sp = FetchStockQuotes(stockName); //calling method
  213. string a = sp.Name;
  214. string b = sp.TradeRate.ToString();
  215. string c = sp.TradeDate;
  216.  
  217. TextBox1.Text = a;
  218. TextBox2.Text = b;
  219. TextBox3.Text = c;
  220.  
  221.  
  222.  
  223. string stockName1 = "aapl"; //stock name
  224.  
  225. StockPrice sp1 = FetchStockQuotes(stockName1); //calling method
  226. string a2 = sp1.Name;
  227. string b2 = sp1.TradeRate.ToString();
  228. string c2 = sp1.TradeDate;
  229.  
  230. TextBox4.Text = a2;
  231. TextBox5.Text = b2;
  232. TextBox6.Text = c2;
  233.  
  234.  
  235. string stockName2 = "rby"; //stock name
  236.  
  237. StockPrice sp2 = FetchStockQuotes(stockName2); //calling method
  238. string a3 = sp2.Name;
  239. string b3 = sp2.TradeRate.ToString();
  240. string c3 = sp2.TradeDate;
  241.  
  242. TextBox7.Text = a3;
  243. TextBox8.Text = b3;
  244. TextBox9.Text = c3;
  245.  
  246. string stockName3 = "msft"; //stock name
  247.  
  248. StockPrice sp3 = FetchStockQuotes(stockName3); //calling method
  249. string a4 = sp3.Name;
  250. string b4 = sp3.TradeRate.ToString();
  251. string c4 = sp3.TradeDate;
  252.  
  253. TextBox10.Text = a4;
  254. TextBox11.Text = b4;
  255. TextBox12.Text = c4;
  256.  
  257.  
  258. string stockName4 = "goog"; //stock name
  259.  
  260. StockPrice sp4 = FetchStockQuotes(stockName4); //calling method
  261. string a5 = sp4.Name;
  262. string b5 = sp4.TradeRate.ToString();
  263. string c5 = sp4.TradeDate;
  264.  
  265. TextBox13.Text = a5;
  266. TextBox14.Text = b5;
  267. TextBox15.Text = c5;
  268.  
  269.  
  270. string stockName5 = "fdx"; //stock name
  271.  
  272. StockPrice sp5 = FetchStockQuotes(stockName5); //calling method
  273. string a6 = sp5.Name;
  274. string b6 = sp5.TradeRate.ToString();
  275. string c6 = sp5.TradeDate;
  276.  
  277. TextBox16.Text = a6;
  278. TextBox17.Text = b6;
  279. TextBox18.Text = c6;
  280.  
  281. string stockName6 = "f"; //stock name
  282.  
  283. StockPrice sp6 = FetchStockQuotes(stockName6); //calling method
  284. string a7 = sp6.Name;
  285. string b7 = sp6.TradeRate.ToString();
  286. string c7 = sp6.TradeDate;
  287.  
  288. TextBox19.Text = a7;
  289. TextBox20.Text = b7;
  290. TextBox21.Text = c7;
  291.  
  292.  
  293. string stockName7 = "bac"; //stock name
  294.  
  295. StockPrice sp7 = FetchStockQuotes(stockName7); //calling method
  296. string a8 = sp7.Name;
  297. string b8 = sp7.TradeRate.ToString();
  298. string c8 = sp7.TradeDate;
  299.  
  300. TextBox22.Text = a8;
  301. TextBox23.Text = b8;
  302. TextBox24.Text = c8;
  303.  
  304. string stockName8 = "usb"; //stock name
  305.  
  306. StockPrice sp8 = FetchStockQuotes(stockName8); //calling method
  307. string a9 = sp8.Name;
  308. string b9 = sp8.TradeRate.ToString();
  309. string c9 = sp8.TradeDate;
  310.  
  311. TextBox25.Text = a9;
  312. TextBox26.Text = b9;
  313. TextBox27.Text = c9;
  314.  
  315.  
  316. string[] mystring = { a, b, c, a2, b2, c2, a3, b3, c3, a4, b4, c4, a5, b5, c5, a6, b6, c6, a7, b7, c7,
  317. a8,b8,c8,a9,b9,c9};
  318.  
  319. using (StreamWriter sw = new StreamWriter("C:\\Users\\admin\\My Documents\\test1.txt"))
  320. {
  321.  
  322. foreach (string s in mystring)
  323. {
  324. sw.WriteLine(s);
  325. }
  326. }
  327.  
  328.  
  329.  
  330.  
  331. }
  332.  
  333.  
  334. }
  335. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement