Blue Theme Orange Theme Green Theme Red Theme
 
Home | Forums | Videos | Photos | Blogs | Beginners
 | Consulting  
Submit an Article Submit a Blog 
 Jump to
Skip Navigation Links
TechnologyExpand Technology
WebsiteExpand Website
Nevron Chart
 Resources  
Close
 Our Network  
Close
Search :       Advanced Search »
Home » VB.NET » Mobile Stock Quote Tracker

Mobile Stock Quote Tracker


We will create a mobile stock quote tracker program in this article. The program retrieves the real time quotes of symbols specified by the user and displays the results in a user friendly format. The user can also specify the high and low thresholds for setting alerts. The View Stock Alerts screen displays the stocks that have crossed above the high threshold and stocks that have values below the low threshold.

Author Rank:
Total page views :  11942
Total downloads :  197
   Print Read/Post comments Post a comment  Similar Articles  
   Email to a friend  Bookmark  Author's other articles  
Download Files:
MobStkAspxDPL.zip
 
Become a Sponsor

We will create a mobile stock quote tracker program in this article. The program retrieves the real time quotes of  symbols specified by the user and displays the results in a user friendly format. The user can also specify the high and low thresholds for setting alerts. The View Stock Alerts screen displays the stocks that have crossed above the high threshold and stocks that have values below the low threshold. 

The stock symbols selected by the user are stored in a simple MS Access database containing a single table "tblStk".

Here is the data structure of tblStk.

Field Name Description Data Type
StkSymbol Stock Symbol. Text
StkHigh High Threshold for alerts. Number(decimal)
StkLow Low Threshold for alerts. Number(decimal)

Open up your favorite html editor and let's get started with the coding. Save the file as mobstk.aspx.

Rather than going sequentially through the program, we will discuss the example in logical steps. You may prefer to download the complete code listing for the article and run through it once, read this overview and then try it out.

The Home Page:

This page provides a simple menu of choices for the user to navigate through the program. 

<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> 

Code Snippet 1: Home Page UI.

The above listing creates the user interface for a mobile web form containing a List control with 3 options.

  • View Stock Quotes.
  • View Stock Alerts.
  •  Add Stocks.

Figure 1 : Home Page.

View Stock Quotes.

Selection of the first option "View Stock Quotes" from the Home page results in the activation of the View Stock Quotes Form.

Below is the code for creating the user interface for the View Stock Quotes 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> 

Code Snippet 2: "View Stock Quotes" UI.

This form contains a list of the stock symbols selected by the user. On clicking the stock symbols, the user is presented with the stock quotes for the symbol. 

The function FillStocks is invoked when the View Stock Quotes is activated and also when the Refresh button is clicked. 

In the function, we get the list of stock symbols specified by the user from the Access database in a dataset. Next we add a column StkPrice to the dataset as a place holder for the stock quotes. The function iterates through each stock symbol and retrieves the stock quote from the web site and fills the value in the StkPrice column of the row. Once the data is ready, we bind the ObjectList control to the data in the dataset and display the results to the user. 

The ObjectList control provides special formatting options in Mobile Forms. This control is strictly databound. 

Private Sub FillQuotes()
Dim Conn As New OleDbConnection(strConn)
Dim cmd As New OleDbCommand("select StkSymbol from tblStk", Conn)
Dim oDataAdapter As New OleDbDataAdapter(cmd)
Conn.Open()
ds =
New DataSet
oDataAdapter.Fill(ds, "tblStk")
Conn.Close()
Dim req As HttpWebRequest
Dim res As HttpWebResponse
Dim sr As StreamReader
Dim strResult As String
Dim temp() As String
Dim strcurindex As String
Dim fullpath As String
Dim PriceColumn As 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)
'Get stock quotes for each stock symbol
Dim myRow As DataRow
For Each myRow In ds.Tables("tblStk").Rows
fullpath = "http://quote.yahoo.com/d/quotes.csv?s=" + myRow(0) + "&f=sl1d1t1c1ohgvj1pp2owern&e=.csv"
Try
req = CType(WebRequest.Create(fullpath), HttpWebRequest)
res =
CType(req.GetResponse(), HttpWebResponse)
sr =
New StreamReader(res.GetResponseStream(), Encoding.ASCII)
strResult = sr.ReadLine()
sr.Close()
temp = strResult.Split(separator)
If temp.Length > 1 Then
'only the relevant portion .
strcurindex = temp(1)
myRow(1) = Convert.ToDecimal(strcurindex)
End If
Catch
End Try
Next myRow
dv = ds.Tables(0).DefaultView
StkQuotes.DataSource = dv
StkQuotes.DataBind()
End Sub 'FillQuotes

Code Snippet 3: Functionality to retrieve and display stock quotes. 

Figure 2: View Stock Quotes Page.

Figure 3: Details of  Stock quote.

View Stock Alerts.

Displaying stock alerts uses the same logic as that for stock quotes with a small addition. The form ViewStkAlerts generates the front end UI for the page. The function FillAlerts  is called on Form activation of the ViewStkAlerts Form or when the Refresh button on the form is clicked.  

When a stock in the list is clicked, the details of the alert are displayed – the high threshold, low threshold and the current stock price.

dv.RowFilter = "StkPrice>StkHigh or StkPrice <StkLow"

Code Snippet 4: "View Stock Alerts" Functionality.

This line of code is used to filter only the stocks which have crossed the user specified threshold values. 

Figure 4: Stock Alerts.

Figure 5: Details of stock alerts.

Add New Stock. 

Listed below is the code for generating the user interface for adding new stock symbols for future tracking.

<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>
 

Code Snippet 5: "Add New Stock" UI.

