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

DropDownList

Scheduled Pinned Locked Moved ASP.NET
databasetutorialsql-serversysadmin
4 Posts 4 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.
  • M Offline
    M Offline
    Murugavel Sadagopan
    wrote on last edited by
    #1

    Hi, How to add database name list (from sql server) into the dropdown box. please guide me, Thanks, Murugavel

    S G W 3 Replies Last reply
    0
    • M Murugavel Sadagopan

      Hi, How to add database name list (from sql server) into the dropdown box. please guide me, Thanks, Murugavel

      S Offline
      S Offline
      subai
      wrote on last edited by
      #2

      did you search before you post ?

      I Wish the Life Had CTRL-Z Wizard's First Rule : People are fool,they believe what they want to believe or what they afraid to believe www.subaitech.blogspot.com

      1 Reply Last reply
      0
      • M Murugavel Sadagopan

        Hi, How to add database name list (from sql server) into the dropdown box. please guide me, Thanks, Murugavel

        G Offline
        G Offline
        Gayani Devapriya
        wrote on last edited by
        #3

        Hi, Im not sure for how depth u except the answer, nor whether you have a layered architecture in place. There are couples of ways of achiving this… Let’s assume that I have database named TestDB and it has a Employee table which has, EmployeeNumber, Name and some other fields. My objective is to get the EmployeeIDs and Names and bind them to the control. The stored procedure ill be using is just a simple one as Select EmployeeNumber, Name from Employee and I will call this sp as Employee_Select. Here is a quick way… • Let’s configure a DataSource to your DropDownList. When configuring a datasource, you need to select the database and connect to it and select the stored procedure you want to use to populate the data to the control. So here is the sample code for that. <asp:DropDownList ID="DropDownList1" runat="server" DataSourceID="dsTestSource" DataTextField="Name" DataValueField="EmployeeNumber"> </asp:DropDownList><asp:SqlDataSource ID="dsTestSource" runat="server" ConnectionString="<%$ ConnectionStrings:TestDBConnectionString %>" SelectCommand="Employee_Select" SelectCommandType="StoredProcedure"></asp:SqlDataSource> Now let’s do it in a structured way… 1. Get the data from the Database. a. Create a Sqlconnection object, and create a Sqlcommand and execute it agaist your database and use an adapter to fill the data to a DataTable. Ex: string sConnection = ConfigurationManager.ConnectionStrings["TestDBConnectionString"].ConnectionString; SqlConnection Con = new SqlConnection(sConnection); SqlCommand cmd = new SqlCommand(); cmd.Connection = Con; cmd.CommandType = CommandType.StoredProcedure; cmd.CommandText = "Employee_Select"; SqlDataAdapter adp = new SqlDataAdapter(cmd); DataTable dtValues = new DataTable(); adp.Fill(dtValues); 2. Bind it to the control. DropDownList1.DataSource = dtValues; DropDownList1.DataTextField = "Name"; DropDownList1.DataValueField = "EmployeeNumber"; DropDownList1.DataBind(); You may write the above code in a method and call it when you need to fill the control. Hope its clear to you.. Thx, Gayani

        modified on Saturday, July 5, 2008 4:26 AM

        1 Reply Last reply
        0
        • M Murugavel Sadagopan

          Hi, How to add database name list (from sql server) into the dropdown box. please guide me, Thanks, Murugavel

          W Offline
          W Offline
          Wasif Ehsan
          wrote on last edited by
          #4

          Execute the stored procedure sp_databases in master database. Then bind the returned result( say its a datatable of name dt) with the dropdown list like this: .DataTextField="Database_Name"; .DataValueField="Database_Name"; .DataSource=dt; .DataBind(); Regards, Wasif.

          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