Friday, March 26, 2010

e-Learning : Microsoft Content Preparation Tools

http://www.microsoft.com/learning/en/us/training/lcds.aspx#tab1

The Microsoft Learning Content Development System (LCDS) is a free tool that enables the Microsoft Learning community to create high-quality, interactive, online courses.

Thursday, March 25, 2010

C# - usage of static class

Below is an example of using "static class" in C#:

Ref.: http://msdn.microsoft.com/en-us/library/79b3xss3(VS.80).aspx

Main class()
{
...

public static class UtilFunc
{
private static string g_strLogFileURL = "test";

public static void SetLogDir(string strURL)
{
if (strURL != "") g_strLogFileURL = strURL;
}

public static string LogToFile()
{
return g_strLogFileURL;
}
}
...
}

Other Class()
{
....

// static class can't be instantiated, simply use its APIs as below
MessageBox.Show(UtilFunc.LogToFile());

....
}

SQL : comparison with Oracle Server

Below is an article which compares difference between SQL and Oracle server:

http://www.sql-server-performance.com/articles/dba/oracle_sql_server_comparison_p1.aspx

Sunday, March 21, 2010

C# - save content to CSV file from GridView

Below is an example to save content from GridView to CSV file:

{
....
string strPath = ".\\XXX_" + strFName + ".csv";
if (File.Exists(strPath))
{
File.Delete(strPath);
}

StringBuilder strColu = new StringBuilder();
StringBuilder strValue = new StringBuilder();
int i = 0;
int j = 0;

try
{
StreamWriter sw = new StreamWriter(new FileStream(strPath,
FileMode.CreateNew), Encoding.GetEncoding("GB2312"));
// ----------------------------------------------------------------
// Write header to CSV file
// ----------------------------------------------------------------
for (i = 0; i <= iTotalCol; i++)
{
strColu.Append(GridView_Task.Columns[i].HeaderText);
strColu.Append(",");
}
strColu.Remove(strColu.Length - 1, 1); // remove last character
sw.WriteLine(strColu);

// ----------------------------------------------------------------
// Write content to CSV file
// ----------------------------------------------------------------
for (i = 0; i < iTotalRow; i++)
{
strValue.Remove(0, strValue.Length);
DataGridViewRow dr = GridView_Task.Rows[i];

for (j = 0; j <= iTotalCol; j++)
{
strValue.Append(dr.Cells[j].Value.ToString());
strValue.Append(",");
}
strValue.Remove(strValue.Length - 1, 1); // remove last character
sw.WriteLine(strValue);
}
sw.Close();

MessageBox.Show("OK. Success to generate CSV file.");
}
catch (Exception ex)
{
MessageBox.Show("Fail to generate CSV file. Error=[" + ex.Message + "]");
}
...
}

Thursday, March 18, 2010

C# - Example of HTTP Post

Below is an eample for HTTP POST (with additional HTTP header)

{ ....
try
{
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create
("http://xxxx/Mt.do");
request.Method = "POST";

string parameter = "HTTP body content";
byte[] byteArray = Encoding.UTF8.GetBytes(parameter);

request.Headers.Add("Encrypt", strMD5);
request.Headers.Add("Mobile", "xxxx");
request.Headers.Add("Vas", "xxxx");

request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = byteArray.Length;

Stream dataStream = request.GetRequestStream();
dataStream.Write(byteArray, 0, byteArray.Length);
dataStream.Close();

HttpWebResponse response = (HttpWebResponse)request.GetResponse();
if (HttpStatusCode.OK == response.StatusCode)
{
dataStream = response.GetResponseStream();
StreamReader reader = new StreamReader(dataStream);

string line;
while ((line = reader.ReadLine()) != null)
{
MessageBox.Show(line.ToString());
}
dataStream.Close();
response.Close();
}
else MessageBox.Show("Response code not OK");
....

}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString());
}
...
}

Wednesday, March 17, 2010

C# - Example to create MD5 hash value

Below is an example to create MD5 has value of "strIn"
{
....

string strIn = "mc20101065806299113601931094";

// Create a new instance of the MD5CryptoServiceProvider object.
MD5 md5Hasher = MD5.Create();

// Convert the input string to a byte array and compute the hash.
byte[] data = md5Hasher.ComputeHash(Encoding.Default.GetBytes(strIn));

// Create a new Stringbuilder to collect the bytes
// and create a string.
StringBuilder sBuilder = new StringBuilder();

// Loop through each byte of the hashed data
// and format each one as a hexadecimal string.
for (int i = 0; i < data.Length; i++)
{
sBuilder.Append(data[i].ToString("x2"));
}

// Return the hexadecimal string.
MessageBox.Show(sBuilder.ToString());
...
}

Sunday, March 14, 2010

C# - read XML from SQL2005

Below is an exmaple to read XML content from SQL2005. Use code as below, otherwise, the returned XML may be truncated with size of 2K bytyes

{
....
System.Xml.XmlReader reader = null;

try
{
conn = new SqlConnection(g_strConnDB);
conn.Open();
cmd = new SqlCommand(strCmd, conn);

strCmd = "select *** from *** FOR XML RAW";
cmd.CommandText = strCmd;
reader = cmd.ExecuteXmlReader();
reader.Read();

strResult = "";
while (reader.ReadState != ReadState.EndOfFile)
{
strResult += reader.ReadOuterXml();
}
reader.Close();

....
}
}