My CodePad

Fewer words, more code!

C#: Programmatically “Run As Administrator” — February 18, 2014

C#: Programmatically “Run As Administrator”

A few days ago I wanted to write to hosts, a HOSTS file in system32 used by Microsoft TCP/IP for Windows. Since this file is protected, you can edit it in administrative mode only. This means the user has to right click it and select “Run as administrator” for your project to run correctly. Fortunately there is a solution. The following code will request admin access from the user, and rerun it’s self in admin mode:

Continue reading

jQuery: How to style Selects and <asp:dropdowns — January 7, 2014

jQuery: How to style Selects and <asp:dropdowns

There is a strange thing about select tags. Browsers do the styling and you have almost no power over it! You can change some colors and fonts but you can never make them beautiful (or in my case I can’t give the designer a chance to make them beautiful).
I searched for a while and found some approaches with pointer-events and divs but they aren’t a complete solution because they either aren’t cross-browser or you can’t style the options of the select.
Continue reading

jQuery: Making AJAX Applications Crawlable — December 19, 2013

jQuery: Making AJAX Applications Crawlable

If you want a fast and responsive web app, then you are definitely using jQuery (or any other javascript libray) and ajax calls to keep the size of the page small and transfer only the data the user requested.

The problem that came along with this approach is that search engines can’t interact with your page the way a user does, and so, there is no way for a search engine to make the ajax call and get the content back from the server. Spiders just aren’t browsers and users! (Learn more about it here)

Continue reading

C#: Basic Combinatorics — December 13, 2013
Visual Studio 2012.4, “Run As Administrator” -> “The application cannot start” error — December 7, 2013

Visual Studio 2012.4, “Run As Administrator” -> “The application cannot start” error

After a good 2 hours of searching what the **** happened and Visual Studio stopped working, I finally found a working solution…

Just copy all dte*.olb files, from C:\Program Files (X86)\Common Files\Microsoft Shared\MSEnv to C:\Program Files X86\Microsoft Visual Studio 9.0\Common7\IDE.

Just for the record, I tried “devenv /resetuserdata” and deleting the involved registry keys with no luck at all. I end up using Process Monitor after reading this post:

With a little luck and a lot of tries,  I got a headache and a working studio.

How to Minify JavaScript with C#, a Google PageSpeed suggestion — December 5, 2013

How to Minify JavaScript with C#, a Google PageSpeed suggestion

Google sais “compacting JavaScript code can save many bytes of data and speed up downloading, parsing, and execution time” and that is obviously true (you can read about it here. And although there are many tools to minify javascript, like JSMin and YUI Compressor, it’s quite time consuming (and boring) to have to run these tools for each change your Web Designer decides to do for every JS file in your project. Except that, non of these tools compacts JS in one file to minimize browser requests.

The solution I came up goes like this (in my project this code runs only once in every application boot by using HttpContext.Current.Cache)

Step 1
Add a new Web Form to your project and name it js.aspx

Continue reading

Client-Side filtering of a RadListBox for ASP.NET AJAX with jQuery — November 27, 2013

Client-Side filtering of a RadListBox for ASP.NET AJAX with jQuery

The well known “Write Less, Do More, JavaScript Library” can easily help you filter options of select tag. Problem is, that Telerik’s control produces a different set of tags as your drop down list:

<div id="rdSchem" class="RadListBox RadListBox_Default RadListBoxScrollable">
	<div class="rlbGroup rlbGroupRight">
		<ul class="rlbList">
			<li id="rdSchem_i0" class="rlbItem">
				<input type="checkbox" class="rlbCheck">
				<span class="rlbText">Option 1</span>
			<li id="rdSchem_i1" class="rlbItem">
				<input type="checkbox" class="rlbCheck">
				<span class="rlbText">Option 2</span>
	<input id="rdSchem_ClientState" name="rdSchem_ClientState" type="hidden" autocomplete="off" value="{&quot;isEnabled&quot;:true,&quot;logEntries&quot;:[],&quot;selectedIndices&quot;:[],&quot;checkedIndices&quot;:[],&quot;scrollPosition&quot;:0}">

Continue reading

Export MS SQL Database schema with C# — November 18, 2013

Export MS SQL Database schema with C#

A few days ago, I was searching for a way to export MS SQL schema of all databases of a server for backup and migration purposes. Although I found some tools, what I needed was a way to get stored procedures, functions, views, triggers and everything even if it was encrypted.

Since I couldn’t find a way to automate this (e.g. run it every night to backup schema of my server), I thought it would be a nice weekend project.

You can read more about it, or download project from!

Continue reading

JSON to C# objects — November 11, 2013

JSON to C# objects

.NET natively supports JSON through System.Web.Script.Serialization.JavaScriptSerializer.
In shorthand, it goes like this:

using System.Web.Script.Serialization;
var obj = new JavaScriptSerializer().Deserialize({YOUR JSON STRING});//String to Object

This, will de-serialize your JSON string to a generic object, but going from this to get a value well within the structure of a JSON string is quite messy:

Continue reading

Getting Real Time Visitor Stats to your blog through Google Analytics (and a little more!) — May 27, 2013

Getting Real Time Visitor Stats to your blog through Google Analytics (and a little more!)

When I decided to create and maintain a blog, the first though that came to my mind was that I didn’t had the time neither to setup nor to support just another open source tool. I just wanted something to immediately start writing and posting and was the perfect solution. Everything is quick and easy here EXCEPT stats. They are awful, especially for someone like me that uses analytics for over 30 sites. I looked around but I found nothing about supporting google analytics to, and that is quite true, I couldn’t have full analytics support. The only thing I managed to do was to have real time visitor stats through google analytics (plus hits,pageviews,location etc), using a little hack and the google analytics image call. Nothing much, but I thought I should share and maybe some one else could find a better solution…

Supposing that this is your tracking code:

  (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),

  ga('create', 'UA-xxxxxx-xx', '');
  ga('send', 'pageview');

You just need to follow these steps:
Continue reading