Morgan
Morgan

Reputation: 181

Convert a byte array in a datatable column to string?

I have a datatable customerTbl and I step through the rows

foreach (DataRow row in customerTbl.Rows)
{
         string CustomerID = row["Customer ID"].ToString();
}

However, the column Customer ID returns byte[]. How can I convert this to a string (CustomerID)?

I tried something like

string CustomerID = Convert.ToBase64String(row["Customer ID"]);

but it obviously doesn't work

Thanks in advance

Upvotes: 0

Views: 7846

Answers (1)

Jeff Mercado
Jeff Mercado

Reputation: 134571

Depending on the encoding of the bytes, you'd need the correct Encoding object to perform the conversion. Assuming it is ASCII, you can do this:

string customerID = Encoding.ASCII.GetString((byte[])row["Customer ID"]);

If in a different encoding (UTF8, UTF16, etc.), use the appropriate one.

Upvotes: 3

Related Questions