user7235054 user7235054 - 2 days ago 3
SQL Question

How can i get the selected column in C#, SQL

I have the following Code/SQL statement in my C# project but the select statement doesnt show the values what are in the column it only shows the column name why is this ?

my code

string Table = ListBox2.Text;
string column= listBox1.GetItemText(listBox1.SelectedItem);

foreach (object Tables in ListBox2.SelectedItems)
{
using (SqlDataAdapter SQLStatement = new SqlDataAdapter("SELECT '"+column+"' FROM " + table, ConnectionString))
{
DataTable dt= new DataTable();
SQLStatement.Fill(dt);
dataGridView1.DataSource = dt;
dataGridView1.Refresh();

}
}

Answer

If you use SELECT '"+column+"' FROM table_name,generated query will be something like this: SELECT "column" FROM table_name.

Please try this:

using (SqlDataAdapter SQLStatement = new SqlDataAdapter("SELECT "+column+" FROM " + table, ConnectionString))
            {
                DataTable dt= new DataTable();
                SQLStatement.Fill(dt);
                dataGridView1.DataSource = dt;
                dataGridView1.Refresh();

            }
Comments