Live Traffic Feed

Wednesday, March 7, 2018

Get dynamic list from store procedure using C# with async

Leave a Comment

public class dalc
    {
        SqlConnection conn = new SqlConnection();
        // SqlConnection conn;

        public dalc()
        {
            conn.ConnectionString = @"YourConnectionString";
            // conn.ConnectionString = ConfigurationManager.ConnectionStrings["myConnectionString"].ConnectionString;
        }

        /// <summary>
        /// Get list by query as a string
        /// </summary>
        /// <typeparam name="T">any class or object</typeparam>
        /// <param name="str">query</param>
        /// <returns>list of data</returns>
        public async Task<List<T>> selectbyqueryList<T>(string str)
        {
            DataTable dt = new DataTable();
            SqlCommand cmd = new SqlCommand();
            cmd.CommandTimeout = 0;
            cmd.Connection = conn;
            cmd.CommandType = CommandType.Text;
            cmd.CommandText = str.ToString();
            SqlDataReader dr;
            try
            {
                await conn.OpenAsync();
                dr = await cmd.ExecuteReaderAsync();
                List<T> lst = new List<T>();
                while (await dr.ReadAsync())
                {
                    T item = GetItem<T>(dr);
                    lst.Add(item);
                }
                return lst;
            }
            catch (Exception e)
            {
                throw e;
            }
            finally
            {
                conn.Close();
                cmd.Parameters.Clear();
                cmd.Dispose();
                // conn.Dispose();
            }
        }

        /// <summary>
        /// Get list by calling store procedure with parameters
        /// </summary>
        /// <typeparam name="T">any class or object</typeparam>
        /// <param name="Query">name of store procedure</param>
        /// <param name="para">parameters</param>
        /// <returns></returns>
        public async Task<List<T>> GetList<T>(string Query, SqlParameter[] para)
        {
            DataTable dt = new DataTable();
            SqlCommand cmd = new SqlCommand();
            cmd.CommandTimeout = 0;
            cmd.Connection = conn;
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddRange(para);
            cmd.CommandText = Query.ToString();
            SqlDataReader dr;
            try
            {
                await conn.OpenAsync();
                dr = await cmd.ExecuteReaderAsync();
                List<T> lst = new List<T>();
                while (await dr.ReadAsync())
                {
                    T item = GetItem<T>(dr);
                    lst.Add(item);
                }
                return lst;
            }
            catch (Exception e)
            {
                throw e;
            }
            finally
            {
                conn.Close();
                cmd.Parameters.Clear();
                cmd.Dispose();
                // conn.Dispose();
            }
        }

       

        /// <summary>
        /// Get list by calling query with parameters
        /// </summary>
        /// <typeparam name="T">any class or object</typeparam>
        /// <param name="Query">parameterize query</param>
        /// <param name="para">parameters</param>
        /// <returns></returns>
        public async Task<List<T>> GetList_Text<T>(string Query, SqlParameter[] para)
        {
            DataTable dt = new DataTable();
            SqlCommand cmd = new SqlCommand();
            cmd.CommandTimeout = 0;
            cmd.Connection = conn;
            cmd.CommandType = CommandType.Text;
            cmd.Parameters.AddRange(para);
            cmd.CommandText = Query.ToString();
            SqlDataReader dr;
            try
            {
                await conn.OpenAsync();
                dr = await cmd.ExecuteReaderAsync();
                List<T> lst = new List<T>();
                if (dr.HasRows)
                {
                    while (await dr.ReadAsync())
                    {
                        T item = GetItem<T>(dr);
                        lst.Add(item);
                    }
                }
                return lst;
            }
            catch (Exception e)
            {
                throw e;
            }
            finally
            {
                conn.Close();
                cmd.Parameters.Clear();
                cmd.Dispose();
                // conn.Dispose();
            }
        }

        /// <summary>
        /// Get dictionary data from query
        /// </summary>
        /// <param name="qry">query</param>
        /// <returns>list of dictionary</returns>
        public async Task<List<Dictionary<string, object>>> GetQryData(string qry)
        {
            SqlCommand cmd = new SqlCommand();
            cmd.CommandTimeout = 0;
            cmd.Connection = conn;
            cmd.CommandType = CommandType.Text;
            cmd.CommandText = qry;
            SqlDataReader dr;
            try
            {
                await conn.OpenAsync();
                dr = await cmd.ExecuteReaderAsync();
                // conn.Close();
                List<Dictionary<string, object>> lst = new List<Dictionary<string, object>>();
                while (await dr.ReadAsync())
                {
                    Dictionary<string, object> obj = new Dictionary<string, object>();
                    for (int i = 0; i < dr.FieldCount; i++)
                    {
                        obj[dr.GetName(i)] = dr[i].GetType().Name == "DBNull" ? null : dr[i];
                    }
                    lst.Add(obj);
                }
                return lst;
            }
            catch (Exception e)
            {
                throw e;
            }
            finally
            {
                conn.Close();
                cmd.Parameters.Clear();
                cmd.Dispose();
            }
        }

        public T GetItem<T>(SqlDataReader dr)
        {
            Type temp = typeof(T);
            T obj = Activator.CreateInstance<T>();

            for (int i = 0; i < dr.FieldCount; i++)
            {
                foreach (PropertyInfo pro in temp.GetProperties())
                {
                    if (pro.Name.ToLower() == dr.GetName(i).ToLower())
                    {
                        if (!string.IsNullOrEmpty(Convert.ToString(dr[i])))
                        {
                            if (pro.PropertyType.Name == "String")
                                pro.SetValue(obj, Convert.ToString(dr[i]));
                            else if (pro.PropertyType.Name == "Byte[]" && string.IsNullOrEmpty(Convert.ToString(dr[i])))
                                pro.SetValue(obj, new byte[0]);
                            else
                                pro.SetValue(obj, dr[i]);
                        }
                        break;
                    }
                }
            }
            return obj;
        }

    }