The code to save the new stock symbol simply executes an insert against the Access database and the user is informed that the Save was successful.

Protected Sub AddNewStk(ByVal Sender As [Object], ByVal e As EventArgs)
Dim strSQL As [String] = "Insert into tblStk (stkSymbol, StkHigh, StkLow) VALUES ('" + txtSymbol.Text + "', " + Convert.ToDecimal(txtHigh.Text) + ", " + Convert.ToDecimal(txtLow.Text) + ");"
Dim Conn As New OleDbConnection("PROVIDER=Microsoft.Jet.OLEDB.4.0;DATA c:\inetpub\wwwroot\dotnet\test.mdb;")
Conn.Open()
Dim cmd As New OleDbCommand(strSQL, Conn)
cmd.ExecuteNonQuery()
Conn.Close()
ActiveForm = FormAdded
End Sub 'AddNewStk.

Code Snippet 6 : Add new Stock Functionality.

Figure 6: Add new stock.

Figure 7 : User confirmation for Add New Stock.

Figure 8: The newly added stock symbol "HWP" is also added in the stock list. 

Note that exception handling, user data entry validation have not been included in this example for the sake of simplicity. 

Conclusion.

This example demonstrates some of the different controls used in Mobile .Net Web Forms and the ease with which the extensive capabilities of the .Net Framework components can be applied for mobile programming.


Login to add your contents and source code to this article
 About the author
 
Dipal Choksi
Dipal Choksi has over 10 years of industry experience in team-effort projects and also as an individual contributor. She has been working on the .Net platform since the beta releases of .Net 1.0.
Looking for C# Consulting?
C# Consulting is founded in 2002 by the founders of C# Corner. Unlike a traditional consulting company, our consultants are well-known experts in .NET and many of them are MVPs, authors, and trainers. We specialize in Microsoft .NET development and utilize Agile Development and Extreme Programming practices to provide fast pace quick turnaround results. Our software development model is a mix of Agile Development, traditional SDLC, and Waterfall models.
Click here to learn more about C# Consulting.
 
Introducing MaxV - one click. infinite control. Hyper-V Hosting from MaximumASP.
Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Dynamic PDF
ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
SQL and .NET performance profiling in one place
Investigate SQL and .NET code side-by-side with ANTS Performance Profiler 6, so you can see which is causing the problem without switching tools.
Go.NET
Build custom interactive diagrams, network, workflow editors, flowcharts, or software design tools. Includes many predefined kinds of nodes, links, and basic shapes. Supports layers, scrolling, zooming, selection, drag-and-drop, clipboard, in-place editing, tooltips, grids, printing, overview window, palette. 100% implemented in C# as a managed .NET Control. Document/View/Tool architecture with many properties&events. Optional automatic layout.
Dundas Software
Dundas Chart for .NET is the most advanced .NET charting package available today.  With an extremely complete feature set, elegant architecture and easy implementation, Dundas Chart can quickly add advanced Charting functionality to enhance and transform ASP.NET and Windows Forms applications.  Whether you are implementing charting into internal projects, or building applications for clients, Dundas Chart offers advanced technology and advanced results to get the most out of data.
60 FREE UI Controls from DevExpress
Register for your FREE copy on over 60 free presentation controls from DevExpress - Absolutely Free-of-Charge without any royalties or distribution costs. Visit Devexpress.com/60 today. Free controls include advanced lists box, dropdown calendar, rich text edit, spin edit, tab control and so much more!

DevExpress engineers feature rich presentation controls and reporting tools for WinForms, ASP.NET, WPF, and Silverlight. Our technologies help you build your best, see complex software with greater clarity and deliver compelling business solutions for Windows and the web in the shortest possible time.
Clickatell's SMS Gateway
Clickatell's Developer Solutions allow you to SMS enable any website or application via a range of API's. Learn More about our API connections.
Free access to .NET Memory Management video
Everything you need to know about Garbage Collection, Temporary Objects, Fragmentation, Finalization and common causes of memory leaks in .NET. Watch the video here.
Microsoft Visual Studio 2010
Visualize your workspace with new multiple monitor support, powerful Web development, new SharePoint support with tons of templates and Web parts, and more accurate targeting of any version of the .NET Framework. Get set to unleash your creativity.
Nevron Chart for .NET 2010.1 Now Available
The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
Developer-Ready ASP.NET 2.0 Web Hosting with 3 MONTHS FREE
Now supporting .NET 3.0 Framework with Windows Workflow Foundation, Windows Communication Foundation (WCF), Windows Presentation Foundation (WPF), windows CardSpace (WCS)! Providing more flexibility for Developers with Web Services Support and a User/Permission Manger. Also supporting MS SQL 2005/2000 with Real-Time Backups, FREE Automated Attach .MDF Tool, FREE SQL Restore and Shrink SQL DB Tools, and SQL
Read the Top 10 Books for Microsoft Developers, 15 Days FREE
Read the Top 10 Books for Microsoft Developers, 15 Days FREE
Try Safari Books Online - 15 Days FREE + 15% Off for 1 Year
Try Safari Books Online - 15 Days FREE + 15% Off for 1 Year
 
 Post a Feedback, Comment, or Question about this article
Subject:
Comment:
Nevron Chart
Become a Sponsor
 Comments
ANTS Performance Profiler 6.0
 Hosted by MaximumASP  |  Found a broken link?  |  Contact Us  |  Terms & conditions  |  Privacy Policy  |  Site Map  |  Suggest an Idea  |  Media Kit
Current Version: 5.2010.8.14
 © 2010  contents copyright of their authors. Rest everything copyright Mindcracker. All rights reserved.