Tuesday 27 September 2011

Create/Change SVN user and password using htpasswd

To create a new SVN user c:\program files\Collabnet\bin\htpasswd -cm ../config/authz-file username password To change existing SVN user's password c:\program files\Collabnet\bin\htpasswd -m ../config/authz-file username

Saturday 24 September 2011

Android Historical Version Names

Android version history and its nick name.
1.5 Cupcake
1.6 Donut
2.0 Eclair
2.2 Froyo
2.3 Gingerbread
3.0 Honeycomb

Friday 23 September 2011

JQuery Ajax Call to Web service via POST

Have been used a lots of jquery in the recent real-time information web development project, I found this quite easy to initiate a POST HTTP call to web service through JQuery. Here are 2 sample code snippets.

Case 1: Pass no parameter

 
<script type="text/javascript">
    // in this case, we post nothing back to api but
    // just call it (no parameter required) and expected json object as result
    function callWebApiViaPost() {
        $.ajax({
            type: "POST",
            url: "yourwebapi",
            data: "{}",
            contentType: "application/json;",
            dataType: "json",
            success: function (t) {
                $("#your_result_display_div").text(t.d);
            }
        });
    }

</script>
 

Case 2: Pass parameter(s)

For example, you have a API like below:
 
[WebMethod]
public static string ToUpperCase(string yourparam)
{
    return yourparam.ToUpper();
}
 
The corresponding jquery ajax post call will be like this:
 
<script type="text/javascript">
    // in this case, we post nothing back to api but
    // just call it (no parameter required) and expected json object as result
    function callWebApiViaPost() {
        $.ajax({
            type: "POST",
            url: "yourweb/ToUpperCase",
            data: "{yourparam: 'hello-world'}",
            contentType: "application/json;",
            dataType: "json",
            success: function (t) {
                $("#your_result_display_div").text(t.d);
            }
        });
    }

</script>
 

Friday 16 September 2011

SQL Server deadlock caused by Inappropriete Index

Recently one of my projects experienced weird transaction deadlock error on the data processing component. The component was running well without errors in the first week of production, but suddenly threw dozens of SQL exception "Transaction (Process ID 69) was deadlocked on lock resources with another process and has been chosen as the deadlock victim. Rerun the transaction.". After digging into a day of the code and database analysis, it appears that a row update operation actually causes an exclusive table level lock. As the result, other update request(s) from my multi-threading component trigger the deadlocks. A further analysis, the grief was a inappropriate primary key index which trigger SQL Server escalates the lock level from row to table. Once the root cause was identified, the remediation is considerable easy and is to make sure the "ALLOW_ROW_LOCKS" option was checked which should be a default setting in SQL Server 2008, and the index type is clustered.

 
alter index [your_index_name] on [dbo].[your_table_name] set (allow_row_locks = ON);
 

Friday 9 September 2011

Hash Table or Key/Value pair Array in JavaScript

Key/Value or hash table sometimes is very handy and helpful on implementing your business logic. In .Net world, I use a lot of Dictionary<TKey, TValue> collection object. How to do the same thing using Javascript is bit a tricky to me as I spent most of my time in the past using Microsoft C++ and C# developing server-side system. Google it a bit and here is my working solution including of declaration, insertion and deletion of items.
 
// declare a hash table
var myTable = new Array();
// add new items to the table
myTable['key1'] = 'hello';
myTable['key2'] = 'world';
myTable['key3'] = ' world.';

// remove a item from the table
delete myTable['key2'];
 

Thursday 8 September 2011

Dynamically add CSS file to ASP.Net page

Programmatically include CSS file to ASP.Net page is considerable simple. Add follow code to your page initialization event. Next time when page was loaded, reference to your css file will be added at runtime.

 
    protected void Page_Init(object sender, EventArgs e)
    {
        HtmlLink css = new HtmlLink();
        css.Href = "css/fancyforms.css";
        css.Attributes["rel"] = "stylesheet";
        css.Attributes["type"] = "text/css";
        css.Attributes["media"] = "all";
        Page.Header.Controls.Add(css);
    }
 

Tuesday 6 September 2011

Detect Browser or User Agent from Server-side

My previous post on Detect Browser or User Agent using JavaScript has described a solution on how to do it from client-side. Today, I want to show how to detect user agent or browser type at server-side. It's quite straightforward under ASP.Net framework. Put the following code in your Global.asax so it checks for every new request. The sample code is written in C# and can be in any .Net language.


 
public class Global : System.Web.HttpApplication
{
    void Session_Start(object sender, EventArgs e)
    {
        DetectBrowser();
    }
    // detect browser type by checking user agent
    // and then do specific things for each platform
    private void DetectBrowser()
    {
        string agent = Request.UserAgent.ToLower();
        if (agent.Contains("iphone") ||
            agent.Contains("symbianos") || 
            agent.Contains("ipad") || 
            agent.Contains("ipod") || 
            agent.Contains("android") || 
            agent.Contains("blackberry") || 
            agent.Contains("samsung") || 
            agent.Contains("nokia") || 
            agent.Contains("windows ce") || 
            agent.Contains("sonyericsson") || 
            agent.Contains("webos") || 
            agent.Contains("wap") || 
            agent.Contains("motor") || 
            agent.Contains("symbian"))
        {
            // do your logic here for device specific requests
        }
        else
        {
            // do your logic here for PC/Mac requests
        }
    }
}
 

Monday 5 September 2011

Detect Browser or User Agent from Client-side using JavaScript

Optimized or tweaked your website for particular device platform, such as iPhone, Android, Windows 7 and etc., becomes more and more critical for improving user experience and attracting more traffic to your website. Usually it makes more sense to detect user browser or user agent on server side in order to redirect traffic to your well built device specific version of your main site as well as regular PC or Mac users. But there is always a case that client-side detection is more handy. Furthermore, it's very easy to do it using JavaScript and is supported by almost every browser platform.

 

<script type="text/javascript">
function doDevice () {
   var agent = navigator.userAgent;
   if (agent.indexOf('Windows') > 0) {
       // it's windows platform
   } else if (agent.indexOf('Android') > 0){
      // it's android platform
   } else if (agent.indexOf('iPhone') > 0) {
      // it's iPhone platform
  }
}
</script>

 

Thursday 1 September 2011

Generate Random bytes using C#

There is a quick way to generate random bytes for any given size using .Net framework. Why people would want to do so? I personally find it very handy in case I need to test network flow, encryption and/or do load/stress testing. Here is the code that you can past to your project. It generate a random bytes array for the given size.
 
public static byte[] GenerateRandomBytes(int length)
{
      // Create a buffer
      byte[] randBytes;
 
      if (length >= 1)
       {
            randBytes = new byte[length];
        }
        else
        {
            randBytes = new byte[1];
        }

        // Create a new RNGCryptoServiceProvider.
        System.Security.Cryptography.RNGCryptoServiceProvider rand = 
             new System.Security.Cryptography.RNGCryptoServiceProvider();

        // Fill the buffer with random bytes.
        rand.GetBytes(randBytes);

        // return the bytes.
        return randBytes;
}