DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 7 of 7

Threaded View

  1. #1
    Join Date
    Jan 2007

    Question VB.Net: Filling a ComboBox with data stored in database

    How to fill a combobox with a data stored in a table in sql server?....
    Can we use stored procedure to do so?

    I tryed this code but it keeps giving an error message:
    "An unhandled exception of type 'System.Data.SqlClient.SqlException' occurred in system.data.dll"
    What does that mean????

    My code:
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    Dim connString As String = "server=(local);database=AdvantEdge;trusted_connection=yes"
    Dim conn As New SqlConnection(connString) 
    ' fillComboBox1()
    Dim strSQL As String = "Select * From Disk"
    Dim DA As New SqlDataAdapter(strSQL, conn)
    Dim DS As New DataSet
    DA.Fill(DS, "Disk")
    'Create and populate the DataTable to bind to the ComboBox:
    Dim dt As New DataTable
    dt.Columns.Add("Disk_Name", GetType(System.String))
    dt.Columns.Add("Disk_Key", GetType(System.String))
    ' Populate the DataTable to bind to the Combobox.
    Dim drDSRow As DataRow
    Dim drNewRow As DataRow
    For Each drDSRow In DS.Tables("Disk").Rows()
    drNewRow = dt.NewRow()
    drNewRow("Disk_Name") = drDSRow("Disk_Name")
    drNewRow("Disk_Key") = drDSRow("Disk_Key")
    'Bind the DataTable to the ComboBox by setting the Combobox's DataSource property to the DataTable. To display the "Description" column in the Combobox's list, set the Combobox's DisplayMember property to the name of column. Likewise, to use the "Code" column as the value of an item in the Combobox set the ValueMember property. 
    ComboBox1.DropDownStyle = ComboBoxStyle.DropDownList
    With ComboBox1
    .DataSource = dt
    .DisplayMember = "Disk_Name"
    .ValueMember = "Disk_Key"
    .SelectedIndex = 0
    End With
    End Sub
    Last edited by Hack; 04-03-2009 at 07:53 AM. Reason: Added Code Tags

Similar Threads

  1. Replies: 2
    Last Post: 09-13-2008, 11:38 AM
  2. Telling if a database is stored locally or on a server
    By DrunkenMunk in forum VB Classic
    Replies: 4
    Last Post: 11-30-2005, 11:23 AM
  3. Replies: 6
    Last Post: 03-14-2001, 05:02 PM
  4. Replies: 7
    Last Post: 01-13-2001, 02:15 PM
  5. I am helpless - Edit Info form
    By Dan in forum VB Classic
    Replies: 0
    Last Post: 03-17-2000, 05:14 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center
Latest Articles
Questions? Contact us.
Web Development
Latest Tips
Open Source

   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center

We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.