Read More

Monday, March 5, 2018

Open Source cryptocurrency Exchanges

Leave a Comment

I would like to discuss with you guys which one of these open source exchanges is the best to go with, which one has it's cons and pros and if you can give us your experience with one of these open source exchanges as a costumer or owner/operator of any exchange.
Here are some basic info for each of these open source exchanges.
BlinkTrade
Live exchanges: FoxBit UrduBit Surbitcoin ChileBit
WLOX
Website: http://wlox.io/
Live Exchanges: 1BTCXE
Peatio
Website: http://www.peat.io/ 
Live Exchanges: Yunbi OneWorldCoin BitSpark
MarginCall
Read More

Friday, September 2, 2016

Generate Excelsheet With Leading zeros using C#

Leave a Comment
public void GenerateExcel()
    {
        System.Data.DataTable dt = new System.Data.DataTable();
        dt.Columns.Add("AccountNo");
        for (int i = 0; i < 10; i++)
        {
            string a = "00001234456" + i;
            if (a.StartsWith("0"))
            {
                a = a.Substring(1).Replace("0", "&CHAR(048)&");
                a = "=CHAR(048)" + a;
                if (a.EndsWith("&"))
                    a = a.Substring(0, a.Length - 1);
                a = a.Replace("&&", "&");
            }
            dt.Rows.Add(a);
        }

        GridView GridView1 = new GridView();
        GridView1.AllowPaging = false;
        GridView1.DataSource = dt;
        GridView1.DataBind();
        string FileName = "Excel";
        HttpContext.Current.Response.Clear();
        HttpContext.Current.Response.Buffer = true;
        HttpContext.Current.Response.AddHeader("content-disposition",
         "attachment;filename=" + FileName + ".xls");
        HttpContext.Current.Response.Charset = "";
        HttpContext.Current.Response.ContentType = "application/vnd.ms-excel";
        StringWriter sw = new StringWriter();
        HtmlTextWriter hw = new HtmlTextWriter(sw);
        GridView1.RenderControl(hw);
        //Open a memory stream that you can use to write back to the response
        byte[] byteArray = Encoding.ASCII.GetBytes(sw.ToString());
        MemoryStream s = new MemoryStream(byteArray);
        StreamReader sr = new StreamReader(s, Encoding.ASCII);
        HttpContext.Current.Response.Write(sr.ReadToEnd());
        HttpContext.Current.Response.Flush(); // Sends all currently buffered output to the client.
        HttpContext.Current.Response.SuppressContent = true;  // Gets or sets a value indicating whether to send HTTP content to the client.
        HttpContext.Current.ApplicationInstance.CompleteRequest();

    }
Read More

Monday, August 8, 2016

Find all rows within 2 km area of your specific point using sql server 2008

1 comment
Create PROCEDURE [SearchViaLocation]
     @Longitude nvarchar(50)='0'
     ,@Latitude nvarchar(50)='0'
     ,@Distance decimal(18,8)=2000---(2km)
    
AS
BEGIN
     -- SET NOCOUNT ON added to prevent extra result sets from
     -- interfering with SELECT statements.
     SET NOCOUNT ON;

   DECLARE @CurrentLocation geography;
SET @CurrentLocation  = geography::Point(@longitude, @latitude, 4326)
SELECT @CurrentLocation
SELECT * FROM YourTblName
WHERE geography::STPointFromText('POINT(' + CAST(ISNULL([LongitudeColumnName],'0') AS VARCHAR(20)) + ' ' +
                    CAST(ISNULL([LatitudeColumnName],'0') AS VARCHAR(20)) + ')', 4326).STDistance(@CurrentLocation )<= @Distance


In Above query you need to change 'YourTblName' with your table name,
'LongitudeColumnName' with your column which stores longitude and 
'LatitudeColumnName' with your column which stores latitude. 
Read More