Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. populating dropdownlist

populating dropdownlist

Scheduled Pinned Locked Moved ASP.NET
database
8 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Y Offline
    Y Offline
    yuvachandra
    wrote on last edited by
    #1

    hi all,, plz let me know the query to populate a dropdownlist with the database names available in sqlserver....!

    S C 2 Replies Last reply
    0
    • Y yuvachandra

      hi all,, plz let me know the query to populate a dropdownlist with the database names available in sqlserver....!

      S Offline
      S Offline
      Sathesh Sakthivel
      wrote on last edited by
      #2

      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.

      Y 1 Reply Last reply
      0
      • Y yuvachandra

        hi all,, plz let me know the query to populate a dropdownlist with the database names available in sqlserver....!

        C Offline
        C Offline
        Chetan Ranpariya
        wrote on last edited by
        #3

        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

        Y 1 Reply Last reply
        0
        • S Sathesh Sakthivel

          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.

          Y Offline
          Y Offline
          yuvachandra
          wrote on last edited by
          #4

          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....!

          S 1 Reply Last reply
          0
          • C Chetan Ranpariya

            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

            Y Offline
            Y Offline
            yuvachandra
            wrote on last edited by
            #5

            thanqs chetan...this stuff worked for my requirement

            Y 1 Reply Last reply
            0
            • Y yuvachandra

              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....!

              S Offline
              S Offline
              Sathesh Sakthivel
              wrote on last edited by
              #6

              Sorry for the Mistake. Try how Chetan has stated i hope that will work.

              Regards, Satips.

              1 Reply Last reply
              0
              • Y yuvachandra

                thanqs chetan...this stuff worked for my requirement

                Y Offline
                Y Offline
                yuvachandra
                wrote on last edited by
                #7

                hello chetan....plz let me know the sql query for retrieving the user tables present in the selected database thanqs in advance..:rose:

                C 1 Reply Last reply
                0
                • Y yuvachandra

                  hello chetan....plz let me know the sql query for retrieving the user tables present in the selected database thanqs in advance..:rose:

                  C Offline
                  C Offline
                  Chetan Ranpariya
                  wrote on last edited by
                  #8

                  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

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups