Feb 8, 2012

SQL get current datetime

SELECT CURRENT_TIMESTAMP
or
SELECT {fn NOW()}
or
SELECT GETDATE()

Feb 7, 2012

byte[] convert to hexadecimal string (and vice versa)

byte[] to hex string:
static string ByteArrayToString1(byte[] source)
{
    StringBuilder sb = new StringBuilder();
    foreach (byte item in source)
    {
        sb.AppendFormat("{0:x2}", item);
    }
    return sb.ToString();
}

static string ByteArrayToString2(byte[] source)
{
    return BitConverter.ToString(source).Replace("-""");
}

static string ByteArrayToString3(byte[] source)
{
    // namespace: System.Runtime.Remoting.Metadata.W3cXsd2001
    return new SoapHexBinary(source).ToString();
}

static string ByteArrayToString4(byte[] source)
{
    // works for .Net 4 only
    return String.Concat(Array.ConvertAll(source, x => x.ToString("X2")));
}

Loop through enum

enum Week
{
    Sunday,
    Monday,
    Tuesday,
    Wednesday,
    Thursday,
    Friday,
    Saturday
}
foreach (Week item in Enum.GetValues(typeof(Week)))
{
    //...
}

Reference: Can You Loop Through All Enum Values? c#

Jan 16, 2012

IPv4 Address convert to Integer (and reverse) (updated)

static Int64 IPv4StringToInt64(string ipv4String)
{
    Int64 ipInteger = 0;
    try
    {
        ipInteger = (long)(uint)IPAddress.NetworkToHostOrder(BitConverter.ToInt32(IPAddress.Parse(ipv4String).GetAddressBytes(), 0));
    }
    catch (Exception)
    { }

    return ipInteger;
}
static string IPv4Int64ToString(Int64 ipv4Int64)
{
    if (ipv4Int64 < 0)
        return "0.0.0.0";
    else if (ipv4Int64 < 4294967295)
        return IPAddress.Parse(ipv4Int64.ToString()).ToString();
    else
       return "255.255.255.255";
}
Usage:
Console.WriteLine("255.255.255.0 => " + IPv4StringToInt64("255.255.255.0"));
Console.WriteLine("4294967040 => " + IPv4Int64ToString(4294967040));

Reference: stackoverflow - How to convert an IPv4 address into a integer in C#?

Jun 19, 2011

Kenny's XML Encrypter

Name: Kenny's XML Encrypter
Version: 1.1
Platform: Windows with .Net Framework 2.0 installed
Introduction:
A small tool that can encrypt/decrypt xml element and attribute value. You can click Help button to see how to use it.
You can set different keys to encrypt different elements or attributes in the same time. Just don't forget those keys.
This tool only works on those leaf elements.
Play around, and leave a message at here if you found any problems.

PS: Don't forget to keep an original copy of your XML file in case something happeded. The save is done automatically when you click the "Encrypt" or "Decrypt" button.

Jun 14, 2011

Refresh WebBrowser in Winform

I put a WebBrowser control on my form and use it to load XML files. Here is the code:
OpenFileDialog FileDialog = new OpenFileDialog();
FileDialog.Title = "Open XML Document";
FileDialog.Filter = "XML file (*.xml)|*.xml";
if (FileDialog.ShowDialog() == DialogResult.OK)
{
  WebBrowser1.Navigate(FileDialog.FileName);
  WebBrowser1.Refresh();
}
I found that if I load the second XML, sometimes it will not refresh itself to show the new XML content. Instead, it still shows the previous one. I google the web to see if there is any other way to prevent the cache. Unfortunately, I found nothing. But this post inspires me and so I create a workaround.

May 24, 2011

Integrated the EasyAlgo FlashUpload control into the ASP.Net web application

Recently, I used their FlashUpload flash control to manage user uploads. It is a very powerful flash control that can let you customize the control. The price is affordabile (or you can try it free without limitations), and their customer service is outstanding. I use google talk to chat with them when I encounter any problem (also request some features!) with no problem.

OK, no more advertisement. :) Below is an example of what I've done for customizing it. The reason I put a blog here is because their document is good but not organized very well. I spent lots of time on puting things together... You can select many ways to integrate the flash control into your web app. Here I choose javascript.