Chris_web
Chris_web

Reputation: 761

Sql server management studio does not allow to edit a value of table

Using c# code, I have converted my byte array to a string, and then stored into a sql table using Entity framework. No issues until here.

But, if I try to see value stored inside a table using 'select' command in MSSql Server Management studio, it does show blank value in that column. If I query Len(MyColumn) then it shows a valid length.

Why I am unable see value stored?

Update: It shows a value when I right click on table -> Edit top 200 rows. But, as soon as I click on that cell, value disappears. I am using Sql server 2005.

Upvotes: 0

Views: 1117

Answers (1)

Scott Chamberlain
Scott Chamberlain

Reputation: 127543

I have converted my byte array to a string

If you did something like message= Encoding.UTF8.GetString(myByteArray) you likely got unprintable characters. A leading \0 can cause your string to not print. This is the wrong way to handle binary data in a SQL database.

The correct way to handle this is one of the following options (in order of preference)

  1. Store the data as a varbinary not a nvarchar this lets you store the byte[] directly with EF without doing any conversion at all.
  2. Encode the data correctly
    1. Using Base64 encoding via message= Convert.ToBase64String(myByteArray) and myByteArray = Convert.FromBase64String(message)
    2. Using hexadecimal, SoapHexBinary makes it very easy to go to and from hexadecimal strings.

Upvotes: 4

Related Questions