0

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

3
  • Why is CustomerId a byte[] in the first place? Commented Nov 22, 2010 at 5:29
  • What is the customer ID supposed to look like? Is it text? Or just a numeric thing? Convert.ToBase64String, does as it says on the package: it converts to a base64 string. Why do you want to see a string? What for? Commented Nov 22, 2010 at 5:30
  • customer id is text and numeric, in the new database we have proper id's but I still need to ship this over for some other referencing. The other database only accepts strings in the column where I want this ID. Commented Nov 22, 2010 at 5:31

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Hi Jeff, with that I get "Error 1 'System.Data.DataRow' does not contain a definition for 'Field' and no extension method 'Field' accepting a first argument of type 'System.Data.DataRow' could be found (are you missing a using directive or an assembly reference?)"
You'll need a cast then instead of using the Field() extension method. Since you've added that it's C#2.0, extension methods are not available.
Ok thanks, it works now. Sorry I forgot to add 2.0 from the beginning.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.