Click here to Skip to main content
15,908,172 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi I want get database 2 table names into DropDownlist ad then fetch table column in the GridView on selection of Table name in DropDownList.

How to get table name in DropDownList?

Please help me

Thanks in advance
Posted
Updated 4-Dec-17 20:56pm

sqldataadapter da=new sqldataadapter("SELECT name FROM sys.tables","ConnectionString")
datatable dt=new datatable(); 
da.fill(dt); 

drp.datasource=dt; 
drp.DataValueField="name";
drp.DataTextField="name";
drp.databind();


Please give your Connection string in place of "ConnectionString"
 
Share this answer
 
v2
Comments
aarif moh shaikh 11-Oct-14 5:19am    
Good ... It will best answer..
Samatha Reddy G 15-Oct-14 0:37am    
thanks
use this query to get table names from database
SQL
SELECT table_name FROM INFORMATION_SCHEMA.TABLES ---gives all the table names

then bind to the drop down like
C#
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
SQL
SELECT * FROM INFORMATION_SCHEMA.columns where table_name='your table name'

then bind the grid.

C#
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();
}
 
Share this answer
 
v2
Comments
nishikant.K 8-Oct-14 8:56am    
Hi please tell me how to fetch multiple table name column in gridview from that DropDownlist selected table name
vangapally Naveen Kumar 8-Oct-14 9:02am    
TO GET THE COLUMN NAMES USE THIS QUERY

SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.columns where table_name='TEST'--HERE TEST IS TABLE NAME
Use this query

SQL
USE database_name
go
SELECT TableName FROM sys.tables


and bind the drop down list with the output
 
Share this answer
 
Below three sql commands return same results

SQL
USE [DB_NAME]
GO

select name from sys.tables
GO

select NAME AS TBL_NAME from sys.all_objects where type_desc='USER_TABLE' AND SCHEMA_ID!=4
GO

SELECT table_name FROM INFORMATION_SCHEMA.TABLES
GO
 
Share this answer
 
C#
sqldataadapter da=new sqldataadapter("select table_name as name from information_schema.tables","ConnectionString")
datatable dt=new datatable(); 
da.fill(dt); 
 
ddl.datasource=dt; 
ddl.DataValueField="name";
ddl.DataTextField="name";
ddl.databind();
 
Share this answer
 
v2
Comments
King Fisher 9-Oct-14 2:38am    
you select only table_name then how can you bind name,id to your Drop down.
Ainy Mughal 11-Oct-14 0:31am    
edited

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900