JayBeeOH

ComboBox Load From Database Demo

Aug 9th, 2016
446
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 3.83 KB | None | 0 0
  1. '------------------------------------------------------------------------------------------
  2. '           Notice of My Copyright and Intellectual Property Rights
  3. '
  4. ' Any intellectual property contained within the program by Joseph L. Bolen remains the
  5. ' intellectual property of the Joseph L. Bolen. This means that no person may distribute,
  6. ' publish or provide such intellectual property to any other person or entity for any
  7. ' reason, commercial or otherwise, without the express written permission of Joseph L. Bolen.
  8. '
  9. '                 Copyright © 2016. All rights reserved.
  10. '        All trademarks remain the property of their respective owners.
  11. '-------------------------------------------------------------------------------------------
  12. ' Program Name:   ComboBox Load From Database Demo
  13. '
  14. ' Author:         Joseph L. Bolen
  15. ' Date Created:   09 AUG 2016
  16. '
  17. ' Description:    Show how to load ComboBox using DataReader (or DataAdapter).
  18. '
  19. '                 Documentation is at:
  20. '                   App's Visual Basic .NET code is at http://pastebin.com/Em4nrNx6
  21. '
  22. '-------------------------------------------------------------------------------------------
  23.  
  24. 'Note: Add Reference to project - System.Configuration and code connection string in the
  25. '      app.config file.
  26.  
  27. Imports System.Configuration
  28. Imports System.Data.SqlClient
  29.  
  30. Public Class MainForm
  31.  
  32.     Private Sub MainForm_Load(sender As Object, e As EventArgs) _
  33.         Handles MyBase.Load
  34.  
  35.         LoadComboBox()
  36.     End Sub
  37.  
  38.     Private Sub LoadComboBox()
  39.  
  40.         Dim conn As String = ConfigurationManager.ConnectionStrings("SimpleDB").ConnectionString
  41.  
  42.         ' Since FirstName is nullable in this table, the build of FullName expression examines
  43.         ' FirstName for null. See http://www.w3schools.com/sql/sql_isnull.asp for SQL NULL Functions.
  44.  
  45.         Dim query As String = "SELECT ContactId, LastName + ', ' + ISNULL(FirstName,'') as FullName " &
  46.                               "FROM Contacts " &
  47.                               "ORDER BY LastName, FirstName;"
  48.         Dim dt As New DataTable
  49.  
  50.         Try
  51.             Using con As New SqlConnection(conn)
  52.                 Using cmd As New SqlCommand(query, con)
  53.                     con.Open()
  54.                     Using rdr As SqlDataReader = cmd.ExecuteReader
  55.                         dt.Load(rdr)
  56.                     End Using
  57.                 End Using
  58.             End Using
  59.         Catch ex As Exception
  60.             Throw ex
  61.         End Try
  62.  
  63.         ' OR ...
  64.         ' Using a DataAdapter
  65.  
  66.         'Try
  67.         '    Using adapter As New SqlDataAdapter(query, conn)
  68.         '        adapter.Fill(dt)
  69.         '    End Using
  70.         'Catch ex As Exception
  71.         '    Throw ex
  72.         'End Try
  73.  
  74.         With ComboBox1
  75.             .IntegralHeight = False
  76.             .MaxDropDownItems = 8
  77.             .DataSource = dt
  78.             .DisplayMember = "FullName"
  79.             .ValueMember = "ContactId"
  80.             .SelectedIndex = -1
  81.         End With
  82.  
  83.         AddHandler ComboBox1.SelectedIndexChanged, AddressOf ComboBox1_SelectedIndexChanged
  84.     End Sub
  85.  
  86.     Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs)
  87.  
  88.         If ComboBox1.SelectedIndex <> -1 Then
  89.             TextBox1.Text = ComboBox1.GetItemText(ComboBox1.SelectedItem)
  90.  
  91.             ' To Retreive the data, use the SelectedValue of the combobox.
  92.             ' Dim id As Integer = CType(ComboBox1.SelectedValue, Integer)
  93.             ' DisplayContact(id)
  94.  
  95.             ComboBox1.SelectedIndex = -1
  96.         End If
  97.     End Sub
  98. End Class
  99.  
  100. ' Also, modify the app.config file to include:
  101.     <connectionStrings>
  102.         <add name="SimpleDB"
  103.             connectionString="Data Source=.\SQLEXPRESS;Initial Catalog=SimpleDB;Integrated Security=True;"
  104.             providerName="System.Data.SqlClient"/>
  105.     </connectionStrings>
Advertisement
Add Comment
Please, Sign In to add comment