get column names from a table where one of the column name is a key word.

Posted by syedsaleemss on Stack Overflow See other posts from Stack Overflow or by syedsaleemss
Published on 2010-04-29T09:26:00Z Indexed on 2010/04/29 9:37 UTC
Read the original article Hit count: 385

Filed under:
|

Im using c# .net windows form application. I have created a database which has many tables. In one of the tables I have entered data. In this table I have 4 columns named key, name,age,value. Here the name "key" of the first column is a key word. Now I am trying to get these column names into a combo box. I am unable to get the name "key".

It works for "key" when I use this code:

private void comboseccolumn_SelectedIndexChanged(object sender, EventArgs e) { string dbname = combodatabase.SelectedItem.ToString(); string path = @"Data Source=" + textBox1.Text + ";Initial Catalog=" + dbname + ";Integrated Security=SSPI"; //string path=@"Data Source=SYED-PC\SQLEXPRESS;Initial Catalog=resources;Integrated Security=SSPI"; SqlConnection con = new SqlConnection(path); string tablename = comboBox2.SelectedItem.ToString(); //string query= "Select * from" +tablename+; //SqlDataAdapter adp = new SqlDataAdapter(" Select [Key] ,value from " + tablename, con); SqlDataAdapter adp = new SqlDataAdapter(" Select [" + combofirstcolumn.SelectedItem.ToString() + "]," + comboseccolumn.SelectedItem.ToString() + "\t from " + tablename, con); DataTable dt = new DataTable(); adp.Fill(dt);

        dataGridView1.DataSource = dt;
    }

This is beacuse I am using "[" in the select query. But it wont work for non keys. Or if I remove the "[" it is not working for key . Please suggest me so that I can get both key as well as nonkey column names.

© Stack Overflow or respective owner

Related posts about c#

Related posts about sql-server