Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.UI;
- using System.Web.UI.WebControls;
- using Quotes;
- using System.Net;
- using System.Text;
- using System.Threading.Tasks;
- using System.IO;
- namespace WebApplication1
- {
- public partial class WebForm1 : System.Web.UI.Page
- {
- protected void Page_Load(object sender, EventArgs e)
- {
- }
- protected void Button1_Click1(object sender, EventArgs e)
- {
- string stockName = "brcm"; //stock name
- StockPrice sp = FetchStockQuotes(stockName); //calling method
- string a = sp.Name;
- string b = sp.TradeRate.ToString();
- string c = sp.TradeDate;
- TextBox1.Text = a;
- TextBox2.Text = b;
- TextBox3.Text = c;
- string stockName1 = "aapl"; //stock name
- StockPrice sp1 = FetchStockQuotes(stockName1); //calling method
- string a2 = sp1.Name;
- string b2 = sp1.TradeRate.ToString();
- string c2 = sp1.TradeDate;
- TextBox4.Text = a2;
- TextBox5.Text = b2;
- TextBox6.Text = c2;
- string stockName2 = "rby"; //stock name
- StockPrice sp2 = FetchStockQuotes(stockName2); //calling method
- string a3 = sp2.Name;
- string b3 = sp2.TradeRate.ToString();
- string c3 = sp2.TradeDate;
- TextBox7.Text = a3;
- TextBox8.Text = b3;
- TextBox9.Text = c3;
- string stockName3 = "msft"; //stock name
- StockPrice sp3 = FetchStockQuotes(stockName3); //calling method
- string a4 = sp3.Name;
- string b4 = sp3.TradeRate.ToString();
- string c4 = sp3.TradeDate;
- TextBox10.Text = a4;
- TextBox11.Text = b4;
- TextBox12.Text = c4;
- string stockName4 = "goog"; //stock name
- StockPrice sp4 = FetchStockQuotes(stockName4); //calling method
- string a5 = sp4.Name;
- string b5 = sp4.TradeRate.ToString();
- string c5 = sp4.TradeDate;
- TextBox13.Text = a5;
- TextBox14.Text = b5;
- TextBox15.Text = c5;
- string stockName5 = "fdx"; //stock name
- StockPrice sp5 = FetchStockQuotes(stockName5); //calling method
- string a6 = sp5.Name;
- string b6 = sp5.TradeRate.ToString();
- string c6 = sp5.TradeDate;
- TextBox16.Text = a6;
- TextBox17.Text = b6;
- TextBox18.Text = c6;
- string stockName6 = "f"; //stock name
- StockPrice sp6 = FetchStockQuotes(stockName6); //calling method
- string a7 = sp6.Name;
- string b7 = sp6.TradeRate.ToString();
- string c7 = sp6.TradeDate;
- TextBox19.Text = a7;
- TextBox20.Text = b7;
- TextBox21.Text = c7;
- string stockName7 = "bac"; //stock name
- StockPrice sp7 = FetchStockQuotes(stockName7); //calling method
- string a8 = sp7.Name;
- string b8 = sp7.TradeRate.ToString();
- string c8 = sp7.TradeDate;
- TextBox22.Text = a8;
- TextBox23.Text = b8;
- TextBox24.Text = c8;
- string stockName8 = "usb"; //stock name
- StockPrice sp8 = FetchStockQuotes(stockName8); //calling method
- string a9 = sp8.Name;
- string b9 = sp8.TradeRate.ToString();
- string c9 = sp8.TradeDate;
- TextBox25.Text = a9;
- TextBox26.Text = b9;
- TextBox27.Text = c9;
- }
- private static List<StockPrice> ParseStockPrices(string csvData)
- {
- List<StockPrice> prices = new List<StockPrice>();
- if (csvData != null && csvData != "")
- {
- try
- {
- string[] rows = csvData.Replace("\r", "").Split('\n');
- foreach (string row in rows)
- {
- if (string.IsNullOrEmpty(row)) continue;
- string[] cols = row.Split(',');
- StockPrice p = new StockPrice();
- p.Symbol = cols[0].Replace("\"", "");
- p.Name = cols[1].Replace("\"", ""); ;
- p.TradeRate = Convert.ToDecimal(cols[2].Replace("\"", ""));
- p.TradeDate = cols[3].ToString().Replace("\"", "") + " " + cols[4].ToString().Replace("\"", "").ToUpper() + " EST";
- p.Change = Convert.ToDecimal(cols[5]);
- p.ChangePercent = Convert.ToString(cols[6]).Replace("\"", "");
- p.ChangePercent = p.ChangePercent.Replace(" - ", "*");
- string[] temp = p.ChangePercent.Split('*');
- p.ChangePercent = temp[1].Replace("\"", "");
- prices.Add(p);
- }
- }
- catch (Exception ex)
- {
- }
- }
- return prices;
- }
- private static string GetStockMarketUrl()
- {
- return "http://finance.yahoo.com/d/quotes.csv?f=snl1d1t1c1c&s=";
- //return StockMarketURL;
- }
- public static StockPrice FetchStockQuotes(string symbol)
- {
- string url = GetStockMarketUrl() + symbol;
- string csvData = string.Empty;
- List<StockPrice> prices = new List<StockPrice>();
- using (WebClient web = new WebClient())
- {
- try
- {
- csvData = web.DownloadString(url);
- if (csvData != "")
- {
- prices = ParseStockPrices(csvData);
- }
- }
- catch (Exception ex)
- {
- }
- }
- return prices[0];
- }
- protected void Button2_Click(object sender, EventArgs e)
- {
- string stockName = "brcm"; //stock name
- StockPrice sp = FetchStockQuotes(stockName); //calling method
- string a = sp.Name;
- string b = sp.TradeRate.ToString();
- string c = sp.TradeDate;
- TextBox1.Text = a;
- TextBox2.Text = b;
- TextBox3.Text = c;
- string stockName1 = "aapl"; //stock name
- StockPrice sp1 = FetchStockQuotes(stockName1); //calling method
- string a2 = sp1.Name;
- string b2 = sp1.TradeRate.ToString();
- string c2 = sp1.TradeDate;
- TextBox4.Text = a2;
- TextBox5.Text = b2;
- TextBox6.Text = c2;
- string stockName2 = "rby"; //stock name
- StockPrice sp2 = FetchStockQuotes(stockName2); //calling method
- string a3 = sp2.Name;
- string b3 = sp2.TradeRate.ToString();
- string c3 = sp2.TradeDate;
- TextBox7.Text = a3;
- TextBox8.Text = b3;
- TextBox9.Text = c3;
- string stockName3 = "msft"; //stock name
- StockPrice sp3 = FetchStockQuotes(stockName3); //calling method
- string a4 = sp3.Name;
- string b4 = sp3.TradeRate.ToString();
- string c4 = sp3.TradeDate;
- TextBox10.Text = a4;
- TextBox11.Text = b4;
- TextBox12.Text = c4;
- string stockName4 = "goog"; //stock name
- StockPrice sp4 = FetchStockQuotes(stockName4); //calling method
- string a5 = sp4.Name;
- string b5 = sp4.TradeRate.ToString();
- string c5 = sp4.TradeDate;
- TextBox13.Text = a5;
- TextBox14.Text = b5;
- TextBox15.Text = c5;
- string stockName5 = "fdx"; //stock name
- StockPrice sp5 = FetchStockQuotes(stockName5); //calling method
- string a6 = sp5.Name;
- string b6 = sp5.TradeRate.ToString();
- string c6 = sp5.TradeDate;
- TextBox16.Text = a6;
- TextBox17.Text = b6;
- TextBox18.Text = c6;
- string stockName6 = "f"; //stock name
- StockPrice sp6 = FetchStockQuotes(stockName6); //calling method
- string a7 = sp6.Name;
- string b7 = sp6.TradeRate.ToString();
- string c7 = sp6.TradeDate;
- TextBox19.Text = a7;
- TextBox20.Text = b7;
- TextBox21.Text = c7;
- string stockName7 = "bac"; //stock name
- StockPrice sp7 = FetchStockQuotes(stockName7); //calling method
- string a8 = sp7.Name;
- string b8 = sp7.TradeRate.ToString();
- string c8 = sp7.TradeDate;
- TextBox22.Text = a8;
- TextBox23.Text = b8;
- TextBox24.Text = c8;
- string stockName8 = "usb"; //stock name
- StockPrice sp8 = FetchStockQuotes(stockName8); //calling method
- string a9 = sp8.Name;
- string b9 = sp8.TradeRate.ToString();
- string c9 = sp8.TradeDate;
- TextBox25.Text = a9;
- TextBox26.Text = b9;
- TextBox27.Text = c9;
- string[] mystring = { a, b, c, a2, b2, c2, a3, b3, c3, a4, b4, c4, a5, b5, c5, a6, b6, c6, a7, b7, c7,
- a8,b8,c8,a9,b9,c9};
- using (StreamWriter sw = new StreamWriter("C:\\Users\\admin\\My Documents\\test1.txt"))
- {
- foreach (string s in mystring)
- {
- sw.WriteLine(s);
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement