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

  • Home
  • SEARCH
  • 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 7016441
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T22:47:07+00:00 2026-05-27T22:47:07+00:00

I am using VS2005 C# and SQL2005. I would like to retrieve data from

  • 0

I am using VS2005 C# and SQL2005.

I would like to retrieve data from multiple columns and merge them, separating them by comma.

E.g. Data in my SQL Table UserData:

No. | Username | Role 1 | Role 2 | Role 3 |

1 | Jimmy | USER | READONLY | WRITE |

Data I would like to display in my GridView GridView1:

No. | Username | Roles |

1 | Jimmy | USER, READONLY, WRITE |

How can I use a SELECT statement to merge data from the 3 columns and merge them, separating the data by commas and list them in the GridView?

Thank you


EDIT

I have tried using the method provided by Shark and Mun, but I would like to remove the commas if the data in a Role column is empty.

Situation now is that Roles column will look like this if Role 2 and Role 3 is empty:
USER,,

May I know how can I group , into the variable as well so it will not display if the value is empty?

  • 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-05-27T22:47:08+00:00Added an answer on May 27, 2026 at 10:47 pm

    Here is the SQL query:

    select
        username,
        isnull(nullif([role 1], '') + ', ', '') + 
        isnull(nullif([role 2], '') + ', ', '') + 
        isnull(nullif([role 3], ''), '') as Roles
    from UserData
    

    And then for your grid view code:

    SqlConnection conn = new SqlConnection(YourConnectionStringHere);
    SqlCommand cmd = new SqlCommand();
    cmd.Connection = conn;
    cmd.CommandText = 
        "select " +
        "    username, " +
        "    isnull(nullif([role 1], '') + ', ', '') + " +
        "    isnull(nullif([role 2], '') + ', ', '') + " +
        "    isnull(nullif([role 3], ''), '') as Roles " +
        "from UserData";
    
    SqlDataAdapter sda = new SqlDataAdapter(cmd);
    Datatable YourData = new DataTable();
    
    try
    {
        sda.Fill(YourData);
    
        GridView1.DataSource = YourData;
        GridView1.Bind();
    }
    catch
    {
        sda.Dispose();
        // handle your error
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using VS2005 and SQL Server 2005. I am trying to execute multiple
I am using VS2005 and SQL Server 2005. I am trying to execute multiple
I am using VS2005 and NUnit. I would like to know if there is
In vb.net (using vs2005), I'd like to see if www.domain.com/myfile.txt exists. Thanks in advance!
I am using VS2005 C# and SQL Server 2005. Currently I am able to
I am using VS2005 C# and SQL Server 2005. I am currently doing an
I am using VS2005 C# and SQL Server 2005. I have a SQL Query
Hi I am tring to import data from excel to sql 2005 .working fine
Pretty straightforward question. I'm building an app which reads data from a SQL Server
Using VS2005 and C#. Having a button in a form and an image from

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.