Click here to Skip to main content
6,295,667 members and growing! (16,105 online)
Email Password   helpLost your password?
Web Development » ASP.NET » Data     Intermediate

Using Character Encoding in ASP.NET

By Samuel Chen

Submit your data to the OS or database server which does not support the character set.
C#.NET 1.0, .NET 1.1, Win2K, WinXP, Win2003, ASP.NET, Visual Studio, Dev
Posted:24 Mar 2004
Updated:15 Apr 2004
Views:131,484
Bookmarked:44 times
Announcements
Loading...
 
Search    
Advanced Search
printPrint   Broken Article?Report       add Share
  Discuss Discuss   Recommend Article Email
15 votes for this article.
Popularity: 4.30 Rating: 3.66 out of 5
2 votes, 13.3%
1
1 vote, 6.7%
2
3 votes, 20.0%
3
3 votes, 20.0%
4
6 votes, 40.0%
5

Introduction

This sample shows how to put your text completely into the operation system or database which does not support the text's character set.

Background

In web application development, frequently I have to connect each kind of old database and OS from customers such as SCO5.05 which does not support UTF-8, GB2312 and other character sets. So, how to completely store or take out my text data has became an important job.

Once on a project, I needed to put some Chinese words in UTF-8 into the system of SCO5.05 + Informix7.3. But when I check the database, found that all characters were changed into "->" (\0x7F) in fact. Many ways I tried, but ever could not solve this problem.

Why?

I found the answer later: this is the trouble of character encoding.

Open the file named web.config in the ASP.NET project. The value of requestEncoding attribute in globalization element is "utf-8". It means the requested texts were encoded as UTF-8 character set. Because SCO5.05 does not support UTF-8, therefore the requested texts where changed.

I got it. The texts should be encoded into the western language (iso8859-1) which SCO5.05 can distinguish from UTF-8 before saving, and converted back after loading.

Solution code

For example, to put the message "���Pi(\u03a0)", means "Hello Pi(��)", into "memo" field of database, use the following code:

// message "Hello pi(��)" in Chinese

string unicodeStr = "���Pi(\u03a0)";

OdbcConnection conn = new OdbcConnection();
System.Data.IDbCommand cmd = conn.CreateCommand();

conn.ConnectionString = "your connection string";
cmd.Connection = conn;

// Encoding here

cmd.CommandText = "INSERT INTO encoding VALUES ('" 
  + CEncoding.unicode_iso8859(unicodeStr) + "')";
cmd.Connection = conn;
conn.Open();
cmd.ExecuteNonQuery();
conn.Close();

I used the function unicode_iso8859() above. It can convert the texts from UTF-8 to ISO8859-1.

public static string unicode_iso8859(string src) {
  Encoding iso = Encoding.GetEncoding("iso8859-1");
  Encoding unicode = Encoding.UTF8;
  byte[] unicodeBytes = unicode.GetBytes(src);
  return iso.GetString(unicodeBytes);
}
public static string iso8859_unicode(string src) {
  Encoding iso = Encoding.GetEncoding("iso8859-1");
  Encoding unicode = Encoding.UTF8;
  byte[] isoBytes = iso.GetBytes(src);
  return unicode.GetString(isoBytes);
}

Select your database and take a look. Is that all the texts converted into ISO symbol which you do not recognized?

Then you can convert back reversely by using iso8859_unicode() function. Of course, you can convert back with other encodings as you want.

If you are using an adapter and binding a DataSet to a DataGrid, it is easy to encode the data with these two methods, too. But you will pay the cost of more time. Use it or not? It is under your own judgment. J

OdbcAdapter adapter = new OdbcAdapter(); 
DataSet1 ds = new DataSet1(); 
DataGrid grid = new DataGrid(); 
OdbcConnection conn = new OdbcConnection(); 

// conn, adapter, dataset and datagrid were initialized 

conn.ConnectionString = "your connection string"; 
adapter.Connection = conn;
adapter.Fill(ds);

string xml = ds.GetXml();
ds.Clear();

// encoding here

ds.ReadXml(new System.IO.StringReader(CEncoding.iso8859_unicode(xml)));
grid.DataBind();

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Samuel Chen


Member
blog(chinese) http://blog.SamuelChen.net
Occupation: Software Developer (Senior)
Location: China China

Other popular ASP.NET articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 25 of 29 (Total in Forum: 29) (Refresh)FirstPrevNext
Generalunicode_iso8859 method returns nulls for some Unicode chars, this works better Pinmembermaestroboomer15:29 31 Jan '08  
AnswerRe: unicode_iso8859 method returns nulls for some Unicode chars, this works better PinmemberGokhan Mamaci5:54 11 May '09  
QuestionProblem in displaying devanagri characters stored in postgres database PinmemberSimmi Kapoor20:13 3 Apr '07  
GeneralI need help Pinmemberrealnaimi0:03 27 Aug '06  
QuestionIs there other workaround other than using your approach? Pinmemberkpchan222:01 1 Jun '06  
AnswerRe: Is there other workaround other than using your approach? PinmemberTechzorcism7:26 27 Mar '07  
QuestionChinese characters Pinmembermrajanikrishna1:07 19 May '06  
QuestionRe: Chinese characters PinmemberSamuel Chen22:12 19 May '06  
AnswerRe: Chinese characters Pinmembermrajanikrishna23:12 19 May '06  
AnswerRe: Chinese characters PinmemberSamuel Chen1:24 20 May '06  
GeneralRe: Chinese characters Pinmembermrajanikrishna16:05 21 May '06  
GeneralThank you! Pinmemberquitchat7:57 5 Apr '06  
Generalhelp Pinmemberviketo7:50 21 Mar '05  
GeneralRe: help PinmemberSamuel Chen17:08 22 Mar '05  
GeneralRe: help PinsussAnonymous18:24 23 Mar '05  
GeneralRe: help PinmemberSamuel Chen18:23 13 Apr '05  
QuestionISO 8859-6 PinmemberNisha G20:23 20 Mar '06  
AnswerRe: ISO 8859-6 PinmemberSamuel Chen20:11 22 Mar '06  
GeneralGreat stuff !!! PinmemberYovav11:09 23 Apr '04  
GeneralRe: Great stuff !!! PinmemberSamuel Chen7:45 24 Apr '04  
GeneralRe: Great stuff !!! PinmemberYovav8:08 24 Apr '04  
GeneralRe: Great stuff !!! PinmemberSamuel Chen9:26 24 Apr '04  
GeneralRe: Great stuff !!! PinmemberYovav10:32 24 Apr '04  
GeneralRe: Great stuff !!! PinmemberSamuel Chen18:29 24 Apr '04  
GeneralRe: Great stuff !!! PinmemberSamuel Chen9:29 24 Apr '04  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 15 Apr 2004
Editor: Nishant Sivakumar
Copyright 2004 by Samuel Chen
Everything else Copyright © CodeProject, 1999-2009
Web15 | Advertise on the Code Project