Click here to Skip to main content
15,896,428 members
Articles / Mobile Apps / Android

Android - Stock Market Watch (COINS) in C# using Visual Studio 2010

Rate me:
Please Sign up or sign in to vote.
4.90/5 (102 votes)
24 Mar 2013CPOL23 min read 271.8K   10.1K   289  
Describes the power of C# in building professional application - Stock Market Watch with Charts on Android platform using Mono C#.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Android.App;
using Android.Content;
using Android.Runtime;
using Android.Views;
using Android.Widget;

using Mono.Data.Sqlite;
using System.IO;
using COINSMobile.Core.BusinessLayer;

namespace COINSMobile.Core.DataLayer
{
    public class StockDatabase
    {
        private static string db_file = "stockdata.db3";
        private string stockName = string.Empty;
        private static string sMessage;

        public string StockName
        {
            get { return stockName; }
            set { stockName = value; }
        }

        private static SqliteConnection GetConnection()
        {
            var dbPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Personal), db_file);
            bool exists = File.Exists(dbPath);

            if (!exists)
                SqliteConnection.CreateFile(dbPath);

            var conn = new SqliteConnection("Data Source=" + dbPath);

            if (!exists)
                CreateDatabase(conn);

            return conn;
        }

        private static void CreateDatabase(SqliteConnection connection)
        {
            var sql = "CREATE TABLE STOCKTABLE (Id INTEGER PRIMARY KEY AUTOINCREMENT, StockName VARCHAR);";

            connection.Open();

            using (var cmd = connection.CreateCommand())
            {
                cmd.CommandText = sql;
                cmd.ExecuteNonQuery();
            }
            connection.Close();
        }

        public static IEnumerable<Stock> GetStocks()
        {
            try
            {
                var sql = "SELECT * FROM STOCKTABLE ORDER BY ID;";

                using (var conn = GetConnection())
                {
                    conn.Open();

                    using (var cmd = conn.CreateCommand())
                    {
                        cmd.CommandText = sql;

                        using (var reader = cmd.ExecuteReader())
                        {
                            while (reader.Read())
                                yield return new Stock(reader.GetInt32(0), reader.GetString(1));
                        }
                    }
                }
            }
            finally
            {
                StockManager.Message = sMessage;
            }
        }
        public static bool IsStockExists(string _stockname)
        {
            bool Ok = false;
            var sql = string.Format("SELECT * FROM STOCKTABLE WHERE STOCKNAME='{0}';", _stockname);

            try
            {
                using (var conn = GetConnection())
                {
                    conn.Open();

                    using (var cmd = conn.CreateCommand())
                    {
                        cmd.CommandText = sql;

                        using (var reader = cmd.ExecuteReader())
                        {
                            while (reader.Read())
                                Ok = true;
                        }
                    }
                }
            }
            finally
            {
                StockManager.Message = sMessage;
            }
            return Ok;
        }

        public static bool SaveStock(string _stockname)
        {
            try
            {
                bool Ok = IsStockExists(_stockname.Trim().ToUpper());
                if (Ok)
                {
                    sMessage = string.Format("Stock Script '{0}' is already added.", _stockname);
                    return false;
                }
                using (var conn = GetConnection())
                {
                    conn.Open();

                    using (var cmd = conn.CreateCommand())
                    {

                        try
                        {
                            // Do an insert
                            cmd.CommandText = "INSERT INTO STOCKTABLE (StockName) VALUES (@StockName);";
                            cmd.Parameters.AddWithValue("@StockName", _stockname.ToUpper());
                            cmd.ExecuteNonQuery();

                            sMessage = string.Format("Stock Script '{0}' is added successfully.", _stockname.ToUpper());
                            return true;
                        }
                        catch (SqliteException ex)
                        {
                            sMessage = ex.Message;
                            return false;
                        }
                    }
                }
            }
            finally
            {
                StockManager.Message = sMessage;
            }
        }

        public static bool DeleteAllStocks()
        {
            try
            {
                using (var conn = GetConnection())
                {
                    conn.Open();

                    using (var cmd = conn.CreateCommand())
                    {

                        try
                        {
                            // Do an insert
                            cmd.CommandText = "DELETE FROM STOCKTABLE;";
                            cmd.ExecuteNonQuery();

                            sMessage = "All Stocks are deleted successfully...\nTo view the stocks in Market Watch, you need to add your custom stock";
                            return true;
                        }
                        catch (SqliteException ex)
                        {
                            sMessage = ex.Message;
                            return false;
                        }
                    }
                }
            }
            finally
            {
                StockManager.Message = sMessage;
            }
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect ClarityCX Solutions & Services Pvt. Ltd.
India India
Sandip is a "Software Architect, CEO, Founder at ClarityCX Solutions & Services Private Limited"

He has 24 years of rich experience working in software development in the areas of market research, web, mobile and e-commerce.

Sandip has wide ranging experience in C#, VB.NET, ASP.Net, JQuery, JSON, LINQ, WCF, WPF, NodeJs, MongoDB, AngularJS, Express, Silverlight, SOA, Android, IPhone, IPad and Windows Mobile applications.

Specialized in Hybrid and Native Apps in iOS, Android, Windows Phone, Blackberry, Symbian, Tizen, WebOS.

Sandip can be reached at sandip.net@gmail.com
Tweet: @sandipnascar
FB: https:sandipnascar
View My Page

Comments and Discussions