Sunday, December 14, 2008

Localizing .NET applications

Most applications nowadays have to be localized in order to provide user interfaces in the language of the user who uses the application. The .NET framework offers a very handy mechanism to provide localization for your apps. It uses so called resource files (.resx) to store keys and the corresponding translated texts or images. If you want to know more on how to work with this translation mechanism take a look at this post.
The main problem with these files is to keep them in sync (for example when adding new strings) and to give translators an easy tool to make translation without requiring them to have visual studio installed on their computers.
I found a tool called "ResEx ... the composite, translation friendly .NET Resource editor" which can be found here that meets exactly this requriements. It provides a nice spreadsheet interface which makes it also for non programmers easy to translate the resources in the files. It is also possible to group resources together by using an underscore e.g. Menu_Home, Menu_Customers; they will all be shown under the tree node "Menu" which makes it easier to understand where the translations are used. Even adding and deleting localization languages is very easy.
The only thing which is missing is to have an automatic translation by using one of the several online translators available nowadays. I will suggest it on the forum and maybe this feature will be available in a future release.

Another very useful tool when working with resource files is the Resoucre Refactoring Tool on CodePlex. It helps you by localize strings in your source code by putting the string into the resource file and replacing the corresponding call in order to get a translated string.

Generating script for SQL Server database

Recently I had to deploy a project which used a Microsoft SQL Server database. During the deployment process all the tables and some predefined data should be created in the database of the machine on which the application is installed. I needed a simple .sql file which contains SQL instructions to create the schema and to insert the data. I thought that this would not be a big deal because SQL Server Management Studio has many tools and functionalities and for sure also one to do this since it is an essential part of software deployment. I used the export Wizard and everything worked well except that with the Wizard it is only possible to export the schema. I was very surprised that it is not possible to export the schema and the data.
After some research on the internet I found several commercial tools which are able to export also the data but I did not want to purchase such a tool only to export the data.
I found that there is an "API" for the SQL Server which is called SQL Server Management Objects (SMO) and SQL Server Replication Objects (RMO). These two "API's" can be used to get information from an SQL Server such as all the databases, all the tables in a database and all other information related to an SQL Server. I played a bit with them and there exists a method for each server object (such as databases, tables,...) which is called Script(). This method returns a StringCollection which can be used to re-create the SQL object. It is worth to give a look if you have to create, update or delete SQL objects from within code.
I tried to build a tool which is able to export a whole database, all its objects (function, triggers,...) and the data to a single sql file. I faced several problems. First it is important in which order the tables and constraints are created because otherwise the script would not run successfully. Second I had to generate insert statements for the data which has to consider all datatypes supported by SQL Server.
In some way I would have been able to solve these two issues but then I found the Microsoft SQL Server Database Publishing Wizard 1.1. This tool by Microsoft does exactly what I need. It exports one or more databases with their schema, objects and the data. If you have a similar issue I suggest you to use this one since it is free and worked fine for me :-)

Here you can find the official home page of the publishing wizard tool on CodePlex.

Monday, December 1, 2008

Show Collection of strings in GridView

Today I faced the following problem; I had a collection of strings which I wanted to show in a GridView. I tried to use the collection as datasource, but nothing was displayed. A quick and simple solution to this problem is the following:
- First you add a template column to the grid view
- In the template for the item you create a Literal control
- Then the Text property of the Literal is set to DataItem of the Container which is evaluated when databinding occurs

This can be accomplished with the following code:
<asp:gridview id="GridView1" runat="server" autogeneratecolumns="False" onrowdeleting="GridView1_RowDeleting">
<asp:templatefield headertext="column">
<asp:literal runat="server" id="customerNames" text="<%# Container.DataItem %>"></asp:literal>
<asp:commandfield showdeletebutton="True">

This code sets a string collection as datasource and binds it to the gridview:
List<string> list = new List<string>();

this.GridView1.DataSource = list;

But now is the question: how con you retrieve the value for example in the RowDeleting event. As accustomed to windows forms I tried:

but that did not work. To get the correct text I had to use:

Alltogether the cell contains 3 controls. The other two are LiteralControls with the Text '\r\n ' and the second one is the Literal control which holds the text.