Category Archives: Miscellanous

This would contain all post that can’t go to the other categories

How database index works

Ever wonder when you trigger a query that took 2 minutes to complete then when you applied index as suggested by the SQL Server Database Engine Tuning Advisor then it suddenly runs under a second?.

Think of it like this, when a data is stored in a disk based storage devices, the entirety of the data is stored as blocks of data. When running a query against unindexed field which value is not unique, to search a value it would require to scan the entire blocks of data (at worst N).

With an indexed field, a new blocks of data is created to store the indexed field which value is already sorted. Therefore binary search is performed when trying to find a value that in the indexed fields (log2 N).

Now for example if you have a table schema like the following


Field Data type Size in disk
Id (primary key) unsigned int 4 bytes
FirstName char(50) 50 bytes
LastName char(50) 50 bytes

with that schema, to store a record in a disk it would take 104 bytes, or in one disk block (1024 bytes) it can store 1024/104 = 9 records in a disk block. If you have 1.000.000 records then it would take 1.000.000/9 = 111.111 disk block to store all of those data.

Now depending on the type of query that you run against the table you would get different result in performance, for example if you do a search query against the Id field it would perform a binary search (log2 n) which results in log2 111.111 = 16 block access. This is possible because the Id field is a primary key which value has to be unique and also has been sorted.

Compare it with a query against the FirstName field, since the FirstName field is not sorted a binary search would not be possible, thus it would require exactly 111.111 block access to find the value,  a huge difference.

Creating an index would help greatly in slow performing query, but once again you have to keep mind that creating index would also mean creating a new data structure that is stored in the disk. For example if we are to create an index for the FirstName field

Field Data type Size in disk
FirstName char(50) 50 bytes
(record pointer) special 4 bytes

Based on that schema then it would require 54 bytes for each record. 1024/54 = 18 record in a disk block.  1.000.000/18 = 55.555 disk block. Manage your index wisely, the more fields the index contains or the more index that you created the more disk space that it’s going to take.


Request format is unrecognized for URL unexpectedly ending in /methodname

Spent quite some time trying to figure out this error message

Request format is unrecognized for URL unexpectedly ending in /methodname


This happens when i tried to send http POST to a remote computer, work fine on my local machine, from here it says.

The .NET Framework 1.1 defines a new protocol that is named HttpPostLocalhost. By default, this new protocol is enabled. This protocol permits invoking Web services that use HTTP POST requests from applications on the same computer

the solution is to add the following in the web.config


Stack VS Heap

There’s two type of memory that we should know. Stack and Heap, what’s the difference ?


  • keeps track of the code execution contains
  • it uses LIFO structure
  • high performance memory, fixed limit
  • local variables goes to stack
  • points to an object in heap


  • It’s a large pool of operating system memory
  • used in dynamic memory allocation
  • garbage collector remove any resources that no longer used


let’s take a look on how stack and heap works


when that method is called, this what’s happening in the stack

  1. The Go() method is called
  2. the x local variable is defined and goes to the stack, x is a pointer that point to MyInt instance in the heap
  3. MyValue property is set, the value is stored in heap because MyValue is declared on the heap
  4. DoSomething() method is called
  5. the MyValue value in the heap is changed to 12345


SQL Connection Pooling

It is expensive to create an SQL connection, a socket connection must be established, a handshake must occurred, the connection credential must be check against the list of known credentials. To optimize this a technique called SQL Connection Pooling is implemented.

Every time a connection is required, we request it from the connection pool, if there’s exist the connection by the specified connection string then it’s going to return that connection rather than creating a new one. Every time we are going to close a connection we do not close it but instead we returned it to the connection pool. This way we can save the connection for later use.


What is SQL Injection ?

It’s a technique which is used to attack a software, be it a desktop application or a website as long as it uses a database server behind it. It is done by inputting a malicious input in attempt to get a dangerous query to run.


This will get all products where the product name is Car

Imagine the Car value is retrieved from an input text on a form somewhere, what would happen if we input something like this.

This query will delete –assuming the running user has access- all data from our products table. which is clearly not good.

Issues like this are commonly happens on code that concatenates string to form query

What should we do ?

  • Do not concatenates string to form a query
  • Use a parameterized query to execute query, ORM tools such as EF uses parameterized query
  • Turn on custom error page on production to avoid giving crucial information to malicious users
  • Give the running user appropriate permissions, do not give access to modify table or creating new record in table if you only want a read only operation

Face detection with Emgu CV

I was wondering how facebook tag functionality work,  how it would be smart enough to draw a rectangle to a person face and tag it. Well i’m not trying to do something that complex yet, i just want to draw a rectange around a person’s face.

So i head to Emgu CV.

Emgu CV is a cross platform .Net wrapper to the OpenCV image processing library. Allowing OpenCV functions to be called from .NET compatible languages such as C#, VB, VC++, IronPython etc. The wrapper can be compiled in Mono and run on Windows, Linux, Mac OS X, iPhone, iPad and Android devices.


