Surendra Sharma

Surendra Sharma

Search This Blog

Wednesday, June 12, 2013

Set Command Timeout for SqlCommand

If you are running store procedure or SQL query from C# which is taking long time to run, set Command-timeout of SQL Command object in seconds.
For this, you don’t need to set connection timeout in connection string in config file.
A value 0 means no limit.
Suppose you want to set command time out for 3 hour = 3 * 60 * 60 = 10800 sec
SqlCommand objSqlCommand = new SqlCommand();

objSqlCommand.CommandTimeout = 10800; 

File Pointer in StreamReader

If you are reading from file using StreamReader by using string fileContent = sr.ReadToEnd();
and now if you will try to read it again using while ((line = sr.ReadLine()) != null)  for the same object, then you will not get any single word as file pointer is on end of file due to sr.ReadToEnd();
StreamReader sr = new StreamReader(fullFilePath);

if (sr != null)
{
string fileContent = sr.ReadToEnd();

if (fileContent.Contains(",") || fileContent.Contains("-"))
{
     
StringBuilder sbZipRadius = new StringBuilder();
string line = string.Empty;

//Read file line by line
while ((line = sr.ReadLine()) != null)
{
//if ',' exists in the line,it will be replace by '-'
line = line.Replace(',', '-');
}
}

//Close the file
sr.Close();
sr.Dispose();

}

Wednesday, June 5, 2013

IIS 7.5 error: Handler “PageHandlerFactory-Integrated” has a bad module “ManagedPipelineHandler” in its module list

Run Cmd “%windir%\Microsoft.NET\Framework\v4.0.30319\aspnet_regiis.exe -i

Bind Client Script function

public static void BindClientScript(string script, Control control) {
            ScriptManager.RegisterStartupScript(control, typeof(Page), Guid.NewGuid().ToString(), script, true);


        }

How to Get Geocoding Search Results

public static XElement GetGeocodingSearchResults(string address) {
            // Use the Google Geocoding service to get information about the user-entered address
            // See http://code.google.com/apis/maps/documentation/geocoding/index.html for more info...
            var url = String.Format("http://maps.google.com/maps/api/geocode/xml?address={0}&sensor=false", HttpContext.Current.Server.UrlEncode(address));

            // Load the XML into an XElement object (whee, LINQ to XML!)
            var results = XElement.Load(url);

            // Check the status
            var status = results.Element("status").Value;
            if (status != "OK" && status != "ZERO_RESULTS")
                // Whoops, something else was wrong with the request...
                throw new ApplicationException("There was an error with Google's Geocoding Service: " + status);

            return results;

        }

Get Latitude Longitude From Location

public static void GetLatitudeLongitudeFromLocation(string address, out double SelectedLatitude, out double SelectedLongitude) {

            SelectedLatitude = 0;
            SelectedLongitude = 0;

            string url = "http://maps.googleapis.com/maps/api/geocode/" + "xml?keyword=Hotels, + Australia&address=" + address + "&sensor=false&region=au";


            WebResponse response = null;

            try {
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
                request.Method = "GET";
                response = request.GetResponse();

                if (response != null) {

                    XPathDocument document = new XPathDocument(response.GetResponseStream());
                    XPathNavigator navigator = document.CreateNavigator();

                    var resultXML = XDocument.Parse(navigator.InnerXml);

                    if (resultXML != null) {
                        var geocodeStatus = (from geocodeResponse in resultXML.Descendants("GeocodeResponse")
                                             select new {
                                                 Status = geocodeResponse.Element("status").Value,
                                             }).First();


                        if (geocodeStatus.Status.Equals("OK")) {
                            var geoLocation = (from Location in resultXML.Descendants("GeocodeResponse").Descendants("geometry").Descendants("location")
                                               select new {
                                                   Latitude = Location.Element("lat").Value,
                                                   Longitude = Location.Element("lng").Value,
                                               }).First();

                            if (geoLocation != null) {
                                SelectedLatitude = Convert.ToDouble(geoLocation.Latitude);
                                SelectedLongitude = Convert.ToDouble(geoLocation.Longitude);

                            }
                        }
                    }
                }
            } catch (Exception ex) {
                string error = ex.Message;
            } finally {
                if (response != null) {
                    response.Close();
                    response = null;
                }
            }

            Console.ReadLine();

        }

Calculate distance between two points on Earth

        public static Double CalculateDistance(Double latitude1, Double longitude1, Double latitude2, Double longitude2) {
            double theta = longitude1 - longitude2;
            double dist = Math.Sin(deg2rad(latitude1)) * Math.Sin(deg2rad(latitude2)) + Math.Cos(deg2rad(latitude1)) * Math.Cos(deg2rad(latitude2)) * Math.Cos(deg2rad(theta));
            dist = Math.Acos(dist);
            dist = rad2deg(dist);
            dist = dist * 60 * 1.1515;
            dist = dist * 1.609344;
            return (dist);
        }

        private static Double deg2rad(Double deg) {
            return (deg * Math.PI / 180.0);
        }

        private static Double rad2deg(Double rad) {
            return (rad / Math.PI * 180.0);

        }