Click here to Skip to main content
15,892,059 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
hello

goal:

I want to save important data as binary

my approach:

i used following code to convert "RTF" and "String" to save them in SQL Server bank with "VarBinary" field:

Dim txt1 as Byte()
txt = System.Text.Encoding.UTF8.GetBytes("Test String")


and with this code i could convert retrieved data to string:

dim txt2 as string
txt2 = System.Text.Encoding.UTF8.GetChars(txt1)


problem:

my application design is 3 layer and a function return DataTable and i can get data correctly and convert them whithout any problem as long as calculation is being done just in codes. but when i want to assign DataTable to DataGridView.DataSource I face many errors just because of "VarBinary" column and finally shows an icon which indicated i were supposed to load image in that column, well of course those columns are string actually.

is my approach correct for that goal?
how can i solve this problem?

thank you in advance
Posted
Comments
Sergey Alexandrovich Kryukov 7-Sep-15 13:43pm    
All you do with Encoding seems wrong and irrelevant to what you call "problem". You can get help only if you properly explain what you want to achieve.
—SA
mmbguide 7-Sep-15 15:11pm    
in my application users are able to send each other message but, I want to encrypt all the message text to forbidden from being read by others who has access to bank
Sergey Alexandrovich Kryukov 7-Sep-15 20:51pm    
Encryption has nothing to do with cryptography.
—SA

mmbguide wrote:

in my application users are able to send each other message but, I want to encrypt all the message text to forbidden from being read by others who has access to bank
Encoding has nothing to do with cryptography. Do make a next step, no matter what it is, you need to learn at least the basics of cryptography. As a minimum, you to understand the ideas. You can start here:
https://en.wikipedia.org/wiki/Cryptography[^],
first of all, http://en.wikipedia.org/wiki/Public-key_cryptography[^],
https://en.wikipedia.org/wiki/Symmetric-key_algorithm[^],
https://en.wikipedia.org/wiki/Cryptographic_hash_function[^].

.NET FCL offers you virtually everything you may need:
https://msdn.microsoft.com/en-us/library/system.security.cryptography%28v=vs.110%29.aspx[^],
https://msdn.microsoft.com/en-us/library/system.security.cryptography.asymmetricalgorithm%28v=vs.110%29.aspx[^],
https://msdn.microsoft.com/en-us/library/system.security.cryptography.symmetricalgorithm%28v=vs.110%29.aspx[^],
… :-)

Your confusion with Encoding is so great that I advise you to learn at least main concepts of text representation and Unicode:
http://en.wikipedia.org/wiki/Unicode[^],
http://unicode.org[^],
http://unicode.org/faq/utf_bom.html[^],
https://msdn.microsoft.com/en-us/library/system.text.encoding%28v=vs.110%29.aspx[^].

—SA
 
Share this answer
 
v2
The simplest solution would seem to be: if it's a string, leave it as a string.
Use a NVARCHAR field in your SQL Server table (that knows about Unicode characters, so it "just works") and save the string directly using a parameterized query. That way, there is no conversion, so no chance of conversion problems, and the DataGridView will display the string content correctly as well.

Don't store anything in a field other than the most appropriate: it always causes problems later. And a c# string is a SQL NVARCHAR field.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900