<%@ Page Inherits="System.Web.UI.MobileControls.MobilePage" Language="C#" Debug="true" %>
<%@ Register TagPrefix="mobile" Namespace="System.Web.UI.MobileControls" Assembly="System.Web.Mobile" %>
<%@ Import Namespace="System" %>
<%@ Import Namespace="System.Data"%>
<%@ Import Namespace="System.Data.OleDb" %>
<%@ Import Namespace="System.Net" %>
<%@ Import Namespace="System.Text" %>
<%@ Import Namespace="System.IO" %>

<script runat="server" language="C#">

public String str;
public string strConn ="PROVIDER=Microsoft.Jet.OLEDB.4.0;DATA SOURCE=F://inetpub//wwwroot//mobile//test.mdb ";
private char[] separator = {','} ;


DataSet ds = new DataSet();
DataView dv = new DataView();


private void Navigate(object sender, ListCommandEventArgs e)
{
switch(e.ListItem.Value)
{
case "ViewStkQuotes":
ActiveForm=FormViewStkQuotes;
break;
case "ViewStkAlerts":
ActiveForm=FormViewStkAlerts;
break;
case "Add":
ActiveForm=FormAdd;
break;
}

}

protected void AddNewStk(Object Sender, EventArgs e)
{
      String strSQL = "Insert into tblStk (stkSymbol, StkHigh, StkLow) VALUES ('" +  txtSymbol.Text + "', " +  Convert.ToDecimal(txtHigh.Text) + ", " + Convert.ToDecimal(txtLow.Text) + ");";

      OleDbConnection Conn = new OleDbConnection("PROVIDER=Microsoft.Jet.OLEDB.4.0;DATA SOURCE=F://inetpub//wwwroot//mobile//test.mdb;");
      Conn.Open();
      OleDbDataReader oReader;
      OleDbCommand cmd = new OleDbCommand(strSQL,Conn);
 
      cmd.ExecuteNonQuery();
      Conn.Close();

     ActiveForm=FormAdded;
  }

private void FillQuotes()
{
OleDbConnection Conn = new OleDbConnection(strConn);
OleDbCommand cmd = new OleDbCommand("select StkSymbol from tblStk",Conn);
OleDbDataAdapter oDataAdapter = new OleDbDataAdapter(cmd);
Conn.Open();
ds = new DataSet();

oDataAdapter.Fill(ds,"tblStk");
Conn.Close();

HttpWebRequest req;
HttpWebResponse res;
StreamReader sr;
string strResult;
string[] temp;
string strcurindex;
string fullpath ;
//get stock quote for each row

   DataColumn PriceColumn = new DataColumn(); 
   PriceColumn.DataType = System.Type.GetType("System.Decimal"); 
   PriceColumn.AllowDBNull = true; 
   PriceColumn.Caption = "Price"; 
   PriceColumn.ColumnName = "StkPrice"; 
   PriceColumn.DefaultValue = 0; 
   // Add the column to the table. 
   ds.Tables["tblStk"].Columns.Add(PriceColumn); 

foreach(DataRow myRow in ds.Tables["tblStk"].Rows)
{
 fullpath = @"http://quote.yahoo.com/d/quotes.csv?s="+ myRow[0] +"&f=sl1d1t1c1ohgvj1pp2owern&e=.csv";
            try
            {
	    WebProxy proxyObject = new WebProxy("proxy.edu.cn", 3128);
            proxyObject.BypassProxyOnLocal = true;
            GlobalProxySelection.Select = proxyObject;
            req = (HttpWebRequest) WebRequest.Create(fullpath);
            res = (HttpWebResponse) req.GetResponse();     
            sr = new StreamReader(res.GetResponseStream(), Encoding.ASCII);
            strResult = sr.ReadLine();
            sr.Close();

	    temp = strResult.Split(separator) ;
            if(temp.Length >1)
            {
            //only the relevant portion .
            strcurindex = temp[1] ; 
	    myRow[1] = Convert.ToDecimal(strcurindex);
            }
            }
            catch(Exception )
            {
              myRow[1] = 0;
            }
}


dv = ds.Tables[0].DefaultView;
StkQuotes.DataSource = dv;
StkQuotes.DataBind();
}

private void FormViewStkQuotes_Activate(object sender, System.EventArgs e)
{
FillQuotes();
}

protected void QuotesRefresh(Object Sender, EventArgs e)
{
FillQuotes();
 }

