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. General Programming
  3. Visual Basic
  4. Populate 2 DDL's with SQL Database Names

Populate 2 DDL's with SQL Database Names

Scheduled Pinned Locked Moved Visual Basic
databasesharepointgraphicsquestion
2 Posts 2 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.
  • P Offline
    P Offline
    paulray
    wrote on last edited by
    #1

    When I run this code, only one DDL (cboSDB) populates, the other remains empty. Code does not trip any errors. Am I missing something obvious? Thanks ****************** Using conn As New SqlConnection(connect) Try conn.Open() ' get DataTable with all available metadata collections Dim dt1 As DataTable = conn.GetSchema() Dim cmd As SqlCommand = New SqlCommand Dim dr As SqlDataReader cmd.CommandType = CommandType.StoredProcedure cmd.CommandText = "sp_databases" cmd.Connection = conn dr = cmd.ExecuteReader cboSDB.Items.Clear() cboSDB.Items.Add(New ListItem("Select", "0")) cboDDB.Items.Clear() cboDDB.Items.Add(New ListItem("Select", "0")) ' bind DataTable controls to display rows cboSDB.DataSource = dr cboSDB.DataTextField = "DATABASE_NAME" cboSDB.DataValueField = "DATABASE_NAME" cboSDB.DataBind() ' This is where it won't work yet code is exactly the same as above. cboDDB.DataSource = dr cboDDB.DataTextField = "DATABASE_NAME" cboDDB.DataValueField = "DATABASE_NAME" cboDDB.DataBind() ' End cmdSConn.Enabled = False Catch ex As System.Exception clsE.logWizError(ex.Message.ToString, "COULD NOT CONNECT TO SOURCE.", sClient) lblMsg.Text = "COULD NOT CONNECT TO DATABASE. PLEASE TRY AGAIN." lblMsg.ForeColor = Drawing.Color.DarkRed lblMsg.Visible = True Finally conn.Close() End Try End Using

    D 1 Reply Last reply
    0
    • P paulray

      When I run this code, only one DDL (cboSDB) populates, the other remains empty. Code does not trip any errors. Am I missing something obvious? Thanks ****************** Using conn As New SqlConnection(connect) Try conn.Open() ' get DataTable with all available metadata collections Dim dt1 As DataTable = conn.GetSchema() Dim cmd As SqlCommand = New SqlCommand Dim dr As SqlDataReader cmd.CommandType = CommandType.StoredProcedure cmd.CommandText = "sp_databases" cmd.Connection = conn dr = cmd.ExecuteReader cboSDB.Items.Clear() cboSDB.Items.Add(New ListItem("Select", "0")) cboDDB.Items.Clear() cboDDB.Items.Add(New ListItem("Select", "0")) ' bind DataTable controls to display rows cboSDB.DataSource = dr cboSDB.DataTextField = "DATABASE_NAME" cboSDB.DataValueField = "DATABASE_NAME" cboSDB.DataBind() ' This is where it won't work yet code is exactly the same as above. cboDDB.DataSource = dr cboDDB.DataTextField = "DATABASE_NAME" cboDDB.DataValueField = "DATABASE_NAME" cboDDB.DataBind() ' End cmdSConn.Enabled = False Catch ex As System.Exception clsE.logWizError(ex.Message.ToString, "COULD NOT CONNECT TO SOURCE.", sClient) lblMsg.Text = "COULD NOT CONNECT TO DATABASE. PLEASE TRY AGAIN." lblMsg.ForeColor = Drawing.Color.DarkRed lblMsg.Visible = True Finally conn.Close() End Try End Using

      D Offline
      D Offline
      Dave Kreskowiak
      wrote on last edited by
      #2

      You don't do databinding like this using a DataReader. A DataReader reads through the returned records forward only. Since during the first bind, you had the reader go through all the records once, there's no way to get it to go back and do it again for the second bind. Drop the DataReader and use a DataAdapter to fill a DataTable, then you can bind both ComboBox's (please don't use abbreviations like DDL!) to that DataTable.

      A guide to posting questions on CodeProject[^]
      Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
           2006, 2007

      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