use this query to get table names from database
SELECT table_name FROM INFORMATION_SCHEMA.TABLES
then bind to the drop down like
using (SqlConnection con = new SqlConnection("Data Source=abc;Integrated Security=true;Initial Catalog=MySampleDB"))
{
con.Open();
SqlCommand cmd = new SqlCommand("SELECT table_name FROM INFORMATION_SCHEMA.TABLES", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
ddlabc.DataSource = ds;
ddlabc.DataTextField = "table_name";
ddlabc.DataValueField = "table_name";
ddlabc.DataBind();
con.Close();
}
get the columns use this query
SELECT * FROM INFORMATION_SCHEMA.columns where table_name='your table name'
then bind the grid.
using (SqlConnection con = new SqlConnection("Data Source=abc;Integrated Security=true;Initial Catalog=MySampleDB"))
{
con.Open();
SqlCommand cmd = new SqlCommand("SELECT * FROM INFORMATION_SCHEMA.columns where table_name='+ ddlabc.selecteditem.text +'", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
gridview1.DataSource = ds;
gridview1.DataBind();
con.Close();
}