Saturday, April 28, 2012

C# Function to get Geo Coding Using Google Maps Geocoding API


 using  Newtonsoft.Json;
 


    public class GeoLocation
    {
        public decimal Lat { getset; }
 
        public decimal Lng { getset; }
    }
 
    public class GeoGeometry
    {
        public GeoLocation Location { getset; }
    }
 
    public class GeoResult
    {
        public GeoGeometry Geometry { getset; }
    }
 
    public class GeoResponse
    {
        public string Status { getset; }
 
        public GeoResult[] Results { getset; }
    }


private Dictionary<stringstring> getLatLong(string address)
    {
  Dictionary<stringstring> diclatlong = new Dictionary<stringstring>();
  string url = "http://maps.googleapis.com/maps/api/geocode/json?address=" 
                + Server.UrlEncode( address) + "&sensor=false";
        WebResponse response = null;
        try
        {
         HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
         request.Method = "GET";
         response = request.GetResponse();
         if (response != null)
         {
           string str = null;
           using (Stream stream = response.GetResponseStream())
           {
            using (StreamReader streamReader = new StreamReader(stream))
            {
             str = streamReader.ReadToEnd();
            }
           }
            GeoResponse geoResponse = JsonConvert.DeserializeObject<GeoResponse>(str);
            if (geoResponse.Status == "OK")
            {
             int count = geoResponse.Results.Length;
             for (int i = 0; i < count; i++)
             {
        diclatlong.Add("Lat", geoResponse.Results[i].Geometry.Location.Lat.ToString());
        diclatlong.Add("Lng", geoResponse.Results[i].Geometry.Location.Lng.ToString());
             }
            }
            else
             {
              diclatlong.Add("Lat""");
              diclatlong.Add("Lng""");
             }
            }
        }
        catch
        { throw new Exception ("JSON response failed."); }
        finally
        {
            if (response != null)
                {
                response.Close ();
                response = null;
                }
        }
        return diclatlong;
    }


I have used JSon.NET Library

Friday, April 27, 2012

Calculate Age Function




private int CalculateAge(int year, int month, int day)
        {
            int age = 0;
            age = DateTime.Now.Year - year;
            if (DateTime.Now.Month < month)
                age--;
            if (DateTime.Now.Month == month)
            {
                if (DateTime.Now.Day < day)
                    age--;
            }
            return age;
        }

Friday, April 20, 2012

HTML Email Template Outlook

 Problem : You want to create a HTML template (Such as HTML formatted Email template to circulate within the organization, mentioning the latest Updates etc) in Outlook 2010


Solution
1. Create Your HTML design
2. Copy the HTML & the images to this folder C:\Program Files\Common Files\microsoft shared\Stationery
3. Give a proper name to your HTML file so that in templates you can easily identify it
4. Open Outlook 2010, Go to  File > Options > Mail > Click on Stationery and Fonts
5. Click on Theme button under Personal Stationery Tab
6. Choose your theme from the left list Click Ok & close all Windows. Now when you create a new email you can see your theme loaded.


Let me know if you need any more details :)