Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8622147
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T06:58:55+00:00 2026-06-12T06:58:55+00:00

All, I have an SQL Parser and editor which I intend to integrate in

  • 0

All, I have an SQL Parser and editor which I intend to integrate in my application. When I run the following query

select * from sys.sysprocesses;

one of the columns returned is a type of byte[]. This column happily gets put into a DataTable, however, when I do

bindingSource.DataSource = result.DataTable;

and attempt to display the data in a DataGridView I get the obvious ArgumentException. In this position, what in the best way to change the byte[] to a string for display in the DataTable?

I could loop through the DataTable and do some thing like

foreach(DataColumn col in dataTable.Columns)
    if (col.DataType == typeof(byte[]))
        foreach (DataRow row in dataTable.Rows)
            row[col] = Encoding.ASCII.GetString((byte[])row[col]); 

But this will attempt to put a string into a byte[] column, and will not work. I could clone the DataTable then change the type,

DataTable dtCloned = dataTable.Clone();
dtCloned.Columns[0].DataType = typeof(String);
foreach (DataRow row in dataTable.Rows)
    dtCloned.ImportRow(row);

but I need a conversion step to convert the byte[] into a hex string. What is the best and preferably most efficent way to achieve what I want?

Thanks for your time.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T06:58:56+00:00Added an answer on June 12, 2026 at 6:58 am

    This how I did this in the end.

    public static void PostProcessData(ref DataTable dataTable)
    {
        // Convert byte[] columns.
        List<DataColumn> colCollRem = new List<DataColumn>();
        List<DataColumn> colCollAdd = new List<DataColumn>();
        foreach(DataColumn col in dataTable.Columns)
            if (col.DataType == typeof(byte[]))
                colCollRem.Add(col);
    
        // Remove old add new.
        foreach (DataColumn col in colCollRem)
        {
            int tmpOrd = col.Ordinal;
            string colName = String.Format("{0}(Hex)", col.ColumnName);
            DataColumn tmpCol = new DataColumn(colName, typeof(String));
            dataTable.Columns.Add(tmpCol);
            colCollAdd.Add(tmpCol);
            foreach (DataRow row in dataTable.Rows)
                row[tmpCol] = Utilities.ByteArrayToHexString((byte[])row[col]);
            dataTable.Columns.Remove(col);
            string colNameNew = colName.Replace("(Hex)", String.Empty);
            dataTable.Columns[colName].ColumnName = colNameNew;
            dataTable.Columns[colNameNew].SetOrdinal(tmpOrd);
        }
    }
    

    Using this conversion

    public static string ByteArrayToHexString(byte[] p)
    {
        byte b;
        char[] c = new char[p.Length * 2 + 2];
        c[0] = '0'; c[1] = 'x';
        for (int y = 0, x = 2; y < p.Length; ++y, ++x)
        {
            b = ((byte)(p[y] >> 4));
            c[x] = (char)(b > 9 ? b + 0x37 : b + 0x30);
            b = ((byte)(p[y] & 0xF));
            c[++x] = (char)(b > 9 ? b + 0x37 : b + 0x30);
        }
        return new string(c);
    }
    

    I hope this helps someone else.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following SQL query: SELECT DISTINCT ProductNumber, PageNumber FROM table I am
I currently have the following SQL query that lists all the names of all
I have a following problem. I have a ListView which returns data from SQL
I have an SQL query that returns all companies records ignore the ones with
I have to create an SQL Query to get all rows starting with a
I have an SQL db with about 200,000 words. I need a query which
All, I have the following table called Foo which is the output of a
I have a sql file generated by mysqldump --all-databases . There are many databases
I have an sql statement that currently is just returning all the end parent
I have a SQL Server 2000 database with a column of type VARCHAR(255). All

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.