private void FillAlerts()
{
OleDbConnection Conn = new OleDbConnection(strConn);
OleDbCommand cmd = new OleDbCommand("select StkSymbol, StkHigh, StkLow from tblStk",Conn);
OleDbDataAdapter oDataAdapter = new OleDbDataAdapter(cmd);
Conn.Open();
ds = new DataSet();
oDataAdapter.Fill(ds,"tblStk");
Conn.Close();

HttpWebRequest req;
HttpWebResponse res;
StreamReader sr;
string strResult;
string[] temp;
string strcurindex;
string fullpath ;
//get stock quote for each row

   DataColumn PriceColumn = new DataColumn(); 
   PriceColumn.DataType = System.Type.GetType("System.Decimal"); 
   PriceColumn.AllowDBNull = true; 
   PriceColumn.Caption = "Price"; 
   PriceColumn.ColumnName = "StkPrice"; 
   PriceColumn.DefaultValue = 0; 
   // Add the column to the table. 
   ds.Tables["tblStk"].Columns.Add(PriceColumn); 

 foreach(DataRow myRow in ds.Tables["tblStk"].Rows)
{
 fullpath = @"http://quote.yahoo.com/d/quotes.csv?s="+ myRow[0] +"&f=sl1d1t1c1ohgvj1pp2owern&e=.csv";
            try
            {
	    WebProxy proxyObject = new WebProxy("proxy.edu.cn", 3128);
            proxyObject.BypassProxyOnLocal = true;
            GlobalProxySelection.Select = proxyObject;
            req = (HttpWebRequest) WebRequest.Create(fullpath);
            res = (HttpWebResponse) req.GetResponse();     
            sr = new StreamReader(res.GetResponseStream(), Encoding.ASCII);
            strResult = sr.ReadLine();
            sr.Close();

	    temp = strResult.Split(separator) ;
            if(temp.Length >1)
            {
            //only the relevant portion .
            strcurindex = temp[1] ; 
	    myRow[3] = Convert.ToDecimal(strcurindex);
            }
            }
            catch(Exception )
            {
              myRow[3] = 0;
            }
}


dv = ds.Tables[0].DefaultView;
dv.RowFilter = "StkPrice>StkHigh or StkPrice <StkLow";
StkAlerts.DataSource = dv;
StkAlerts.DataBind();
}

private void FormViewStkAlerts_Activate(object sender, System.EventArgs e)
{
FillAlerts();
}



protected void AlertsRefresh(Object Sender, EventArgs e)
{
FillAlerts();
 }
</script>

<mobile:Form id = "Form1" runat="server">

<mobile:Label runat="server">Stock Quotes</mobile:Label>
<mobile:List id="List1" runat="server" OnItemCommand="Navigate">
<Item Value="ViewStkQuotes" Text="View Stock Quotes"></Item>
<Item Value="ViewStkAlerts" Text="View Stock Alerts" ></Item>
<Item Value="Add" Text="Add Stocks" ></Item>
</mobile:List>

</mobile:Form>

<mobile:Form runat="server" id="FormViewStkQuotes" OnActivate="FormViewStkQuotes_Activate">
<mobile:Label id ="lblAllStkQuotes" runat="server" >Your Stock Quotes</mobile:Label>
<mobile:ObjectList id="StkQuotes" runat="server" TableFields="StkSymbol;StkPrice"  >
</mobile:ObjectList>

<mobile:Command runat="server" id="btnRefreshQuotes" OnClick="QuotesRefresh">
Refresh
</mobile:Command>

<mobile:Link runat="server" NavigateURL="#Form1">Back to Main Menu</mobile:Link>
</mobile:Form>

<mobile:Form runat="server" id="FormViewStkAlerts" OnActivate="FormViewStkAlerts_Activate">
<mobile:Label id ="lblAllStkAlerts" runat="server" >Your Stock Alerts</mobile:Label>
<mobile:ObjectList id="StkAlerts" runat="server" TableFields="StkSymbol;StkPrice">
</mobile:ObjectList>

<mobile:Command runat="server" id="btnRefreshAlerts" OnClick="AlertsRefresh">Refresh</mobile:Command>
<mobile:Link runat="server" NavigateURL="#Form1">Back to Main Menu</mobile:Link>

</mobile:Form>

<mobile:Form runat="server" id="FormAdd">
<mobile:Label id ="lbl1" runat="server" >Enter a new Stock to track</mobile:Label><BR/>
<mobile:Label id ="lblSymbol" runat="server" >Symbol:</mobile:Label>
<mobile:TextBox id="txtSymbol" runat="server"></mobile:TextBox>
<mobile:Label id ="lblHigh" runat="server" >High Threshold</mobile:Label><BR/>
<mobile:TextBox id="txtHigh" runat="server"></mobile:TextBox>
<mobile:Label id ="lblLow" runat="server" >Low Threshold</mobile:Label><BR/>
<mobile:TextBox id="txtLow" runat="server"></mobile:TextBox>

<mobile:Command runat="server" id="Button" OnClick="AddNewStk">
OK
</mobile:Command>
<mobile:Link runat="server" NavigateURL="#Form1">Back to Main Menu</mobile:Link>
</mobile:Form>

<mobile:Form runat="server" id="FormAdded" >
<mobile:Label id ="lbl2" runat="server" >Your new stock has been saved for future tracking</mobile:Label><BR/>
<mobile:Link runat="server" NavigateURL="#Form1">Back to Main Menu</mobile:Link>
</mobile:Form>


