Category: Programming

All about programming

ASP.NET download big file using BinaryWrite

Author: | Categories: Programming No Comments
There are situation when you need to provide download option for a big file located somewhere on server or generated at runtime. Below function could be used to download files of any size. Sometimes downloading big file throws exception OutOfMemoryException showing “Insufficient memory to continue execution of the program”. So this function also handle this […]

C# Create Instance of a Class from string

Author: | Categories: Programming No Comments
In C# You can create instance of any class dynamically using Activator.CreateInstance method public object GetInstance(string FullyQualifiedNameOfClass) { Type t = Type.GetType(FullyQualifiedNameOfClass); return Activator.CreateInstance(t); } If FullyQualifiedNameOfClass is in another assembly then loop through all assemblies and find the Type. For that use below code: public object GetInstance(string FullyQualifiedNameOfClass) { Type type = Type.GetType(FullyQualifiedNameOfClass); if […]

Get query string values in JavaScript

Author: | Categories: Programming No Comments
Option 1 No need jQuery for that purpose. Just pure JavaScript: function getParameterByName(name) { name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]"); var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"), results = regex.exec(location.search); return results === null ? "" : decodeURIComponent(results[1].replace(/\+/g, " ")); } Usage: var prodId = getParameterByName('prodId'); Option 2 var urlParams; (window.onpopstate = function () […]

Detect a mobile device in jQuery

Author: | Categories: Programming 4 Comments
Below are few options to detect whether web request is from a Mobile device using Javascript. Option 1 if( /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent) ) { // some code.. } Option 2 var isMobile = { Android: function() { return navigator.userAgent.match(/Android/i); }, BlackBerry: function() { return navigator.userAgent.match(/BlackBerry/i); }, iOS: function() { return navigator.userAgent.match(/iPhone|iPad|iPod/i); }, Opera: function() { return […]

Detecting Mouse Movement with jQuery

Author: | Categories: Programming No Comments
Javascript / jQuery can help in capturing Mouse move, Scroll and Click events Option 1 – Capturing Mouse move along with Click and Scroll var timeout = null; $(document).on('mousemove click scroll', function() { clearTimeout(timeout); timeout = setTimeout(function() { console.log('Mouse idle for 3 sec'); }, 3000); }); Option 2 – Capturing Mouse with Javascript (function() { […]

Write text file without Read lock in C#

Author: | Categories: Programming No Comments
Suppose there is a situation in which a service is writing a log file and there is another application which needs to open this file for reading. By default a file which is written is exclusively open by writer. To overcome this and make Write and Read non-exclusive use the following code. Writer var writer […]

Log4net logging in asp.net MVC

Author: | Categories: Programming No Comments
Make changes in web.config file as follows <configuration> <configSections> ... <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" /> </configSections> <appSettings> ... <add key="log4net.Config" value="log4net.config"/> <add key="log4net.Config.Watch" value="True"/> </appSettings> <log4net> <appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender"> <file type="log4net.Util.PatternString" value="D:/Logs/TTG SmartANPR REST Service/smartanprrest_%date{ddMMyyyy}.log" /> <appendToFile value="true" /> <rollingStyle value="Date" /> <datePattern value="yyyyMMdd" /> <staticLogFileName value="true" /> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="%date %level - %message%newline" […]

WebClient with Timeout Support

Author: | Categories: Programming No Comments
By default the C# WebClient class does not let you modify the default timeout value of 1 minute. This subclass exposes the Timeout property so you can change the default without having to use a HttpWebRequest. using System; using System.Net; namespace MyNamespace { public class WebClientEx : WebClient { public int Timeout { get; set; […]

HTTP Status Code in case of Exception

Author: | Categories: Programming No Comments
This is how to get HTTP Status code in case of an Exception during HttpWebRequest catch(WebException ex) { HttpWebResponse res = (HttpWebResponse) ex.Response; Console.WriteLine("Http status code: " + res.StatusCode); }

Visual Source Safe with Visual studio 2013

Author: | Categories: Programming No Comments
By default Visual Studio 2013 does not integrate with Visual SourceSafe. In order to activate VisualSource Safe in Visual Studio 2013 activate this in Tools -> Options -> Source Control -> Plug-in Selection and change the plug in to the Visual SourceSafe. Adding a solution to source control will launch the Visual SourceSafe.