none
how to get column names based on particular table name selection using listbox? RRS feed

  • Question

  • i have two list boxes,

    listbox1 will display all the tables

    listbox2 have to display all the columns of a particular selected table

    string qryString1 = "select table_name,column_name from INFORMATION_SCHEMA.COLUMNS";

     if(table_lb.SelectedItem!=null)
                   {
                       if (table_lb.SelectedIndex > 0)
                       {
                           string str = table_lb.Text.ToString();
                           Console.WriteLine(str);

                           string tablename = str;
                           DataSet ds = new DataSet();
                           SqlConnection conSqlServer;
                           string connectionString = "Data Source=DEVSQLA1;Initial Catalog=Triton;Integrated Security=True";
                           conSqlServer = new SqlConnection(connectionString);
                           // string qryString1 = "SELECT [name] AS [Column Name] FROM syscolumns WHERE id = object_id()" + str;
                           string qryString1 = "select table_name,column_name from INFORMATION_SCHEMA.COLUMNS";
                           //"select column_name from INFORMATION_SCHEMA.COLUMNS where TABLE_NAME=" + str;
                           //"select table_name, column_name from INFORMATION_SCHEMA.COLUMNS";
                           //string qryString1 = "SELECT column_name FROM information_schema.columns where TABLE_NAME=" + str;
                           SqlCommand cmnd = new SqlCommand(qryString1, conSqlServer);
                           conSqlServer.Open();
                           SqlDataAdapter sd = new SqlDataAdapter(cmnd);
                          // column_lb.Items.Add(table_lb.Text.ToString());

                           sd.Fill(ds);
                           column_lb.DataSource = ds.Tables[0] ;
                           column_lb.MultiColumn = false;
                           column_lb.DisplayMember = "column_name";
                         
                           column_lb.ValueMember ="table_name";
                         // int i =  column_lb.FindString( str);
                           //Console.WriteLine(sd.Fill(ds, "columns"));
                          //  table_lb.Click += new EventHandler(table_lb_Click);

                           /*foreach (DataRow dr in ds.Tables[0].Rows)
                           {
                               MessageBox.Show(Convert.ToString(dr[0]));
                           }*/
                           //column_lb.DataSource = ds.Tables[0];
                           //column_lb.DisplayMember = "name";
                           Console.WriteLine("columns...............");
                       }
                        
                    }

    thanx in advance...........

    Wednesday, November 20, 2013 6:21 AM

Answers

  • Hi Mani Rockz,

    You have not told us what’s wrong with your code? So I write a code sample according your title.

    You could get the whole project in this page.  http://sdrv.ms/18pOe7p

    Hope useful to you.

    Best Regards,


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Thursday, November 21, 2013 6:42 AM
    Moderator