That's not really enough information, so I must assume some things.

You can choose your three columns and concatenate them as a string into a List<string> then call .Distinct() on the list. //Linq namespace required.

Put all of the data into a genetric list, and then do some linq query with using Distinct() extension method.
One more thing, what exactly is your datasource?

Thank you for your reply,

I am using MS Sql Server 2008 database with C# .NET
My table structure is as below:

Table1

Col1 Col2 Col3 Col4
M1 --- A --- B --- C
M2 --- E --- A --- G
M3 --- B --- C --- A
M4 --- F --- D --- B

In dropdown, distinct values from Column - col1, col2 and col3 should be displayed as
--Select--
A
B
C
D
E
F
G

You can try it this way:

            List<string> values = new List<string>();
            using (SqlConnection conn = new SqlConnection())
            {
                using (SqlCommand cmd = new SqlCommand(@"SELECT Col1, Col2, Col3, Col4 FROM MyTable", conn)) //change your table name
                {
                    using (SqlDataReader reader = cmd.ExecuteReader())
                    {
                        while (reader.Read())
                        {
                            values.Add(reader[0].ToString());
                            values.Add(reader[1].ToString());
                            values.Add(reader[2].ToString());
                            values.Add(reader[3].ToString());
                        }
                    }
                }
            }
            List<string> distinctValues = values.Select(s => s).Distinct().ToList();
Comments
list error coming

I have used

List<string> values = new List<string>();
using (SqlConnection conn = new SqlConnection())
{
using (SqlCommand cmd = new SqlCommand(@"SELECT Col1, Col2, Col3, Col4 FROM MyTable", conn)) //change your table name
{
using (SqlDataReader reader = cmd.ExecuteReader())
{
while (reader.Read())
{
values.Add(reader[0].ToString());
values.Add(reader[1].ToString());
values.Add(reader[2].ToString());
values.Add(reader[3].ToString());
}
}
}
}
List<string> distinctValues = values.Select(s => s).Distinct().ToList();

// but here error like

Error 2 The type or namespace name 'List' could not be found (are you missing a using directive or an assembly reference?)

Error 3 The type or namespace name 'List' could not be found (are you missing a using directive or an assembly reference?)

Error 4 The type or namespace name 'List' could not be found (are you missing a using directive or an assembly reference?)

PLZ GIVE DIRECT COADING FOR DROPDOWN......
SO THAT I JST MAKE SOME CHANGES OF DROPDOWN ID AND THE PROJECT WORKS

Plz help soon , i wanna submitted my project soon

I m in project online shopping...
According to this i wanna fetch my features of a product in a 1dropdownlist with distinct values..
and i had stored my features in 5 different column of a table....

so can u help for this??

Edited 4 Years Ago by ravi.26jani

I mean web service, win form,...?
But my guess is you do a web application, since you dont have a generic list<T>.
What you can do, is to change it to String Array (T[]).

Example:

        string[] values = new string[0];
        using (SqlConnection conn = new SqlConnection())
        {
            using (SqlCommand cmd = new SqlCommand(@"SELECT Col1, Col2, Col3, Col4 FROM MyTable", conn)) //change your table name
            {
                using (SqlDataReader reader = cmd.ExecuteReader())
                {
                    while (reader.Read())
                    {
                        Array.Resize(ref values, values.Length + 4);
                        values[values.Length - 4] = reader[0].ToString();
                        values[values.Length - 3] = reader[0].ToString();
                        values[values.Length - 2] = reader[0].ToString();
                        values[values.Length - 1] = reader[0].ToString();
                    }
                }
            }
        }
        string[] distinctValues = values.Distinct().ToArray();

Mitja Bonca, in regards to your last post with code (post1792179), what's the benefit of using those nested using blocks? Can you not simply declare SqlCommand and SqlReader within the first using block? Thanks, signed a c# noob.

Use of using statement, is only means you dont have to explicitly declare the Dispose() method after ending.
It means that the object for example SqlCommand outside of using statement will be disposed (release of memory).

I had also tried
string[] values = new string[0];
using (SqlConnection conn = new SqlConnection())
{
using (SqlCommand cmd = new SqlCommand(@"SELECT Col1, Col2, Col3, Col4 FROM MyTable", conn)) //change your table name
{
using (SqlDataReader reader = cmd.ExecuteReader())
{
while (reader.Read())
{
Array.Resize(ref values, values.Length + 4);
values[values.Length - 4] = reader[0].ToString();
values[values.Length - 3] = reader[0].ToString();
values[values.Length - 2] = reader[0].ToString();
values[values.Length - 1] = reader[0].ToString();
}
}
}
}
string[] distinctValues = values.Distinct().ToArray();

// but at run time on
using (SqlDataReader reader = cmd.ExecuteReader())

an error such as::::
"ExecuteReader requires an open and available Connection. The connection's current state is closed."

though i had given connection on conn....

Ups, I forgot to open the connection, sorry, add it in here:

using (SqlCommand cmd = new SqlCommand(@"SELECT Col1, Col2, Col3, Col4 FROM MyTable", conn)) //change your table name
conn.Open(); //open it here
{
    using (SqlDataReader reader = cmd.ExecuteReader())
   {
      //and rest of code...

I Have tried this

public partial class Admin_Default3 : System.Web.UI.Page
{
SqlConnection conn = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory| \shopping.mdf;Integrated Security=True;User Instance=True");
SqlCommand cmd;
SqlDataReader reader;

protected void Page_Load(object sender, EventArgs e)
{
    string[] values = new string[0];

    using (SqlConnection conn = new SqlConnection())
    {
        string query = "SELECT touch_screen, bluetooth, digital_camera, gps FROM mobile_info";

        using (SqlCommand cmd = new SqlCommand(query, conn)) ;
        {
            conn.Open();

          using (SqlDataReader reader = cmd.ExecuteReader())
            {
                while (reader.Read())
                {
                    Array.Resize(ref values, values.Length + 4);
                    values[values.Length - 4] = reader[0].ToString();
                    values[values.Length - 3] = reader[0].ToString();
                    values[values.Length - 2] = reader[0].ToString();
                    values[values.Length - 1] = reader[0].ToString();
                }
            }
        }
    }
    string[] distinctValues = values.Distinct().ToArray()
    }
    }


    //
    but on ......conn.Open();
    error such as
    The ConnectionString property has not been initialized.




    also make some changes if i had mistaked in above coading....
    and
    where to make conn.close();
    and 
    how to place ending string in my dropdown..
   // DropDownList1.Text=distinctValues.ToString();
    i mean like this?
    OR in another way????

Edited 4 Years Ago by ravi.26jani

This article has been dead for over six months. Start a new discussion instead.