ASP Localization: Having Fun with Different Languages

When working as a software developer in multi-cultural countries, like Malaysia and Singapore, localization of your app is very important sometimes. To reach out more users, it’s better if your app can communicate with the users no matter what language they speak.

Resource Files for ASP.NET
I tried doing localization for OneSync two years ago when I was developing its third version. The localization for C# desktop app is easy and straight-forward. It just needs to have different resource files for different languages and then retrieve the information from the files based on user’s selected language.

So, how about ASP.NET? It is basically the same thing. Resources are available in ASP for web pages contents localization as well (Reference: http://msdn.microsoft.com/en-us/library/ms228208.aspx). In ASP, there are two types of resource files: Global Resource Files and Local Resource Files. According to MSDN (http://msdn.microsoft.com/en-us/library/ms247246.aspx), global resource files are available to any web page in the web site and local resource files are only associated with a single web page. Does that mean we should store the resources that will be used in more than one web page under the Global Resource Files? I guess so. However, I once tried putting all my localized texts under the Local Resource Files folder. It did not actually stopping me from retrieving the localized texts in the local resource files from more than just one web page. I’m still not sure why it’s so.

Where to Store Language Setting?
One place to store the user language setting for your ASP website can be cookies. This is because information about user language selection should be saved permanently. Actually it’s not really permanent storage because the Expires field of a cookie must be set. If it’s not set, then the cookie will be treated as a session, meaning the cookie expires when the session ends. Thus, no expiry date means the cookies won’t be stored in the user’s hard disk at all (Reference: http://msdn.microsoft.com/en-us/library/ms178194.aspx).

To create a new cookie, I currently use the following codes.

Response.Cookies[“language”].Value = “Bahasa Melayu”;
Response.Cookies[“language”].Expires = DateTime.Now.AddDays(365);

Here, I make the cookies expiration to be one year later of its creation.

To read the user language setting from the cookie, I just need to use the following code: Request.Cookies[“language”].Value and then I can happily get the user language selection.

Yup, that’s all with my first attempt in ASP localization.

ASP Textbox Not Updated; User Input Not Captured

I was playing with MS SQL just now and I encounter a problem when I would like to update a table in database. It later turned out that the problem was not in my UPDATE statement, but due to the fact that the user inputs in ASP textboxes are “not captured”.

Let me simplify the scenario. I have one textbox and one submit button.

<asp:TextBox ID=”tb_editName” runat=”server” Width=”432px” Text=”<%# retrieveInfo() %>”></asp:TextBox>

<asp:Button ID=”btn_editInfo” runat=”server” Text=”Save” onclick=”btn_editInfo_Click”/>

Here is part of the CodeBehind of the page:

protected void Page_Load(object sender, EventArgs e)
{

DataBind();

}

protected string retrieveInfo()
{

tb_editName.Text = “Kagami”;

}

protected void btn_editInfo_Click(object sender, EventArgs e)
{

//update the table with UPDATE statement

}

If I entered a value, for example “Konata”, into the textbox tb_editName and then clicked the button btn_editInfo, the UPDATE statement will always update the table in database with the value “Kagami”, instead of the user input “Konata”.

I searched on Google and then found the following online discussion: http://forums.asp.net/t/1104590.aspx/1. They said the problem was due to the fact that the Page_Load event would be fired in each postback.

Ah-ha! I found the solution: Using Page.IsPostBack. So basically I just need to make sure my table is updated before the DataBind() is called. Yup, after I have done that, everything is working fine again. =)

What about >=2 Buttons?
The example above has only 1 button. So, will there by any different if we have two or more buttons? The answer is yes.

Page.IsPostBack does not say anything about which button is pressed. So, to handle the cases where we have two or more buttons, we should have a change in the code organization shown above. A detailed discussion can be found here: http://p2p.wrox.com/asp-net-3-5-basics/79502-how-check-if-button-clicked-asp.html. Page_Load should have only the code which needs to be executed every time when the page loads. The actions that need to be carried out after a button is clicked should still be handled in the click event of the button.

Ah… I see.

No Permission to Create Database in SQL Server 2008 R2

This morning, I was playing with Microsoft SQL Server since I was asked by my senior to learn using it before going to work in next week.

I thought I had the free Express version. However, when I searched through the programs list, I found out that I actually had Microsoft SQL Server 2008 R2 in my computer. Yay~ I guess I got it for free from school. Thanks MSDNAA for helping SoC students that much!

I was very happy until I run the program. When I was about to create a database, I just found that I had no permission to do so. Oh my tian… I even forgot the password that I set that time (Wait… Did I even set a password?). I looked at the users list (/Security/Logins), I only saw BUILTIN\Users and sa. Ano… Where was the BULTIN\Administrators? I later found out that due to security issues, new SQL Server 2008 no longer had BUILTIN\Administrators in the SQL Server sysadmin fixed server role (Reference: Database Engine Configuration – Account Provisioning). Also I couldn’t access using SA account because I forgot the password that I set a long, long time ago.

Besides, I also could not create new user. That was quite disappointing.

CREATE DATABASE Permission Denied
CREATE DATABASE Permission Denied

Later, I read a thread on StackOverflow about resetting the SA password: http://stackoverflow.com/questions/4188193/how-do-you-reset-the-sa-password. Their problems were very similar to mine. Although it was about resetting the SA password, I guess if I could reset the password, I should have no problem accessing the databases as well.

Hence, I decided to try out their suggestions: Starting SQL Server in single-user mode. To do so, after closing the SQL Server Management Studio window, I launched SQL Server Configuration Manager. From there, I looked for the SQL Server instance. I then right-clicked on it go to Properties.

Step 1: Right-click Server Instance, Select Properties
Step 1: Right-click Server Instance, Select Properties

In the Advanced tab, I entered parameter “-m;” before existing parameters in “Startup Parameters”.

Step 2: Add -m as Parameter
Step 2: Add -m as Parameter

Finally, I clicked on OK and then restarted the server instance.

Step 3: Restart Server Instance
Step 3: Restart Server Instance

After that, I run SQL Server Management Studio again. Then I saw the following error message when I tried to connect with Windows Authentication.

Error: Only One Admin Can Connect At One Time
Error: Only One Admin Can Connect At One Time

Soon, I found that it won’t show this error message if I run SQL Server Management Studio as administrator as shown below.

Run SQL Server Management Studio as Administrator
Run SQL Server Management Studio as Administrator

Yup, now I can successfully create databases, add new users and reset passwords. =)

Now, it is time to remove the “-m” parameter.

In fact, there are online tutorials about starting SQL Server instance in single-user mode, for example http://blog.sqlauthority.com/2009/02/10/sql-server-start-sql-server-instance-in-single-user-mode/

Oh yeah, if there is a need to login SQL Server Management Studio with SA account, I can enable it in the Login Properties of the SA account (Reference: http://anujyadavcse.blogspot.com/2010/08/login-failed-for-user-sa-reason-account.html).

Enable SA Login
Enable SA Login