ComboBox.DataSource?
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8

Thread: ComboBox.DataSource?

  1. #1
    Join Date
    Aug 2001
    Location
    Minneapolis, MN, USA
    Posts
    150

    Question ComboBox.DataSource?

    I'm querying a db and I want to load in data into a ComboBox. Has anyone played around with this? It has to be real easy and similiar to throwing data into a DataGrid? My code is below.....


    MY CODE:
    Code:
    Private Sub frmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
       Dim dsV As DataSet
       dsV = LoadVendors()
       cboVendors.DataSource = dsV.Tables("VENDORS")
    End Sub
    
    Public Function LoadVendors() As DataSet
            Dim cnNODsql As New SqlConnection("server=serv;database=NOLCsql;uid=sa;pwd=;")
            Dim cmdSelect As New SqlCommand()
            Dim daVendors As New SqlDataAdapter()
            Dim dsVendors As New DataSet()
    
            Try
                With cmdSelect
                    .CommandText = "spLoadVendors" '"spSELECTUnassignedOrders" SearchTitlesByAuthorLastName"
                    .CommandType = System.Data.CommandType.StoredProcedure
                    .Connection = cnNODsql
                End With
    
                With daVendors
                    .SelectCommand = cmdSelect
                    .Fill(dsVendors, "VENDORS")
                End With
                LoadVendors = dsVendors
    
            Catch excSQL As SqlException
                ' Add exception handling
            End Try
    End Function

    thanks again.....

  2. #2
    Join Date
    May 2000
    Location
    New York, NY, USA
    Posts
    2,878
    Dim cnNorthwind As New SqlConnection("Data Source=localhost;Integrated
    Security=SSPI;Initial Catalog=northwind")
    Dim cmCustomers As New SqlCommand("uspS_Company", cnNorthwind)
    Dim daCustomers As New SqlDataAdapter(cmCustomers)
    Dim dsCustomers As New DataSet()
    Dim drCustomers As DataRow

    'fill DataSet
    daCustomers.Fill(dsCustomers, "Customers")

    'adding items with DataRow from DataSet
    For Each drCustomers In dsCustomers.Tables("Customers").Rows
    cbo.Items.Add(drCustomers("CompanyName"))
    Next



    Enjoy.
    Iouri Boutchkine
    iouri@hotsheet.NOSPAM.com

  3. #3
    Join Date
    Mar 2002
    Location
    NY
    Posts
    236
    cboVendors.DataSource = dsV.Tables("VENDORS")
    cboVendors.DisplayMember= dsV.Tables("VENDORS").Columns("WhateverYouWantToDisplay").Caption

  4. #4
    Join Date
    Aug 2001
    Location
    Minneapolis, MN, USA
    Posts
    150

    Talking

    UNBELIEVABLE! THANKS........

  5. #5
    Join Date
    Mar 2002
    Location
    NY
    Posts
    236
    Anytime

  6. #6
    Join Date
    Sep 2004
    Posts
    1

    Re: ComboBox.DataSource?

    Akim is correct, but do not use the 'Caption', instead use the 'ColumnName'.

    The 'Caption' is language specific, and may differ from the actual 'ColumnName' in your table on a localized Form.

    Also the 'Caption' may not be unique (at least is does not have to be) in your table, at which point the ComboBox will only ever display the first instance.

  7. #7
    Join Date
    Mar 2012
    Posts
    2

    Re: ComboBox.DataSource?

    Hi ,

    could u please give me the correct code.

    I have made as
    ComboBox1.DisplayMember = dsSeg.Tables("dsTBL").Columns("country_id").ColumnName

    where dstbl is table name.
    country_id is the column name..
    i get error as object reference set to null
    Please help
    thanks in advance

  8. #8
    Join Date
    Mar 2012
    Posts
    2

    Re: ComboBox.DataSource?

    to be clear of the doubt i ask


    I dont know how to call the combo box to fill up after getting the dataset assignment from loadvendors()

    Private Sub frmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    Dim dsV As DataSet
    dsV = LoadVendors()
    ???
    ???

    could u please explain how to do,and please give me explaination for both the lines of
    combobox.datasource
    combobox.displaymember

    this may be silly thing,but it s confusing me alot


    thanks in advance.
    waitin for ur sooner reply

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center