Live Traffic Feed

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