0

I am trying to convert a type array to a string. I have done my research and nothing seems to be working for me. Here is my code,

 if (row.Table.Columns.Contains("DataRow") && !Convert.IsDBNull(row["DataRow"]))
        {
            byte[] rowData;

            if(Byte.TryParse(row["DataRow"].ToString(), out rowData)
            {
                dbModel.DataRow= rowData;
            }
            else
            {
                return null;
            }

I am trying to initialize and assign the array to the "host" and hold the value throughout the if statement. DataRow holds varBinary(128), the arguments in the second if statement give out an error that reads "The best overloaded method match for 'byte.TryParse(string, out byte)' has some invalid arguments

John Saunders
  • 157,405
  • 24
  • 229
  • 388
Brian
  • 399
  • 2
  • 4
  • 13
  • 1
    What is in row["DataRow"]? What does *nothing seems to be working* mean? Does it compile? Dies it throw an exception? Does it output wrong values? – rene May 01 '15 at 19:42
  • What would a byte array .ToString() look like? You can convert it to a Base64 string and send it, then convert it back to a byte array on the other side, thats probably the best option. – Ron Beyer May 01 '15 at 19:43
  • Byte.TryParse() will only parse a single byte. We need to the format of DataRow as rene says. – GazTheDestroyer May 01 '15 at 19:44
  • Sorry I will edit to be more specific. DataRow holds varBinary(128), the arguments in the second if statement give out an error that reads "The best overloaded method match for 'byte.TryParse(string, out byte)' has some invalid arguments " @rene – Brian May 01 '15 at 19:45
  • possible duplicate of [How to convert byte\[\] to string?](http://stackoverflow.com/questions/1003275/how-to-convert-byte-to-string) – Rich Turner May 01 '15 at 19:46
  • What is the meaning of the data in the `varBinary(128)` - 'binary' data could be anything, and how you would convert that to some string representation depends on what the bits actually mean. Do the bits represent UTF-8 encoded text? Then you would use the UTF8 decoder to convert. Do the bits represent an numeric value? Then you'd probably use something like BigInteger to convert it to the integer's string. Do the bits represent arbitrary data? Then a hex dump or base64 encoding is probably the most reasonable approach. We need to know more to help you, though. – antiduh May 01 '15 at 19:49
  • It's very unclear what you're trying to accomplish. What's in this `varbinary` field, and how does it relate to a string? Is it readable data? – John Saunders May 01 '15 at 19:49
  • It is encryted data @JohnSaunders – Brian May 01 '15 at 20:00
  • So what are you trying to do with it? If it's encrypted, you're not going to be able to use it until it's decrypted. – John Saunders May 01 '15 at 20:09
  • I am getting it from the database through the service. I wont worry about decrypting it until I get to the controller. – Brian May 01 '15 at 20:16
  • But why attempt to convert it to the string at all then? Why not just leave the data as a `byte[]` and pass that forward? Do you have to write this over a text-based protocol, or display to the user somehow? There are several approaches to doing that, such as performing base64 conversion, or treating it as a 128-byte integer and obtaining the string representation of the numeric value. If you want to treat this a 128-byte numeric integer, then I'd recommend using BigInteger to convert it to text for you, but we really need to know how the data is going to be used. – antiduh May 01 '15 at 20:19

1 Answers1

1

If you really are reading an array, try this:

if (row["DataRow"] != DBNull.Value)
{
    byte[] data = (byte[])row["DataRow"];
}

If its coming back from the provider as a byte array, that will change it from object back to a byte array.

Ron Beyer
  • 10,378
  • 1
  • 15
  • 33