populating dropdownlist
-
hi all,, plz let me know the query to populate a dropdownlist with the database names available in sqlserver....!
-
hi all,, plz let me know the query to populate a dropdownlist with the database names available in sqlserver....!
SqlConnection myConnection = new SqlConnection(connectionString); SqlDataAdapter myCommand = new SqlDataAdapter("select FirstName, LastName from NameList", myConnection); SqlDataReader dr; dr = myCommand.ExecuteReader(CommandBehavior.CloseConnection); while (dr.Read()) { myDropDownList.Items.Add(new ListItem((String)dr["FirstName"]+dr["LastName"],"")); } dr.Close();
Regards, Satips.
-
hi all,, plz let me know the query to populate a dropdownlist with the database names available in sqlserver....!
Hi, You can do this by connecting to the "master" database of the particular SQLServer and querying "sysdatabases" table. The following is the code sample. SqlConnection conn = new SqlConnection("Data Source=servername;Initial Catalog=master;User ID=uid;Password=pwd"); SqlCommand cmd = new SqlCommand("select dbid, name from sysdatabases"); cmd.Connection = conn; cmd.CommandType = CommandType.Text; conn.Open(); SqlDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection); DropDownList1.DataSource = dr; DropDownList1.DataTextField = "name"; DropDownList1.DataValueField = "dbid"; DropDownList1.DataBind(); dr.Close(); I hope this will solve your issue.
Thanks and Regards, Chetan Ranpariya
-
SqlConnection myConnection = new SqlConnection(connectionString); SqlDataAdapter myCommand = new SqlDataAdapter("select FirstName, LastName from NameList", myConnection); SqlDataReader dr; dr = myCommand.ExecuteReader(CommandBehavior.CloseConnection); while (dr.Read()) { myDropDownList.Items.Add(new ListItem((String)dr["FirstName"]+dr["LastName"],"")); } dr.Close();
Regards, Satips.
hello satips....u ahven't understand my questions my requirement, i want to populate a dropdownlist with all the database names .....like master,nothwind,.....(my own databses)....etc i don't want to select some columns from some table plz help me....!
-
Hi, You can do this by connecting to the "master" database of the particular SQLServer and querying "sysdatabases" table. The following is the code sample. SqlConnection conn = new SqlConnection("Data Source=servername;Initial Catalog=master;User ID=uid;Password=pwd"); SqlCommand cmd = new SqlCommand("select dbid, name from sysdatabases"); cmd.Connection = conn; cmd.CommandType = CommandType.Text; conn.Open(); SqlDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection); DropDownList1.DataSource = dr; DropDownList1.DataTextField = "name"; DropDownList1.DataValueField = "dbid"; DropDownList1.DataBind(); dr.Close(); I hope this will solve your issue.
Thanks and Regards, Chetan Ranpariya
thanqs chetan...this stuff worked for my requirement
-
hello satips....u ahven't understand my questions my requirement, i want to populate a dropdownlist with all the database names .....like master,nothwind,.....(my own databses)....etc i don't want to select some columns from some table plz help me....!
Sorry for the Mistake. Try how Chetan has stated i hope that will work.
Regards, Satips.
-
thanqs chetan...this stuff worked for my requirement
hello chetan....plz let me know the sql query for retrieving the user tables present in the selected database thanqs in advance..:rose:
-
hello chetan....plz let me know the sql query for retrieving the user tables present in the selected database thanqs in advance..:rose:
Hi, To get all the user tables for the selected database, u have to connect to that database and query its "sysobjects" table. "select name, id from SysObjects where Type = 'U'" This way you can get names of all the user tables of the selected database.
Thanks and Regards, Chetan Ranpariya