newbie in C# 2003, saving Asian characters to database problem

H

HibernatingBear

Hope I'm posting in the right place. Have spent more hours than i
would like on this problem and still have not solved it. I'm using
SQL Server for the db.

I have a GUI application with 2 multi-line textboxes that can be
populated with Asian characters. I can't get the Asian characters to
save to the database. (Bizarrely enough, when I manually saved a few
Asian characters in the database table, they displayed on the GUI with
no problems, and without using extra code, either. Thought I could do
a Save without problems, but got "???")


In trying to Save, I've initialized and used StreamReader with the
"using System.IO" namespace. That didn't work, seems the application
went looking for a file with the character name. Also tried an Int32
conversion(?) for the textbox, which turned it into a byte array.

Can anyone point me in the right direction? Hints? Anything?

Thanks in advance.
 
J

Jon Skeet [C# MVP]

HibernatingBear said:
Hope I'm posting in the right place. Have spent more hours than i
would like on this problem and still have not solved it. I'm using
SQL Server for the db.

I have a GUI application with 2 multi-line textboxes that can be
populated with Asian characters. I can't get the Asian characters to
save to the database. (Bizarrely enough, when I manually saved a few
Asian characters in the database table, they displayed on the GUI with
no problems, and without using extra code, either. Thought I could do
a Save without problems, but got "???")

See http://pobox.com/~skeet/csharp/debuggingunicode.html
 
J

james

Hope I'm posting in the right place. Have spent more hours than i
would like on this problem and still have not solved it. I'm using
SQL Server for the db.

I have a GUI application with 2 multi-line textboxes that can be
populated with Asian characters. I can't get the Asian characters to
save to the database. (Bizarrely enough, when I manually saved a few
Asian characters in the database table, they displayed on the GUI with
no problems, and without using extra code, either. Thought I could do
a Save without problems, but got "???")

In trying to Save, I've initialized and used StreamReader with the
"using System.IO" namespace. That didn't work, seems the application
went looking for a file with the character name. Also tried an Int32
conversion(?) for the textbox, which turned it into a byte array.

Can anyone point me in the right direction? Hints? Anything?

Thanks in advance.

You should look at the encoding that the StreamWriter is using. Some
encodings like ASCII will drop asian characters because they are
larger than 16-bit. As far as I know UTF-8 should not drop characters.
 
H

HibernatingBear

You should look at the encoding that the StreamWriter is using. Some
encodings like ASCII will drop asian characters because they are
larger than 16-bit. As far as I know UTF-8 should not drop characters.- Hide quoted text -

- Show quoted text -

Thought asian chars required at least 16-bit? Had the same problem
with 16-bit, anyway. Application won't do System.Convert.ToInt8,
unfortunately.

Will take a look at Jon Skeet's link above.

Thanks to both of you!
 
J

Jon Skeet [C# MVP]

HibernatingBear said:
Thought asian chars required at least 16-bit? Had the same problem
with 16-bit, anyway. Application won't do System.Convert.ToInt8,
unfortunately.

You shouldn't need to use any type of Convert.ToIntXXX.

Just keep everything as text, and it should be okay.
 
H

HibernatingBear

Changed from converting to bytes to using the following sql string to
insert text, and I'm still getting the "???"

string sql = "INSERT INTO MyTable"
+ " (Comment1)"
+ " VALUES (" txtComment.Text + "')";

It works fine for English. At this point, I'm just plain out of
ideas. On another bulletin board, found one guy who had a similar
problem, but the helper chose to delete his solution -- the post was
dated 2002, but looked like it would've helped if it was left up.
 
J

Jon Skeet [C# MVP]

HibernatingBear said:
Changed from converting to bytes to using the following sql string to
insert text, and I'm still getting the "???"

string sql = "INSERT INTO MyTable"
+ " (Comment1)"
+ " VALUES (" txtComment.Text + "')";

It works fine for English. At this point, I'm just plain out of
ideas. On another bulletin board, found one guy who had a similar
problem, but the helper chose to delete his solution -- the post was
dated 2002, but looked like it would've helped if it was left up.

Don't put the parameter in the command directly in the SQL like that -
use a parameterised command. That may well help.
 
H

HibernatingBear

Sorry to say, it didn't. :(

string strComment = this.txtComment.Text;
string sql = "INSERT INTO MyTable"
+ " (Comment1)"
+ " VALUES ("strComment+ "')";
 
J

Jon Skeet [C# MVP]

HibernatingBear said:
Sorry to say, it didn't. :(

string strComment = this.txtComment.Text;
string sql = "INSERT INTO MyTable"
+ " (Comment1)"
+ " VALUES ("strComment+ "')";

Um, that's still not a parameterised command.

I mean using a SqlCommand and its Parameters property.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top