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. Add two field from database to combobox

Add two field from database to combobox

Scheduled Pinned Locked Moved Visual Basic
database
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.
  • I Offline
    I Offline
    ivo75
    wrote on last edited by
    #1

    Hi I use this code and want to add id and avtor to combobox but display the field "avtor", when I select some from combobox I want to get "id"

    Private Sub avtori()
    Dim selectdata As String
    selectdata = "SELECT id, avtor FROM avtor"
    Dim strConn As String
    Dim reader As OleDbDataReader
    'Низ за връзка с БД
    strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & strAcessFilePath1 & ";Jet OLEDB:Database Password=;"
    OleDbConnection2 = New OleDbConnection(strConn)
    OleDbConnection2.Open() 'Отваряне на връзка с БД
    OleDbCommand2 = New OleDbCommand(selectdata, OleDbConnection2)
    reader = OleDbCommand2.ExecuteReader()
    'Цикъл за извличане на всички автори и тяхното добавя към комбинирина списъчна кутия
    Dim i As Integer
    While reader.Read()
    ComboBox1.Items.Add(reader("avtor"))

        End While
        reader.Close()
        OleDbCommand2.Dispose()
        OleDbConnection2.Close()
    End Sub
    

    modified on Saturday, February 5, 2011 2:15 PM

    D 1 Reply Last reply
    0
    • I ivo75

      Hi I use this code and want to add id and avtor to combobox but display the field "avtor", when I select some from combobox I want to get "id"

      Private Sub avtori()
      Dim selectdata As String
      selectdata = "SELECT id, avtor FROM avtor"
      Dim strConn As String
      Dim reader As OleDbDataReader
      'Низ за връзка с БД
      strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & strAcessFilePath1 & ";Jet OLEDB:Database Password=;"
      OleDbConnection2 = New OleDbConnection(strConn)
      OleDbConnection2.Open() 'Отваряне на връзка с БД
      OleDbCommand2 = New OleDbCommand(selectdata, OleDbConnection2)
      reader = OleDbCommand2.ExecuteReader()
      'Цикъл за извличане на всички автори и тяхното добавя към комбинирина списъчна кутия
      Dim i As Integer
      While reader.Read()
      ComboBox1.Items.Add(reader("avtor"))

          End While
          reader.Close()
          OleDbCommand2.Dispose()
          OleDbConnection2.Close()
      End Sub
      

      modified on Saturday, February 5, 2011 2:15 PM

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

      Instead of going through a datareader and adding each item to the combobox, fill a datatable with the data, then bind the combobox to it using its DataSource property. Then set the combos DisplayMember (what the user sees in the list) and the ValueMember (what is returned by the combo) properties to the column names in the datatable.

      A guide to posting questions on CodeProject[^]
      Dave Kreskowiak

      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