0

I'm pretty new to C# (As I'm sure you'll be able to tell) and I know this question has been asked a few times but none of the solutions have worked for me.

I'm trying to pull a Varbinary(MAX) value out of a database and store it as a string. I'm using a SqlDataReader for lifting some of the other columns out which works fine. All I'm using for them is .ToString(), but if I use ToString() for the Logo it returns System.Byte[]

    public TemplateData(SqlDataReader dr)
    {
        initialiseData();
        if (dr.HasRows)
        {

            Logo = dr["Logo"].ToString();
            TemplateId = dr["TemplateId"].ToString();
            Comment = dr["Comment"].ToString();
            SchemeCode = dr["SchemeCode"].ToString();
            Version = dr["Version"].ToString();
        }
    }

There is a plethora of issues I'm having aswell but I'll leave them for now.

Cheers!

1
  • 2
    What is the reason of storing varbinary as string, not as byte[]? Very strange design, I suppose. Commented May 20, 2015 at 9:03

1 Answer 1

2

Cast as a byte array then convert, for example:

byte[] data = (byte[])row["BinaryColumn"];
string str = Encoding.UTF8.GetString(data);

However this is making the big assumption the binary is some kind of string, otherwise you might want Base-64:

string str = Convert.ToBase64String(data);
Sign up to request clarification or add additional context in comments.

1 Comment

Yeeaaah... Cheers for the quick response, but it's not working :(

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.