Code to Set the Language Properties for a User Profile

One of the first tasks in developing an international site is to decide how you will determine the locale settings preferred by the user. Although there are a number of methods for accomplishing this, one common way is by reading the language and currency properties from the user profile. These two properties are included in the default user profile supplied in Commerce Server. If your site relies on these properties to determine how to display locale-specific content, you will need to mark these as required fields in your user registration form.

Once you have retrieved the preferences for a user from their user profile, you can use these settings to display the appropriate content. The following code example illustrates this concept. The example consists of a simple HTML form for entering a user ID, password, and language preference. The form posts the entered data to an ASP file named Profileexamp.asp. The Profileexamp.asp file instantiates the Profile Service, and creates a new profile with the user ID, password, and language preference entered in the form. The Profileexamp.asp file then retrieves the language setting for the new user profile, and displays it.

Note this code example does not reflect best practices for developing with the Profile objects, and is only provided for illustrative purposes. Error handling has been removed, and instantiating the Profile Service at the page level is not recommended. For a more complete example of how to use the Profile objects, see Profile Sitelet.

Form to enter user information

<HTML>
<HEAD>
<META NAME="GENERATOR" Content="Microsoft Visual Studio 6.0">
<TITLE></TITLE>
</HEAD>
<BODY>
<Form ID="setlang" Action = "profileexamp.asp" Method = "Post">
<Input Type= text id=useridin name=userid>User ID</Input><BR>
<Input Type= text id=pwordin name=pword>Password</Input><BR>

<SELECT id=select1 name=Lang>
<OPTION value=fr-FR>French</OPTION>
<OPTION value=en-US>English</OPTION>
<OPTION value=de-DE>German</OPTION></SELECT>
<BR>
<Input Type=submit>
</Form> 

</BODY>
</HTML>

Profileexamp.asp

<html>
<body>
<%
dim oProfService
dim oProfile
dim sConnStr
dim sUser
dim sCatagoryname
dim sLang
dim sProfile
dim sUserLang

'Instantiate ProfileService and setup string variables
Set oProfService = Server.CreateObject("Commerce.ProfileService")
'do not use SA as username – remember to use strong passwords
sConnStr = "Provider=CSOLEDB;User ID=username;Password=******;Initial Catalog=Retail_Commerce;Data Source=Myserver;"
sLang = Request.Form ("Lang")
sUser = Request.Form ("Userid")
sProfile = "UserObject"

'Initialize the Profile Service, create the profile, and set the language preference
oProfService.Initialize sConnStr
Set oProfile = oProfService.CreateProfile (sUser, sProfile)
oProfile.GeneralInfo.Language = sLang
oProfile.Update
‘Retrieve the profile property you just set and write it out to the client
sUserLang = oProfile.GeneralInfo.Language
Response.Write sUserLang
%>
</body>
</html>

See Also

International Campaigns

Copyright © 2005 Microsoft Corporation.
All rights reserved.