And the result ?


The actual algorithm to do this i believe is pretty complex, thankfully i don’t need to mind all that complicated stuff and just use the functionality from Emgu CV.

Here are the steps that i used to build this simple app

  • Head to sourceforge and download the installer, i’m using version 2.4.2. Needless to say you need to install it.
  • Create an empty ASP.NET website project
  • Change the configuration properties to build in x86
  • Reference Emgu.CV.dll, Emgu.CV.UI.dll, and Emgu.Util.dll
  • Right click project and add existing items, go to Emgu installation directory and under x86 add all the dlls
  • Add an aspx page to handle the file upload and displaying the image
  • Add an ashx handler to do the face detection.
  • Add the haarcascade_frontalface_default.xml file to the solution.

Too simple, i know, i’m too lazy to put pretty picture on the steps. I think seeing the code is much better idea anyway.

Here you go

Granting Execute permission to all SP

Was shown a quick way to grant Execute permission to SP today by a colleague, it seems i’ve been doing it the hard way all this time.

Imagine  i had this list of SP.


the list still goes longer than that. now the story is that i need to give execute permissions to those SP, and the way that i know is to do this.

or by doing this

i would get this


copy paste that on the sql editor and life is good.

OR,  as my colleague showed to me

i could do this instead

  • Right click on the database, choose properties
  • From permissions, search the user that you want to give permission
  • then on the Execute row, tick the grant checkbox
  • click ok and the permissions is setup for that user


now life is even better!.


I’ve joined this online course in Coursera that discuss about gamification. Now even though i consider myself a gamer, and it’s true that the course title pique my interest in the first place. gamification is not something you study to make yourself better as a gamer or a game maker.

So what is gamification ?

Gamification is a concept where you use game elements and game designs in a non gaming context.


Major companies has started to implement gamification in their way of interacting with the consumer, take for example Nike Plus. Where you put your running shoes and then when you complete your running it will when have access to your phone or laptop update your achievement on twitter by saying how many km that you take. How does this matter to Nike, well of course it matters to them, they want to make more people run, the more people that run means more people will buy their product.

Taking the course have made me more aware on the gamification concept being implemented on my daily life. I noticed that my current company give virtual points to among employees to increase motivation and productivity. So it’s not just about major companies increasing their sales. It’s about taking the fun, challenges, and sense of accomplishment of gaming to non gaming context.

I’m having a great fun joining this course, and it’s free!. i get to expand my knowledge to other than just hitting the keyboard and produce awesome code  -self claimed Smile with tongue out

For those of you who want to join the course, you can still join as the first week homework deadline is on 9th September 2012.

See ya there.

The problem with writing to convey message

The problem is that it could be misinterpreted. I mean even by just talking the things that we meant to say could have been perceived differently by the person we are talking to. And trying to convey the same message through writing could make matters worse.

It’s especially true when the person trying to convey the message does not have the skill –yes it’s a skill- to deliver the message effectively. Have you ever encountered that smiley icon on your favorite messenger ?. that one picture could mean a lot of different things depending on the context of the conversation. it could be a warm smile, a fake smile, even sinister smile using the same icon!.

Or how about the symbol that we usually use to indicate strong expressions, yes I’m talking about the exclamation mark. Here’s how I feel if I have somebody text me a message like this:

Stop! – okay, he’s telling me to stop

Stop!! – okay, he’s starting to get pissed off, I must stop immediately

Stop!!! – he’s clearly stretching his veins in his neck to say that. and I better damn stop whatever I’m doing right now before he punch right through me.

Like it’s said on

The sign used in writing after an exclamation or interjections, expressing strong emotion or astonishment, or to indicate a command.

So dear reader, be wiser and don’t type excessive symbol like this, because that would cause the person on the other end to interpret the message differently.. unless of course you do it knowingly the meaning of those exclamation marks.

Why am I writing this you might say ?. Just have a lot on my mind right now and thought writing some random blog post late night would do me some good. ha!

Creating a Dynamic Dictionary with C# 4 dynamic

In C# 4 there’s a new keyword introduced which is dynamic.

Here’s what msdn say about it

The dynamic type enables the operations in which it occurs to bypass

compile-time type checking. Instead, these operations are resolved at

run time.

combined with the var keyword.. anonymous method, and don’t forget anonymous object. I couldn’t help get the feeling that coding in C# is getting similar in a way coding in javascript Smile.

Right, let’s get back to the topic,  so I want to create a dynamic dictionary which enable me from doing this

to this

This is surprisingly quite easy to implement, as all we need to is extend our class from DynamicObject that reside in System.Dynamic and override couple of methods..two actually.

The trick is in the TryGetMember and TrySetMember method which set and get the property for dynamic object. There’s other method in DynamicObject that you can override such TryGetIndex and TrySetIndex which try to access the dynamic object by index.

You can read more about it in Aaron’s blog