Profile

Click to view full profile
Hi, I'm Veerapat Sriarunrungrueang, an expert in technology field, especially full stack web development and performance testing.This is my coding diary. I usually develop and keep code snippets or some tricks, and update to this diary when I have time. Nowadays, I've been giving counsel to many well-known firms in Thailand.
view more...

Wednesday, May 30, 2012

Embed Fonts in Windows Phone

To embed external fonts into Windows Phone, it is not difficult. First, you need to have fonts (*.ttf). Then, you add it into your Windows Phone project. Next, at your font properties, copy to output directory to copy if newer. Finally, in FontFamily property of your control. Refer to your font file followed by # then a font name, which contained in your font file.
// For example my font file name is RSU_Regular.ttf, and it is under fonts folder in my project.
// Refer to font name "RSU" which contained in RSU_Regular.ttf.
<UserControl ... FontFamily="fonts/RSU_Regular.ttf#RSU" ...> 
     ... 
</UserControl>
If you want to change control's font in C# file, you can do it by:
this.FontFamily = new FontFamily("fonts/RSU_Regular.ttf#RSU");

How to generate List of Anonymous Types in C#

Normally, we can use List<T> when T is a strong type, then we can add Class T to that List. However, we can trick it by creating a method that receive generic type T, and create List of type T as a return.
// Tricky method to return anonymous List
public static CreateList<T>(T ob)
{
     var list = new List();
     return list;
}

// Create anonymous object, and let's generate our list
var person = new { Name = "Hello", Age = 10 };
var list = CreateList(person);
list.Add(person);
list.Add(new { Name="John", Age = 47 });
It is useful in some cases, but be careful that anonymous objects you created are read-only objects, we can't modify their data after we created them.

Friday, May 18, 2012

Create Chart using Chart Helper in ASP.NET MVC 3

To insert chart into a web page in ASP.NET MVC3, you can use Chart Helper which included in MVC 3. It will generate image chart dynamically that you can use generated chart to be the image soruce in image tag.
In cshtml page has an image element as follows:
<!-- It will refer to /[Name]Controller/MyChart, and send back an image chart. -->
<img src="@Url.Action("MyChart")" alt="SimpleChart" />
Under your controller class, you create a MyChart method that return String in an image format (jpg/png) as an action.
public ActionResult MyChart()
{
     var bytes = new Chart(width: 400, height: 200)
          .AddSeries(
               chartType: "bar",
               xValue: new[] { "Math", "English", "Computer", "Urdu" },
               yValues: new[] { "60", "70", "68", "88" })
          .GetBytes("png");
     return File(bytes, "image/png");
}
After that, you will get the chart image when requesting MyChart action.

Thursday, May 17, 2012

Read Excel files in .NET

First of all, some people may have a chance to work with excel files. However, to working with it for both reading and writing it has some difficulties e.g. how many ways to access excel files especially if you are not Windows users, which use Microsoft Excel in daily life. To solve this problem, what we need is a cross platform tool to access excel data.

Here, it is a tool for Excel Data Reader to read excel files in CodePlex (to use this tool in Linux, you must install Mono).

First, Know your format first, different version of Excel using different class.
FileStream stream = File.Open(filePath, FileMode.Open, FileAccess.Read);
//1. Reading from a binary Excel file ('97-2003 format; *.xls)
IExcelDataReader excelReader = ExcelReaderFactory.CreateBinaryReader(stream);
// or
//2. Reading from a OpenXml Excel file (2007 format; *.xlsx)
IExcelDataReader excelReader = ExcelReaderFactory.CreateOpenXmlReader(stream);
Second, Load it as a DataSet, also includes the feature select the first row to be the column name.
//3. DataSet - Create column names from first row (true/false)
excelReader.IsFirstRowAsColumnNames = true;
//DataSet - The result of each spreadsheet will be created in the result.Tables
DataSet result = excelReader.AsDataSet();
Third, Get the data from DataTable.
foreach(DataRow row in result.Rows)
{
     // Do something
}
Finally, Don't forget to free resources (IExcelDataReader is IDisposable)
excelReader.Close();
More at: http://exceldatareader.codeplex.com

There are also many ways to access Excel files. e.g. using Microsoft.Jet.OLEDB, Microsoft.Office.Interop.Excel.

For example: using Microsoft.Jet.OLEDB to access a excel file.
String filePath = "somewhere";
// Load connection string
OleDbConnection con = new OleDbConnection(String.Format("Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};Extended Properties=Excel 8.0", filePath));
// select [column_name], ... from [sheet_name]$[range]
OleDbDataAdapter da = new OleDbDataAdapter("select * from [Sheet1$A1:C3]", con);
// Load data into DataTable
DataTable dt = new DataTable();
da.Fill(dt);

foreach (DataRow row in dt.Rows)
{
     // Do something
}

excelReader.Close();

What is the difference between a dashboard and a scorecard?

A scorecard measures performance against goals and typically display in graphic indicator to visually display the overall success and failure. It is based on key performance indicators(KPIS).

Scorecard sample
But a dashboard is a container for various types of reports, and provide high level of interactivity.

Dashboard sample

Wednesday, May 16, 2012

How to get all items in current directory in C#

To get all items in a given current directory it needs to use a combination of two methods: GetDirectories(), GetFiles(). Both of them are the static method in class Directory which return all folders, and all files in the given directory respectively.
static String[] Dir(String path)
{
     // Get all folders in a given path
     List list = Directory.GetDirectories(path).ToList();
     // Get all files in a given path then add them to the list which contains folders
     list.AddRange(Directory.GetFiles(path));
     // You may sort the result, the ordering will be similar to dir function in the command prompt
     list.Sort();
     // Get the result
     return list.ToArray();
}
To use the function you written, it is very easy to use by giving the string path to it, it will give you an result.
static void Main(string[] args)
{
     string[] filepaths = Dir(@"C:\\");
     // Show all items under your drive C.
     foreach (String filepath in filepaths)
          Console.WriteLine(filepath);
}

Monday, May 7, 2012

Interesting Blog posts about HTML5

Mostly about game development with HTML5: .
Facebook HTML5 Showcase: Facebook HTML5 Center.

p.s. If there are any more interesting blog posts about HTML5, I will add them on this post.