Wrox Programmer Forums
Go Back   Wrox Programmer Forums > .NET > Other .NET > ADO.NET
|
ADO.NET For discussion about ADO.NET.  Topics such as question regarding the System.Data namespace are appropriate.  Questions specific to a particular application should be posted in a forum specific to the application .
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ADO.NET section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old December 23rd, 2003, 01:34 PM
Authorized User
 
Join Date: Aug 2003
Posts: 22
Thanks: 0
Thanked 0 Times in 0 Posts
Default ComboBox: HELP! ;)

Now, this may or may not be possible. If not, break it to me gently, but here's what I want to do:

Can you set the value member of a combo box based off a data reader?

        Dim sqlGetReliefPersonnel As String = "SELECT SNo, SortName FROM Personnel WHERE SortName <> '' ORDER BY SortName"
        Dim cmdGetReliefPersonnel As SqlClient.SqlCommand
        cmdGetReliefPersonnel = New SqlClient.SqlCommand()
        cmdGetReliefPersonnel.Connection = dbConn
        cmdGetReliefPersonnel.CommandText = sqlGetReliefPersonnel
        Dim reliefPersonnelReader As SqlClient.SqlDataReader
        dbConn.open()
        reliefPersonnelReader = cmdGetReliefPersonnel.ExecuteReader
        Do While reliefPersonnelReader.Read()
            cboReliefPersonnel.DataSource = CType(reliefPersonnelReader("SortName"), IList)
        Loop
        dbConn.close()
        reliefPersonnelReader.Close()

If you can set the value member dynamically through a data reader, please tell me how and where in the code it would go. Thanks to anyone for their help!

 
Old December 29th, 2003, 10:44 AM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

You don't need to loop thru a datareader to get its data into a list type control. You only need to set the control's datasource to that datareader and DataBind() the control.

reliefPersonnelReader = cmdGetReliefPersonnel.ExecuteReader
<s>Do While reliefPersonnelReader.Read()
    cboReliefPersonnel.DataSource = CType(reliefPersonnelReader("SortName"), IList)
Loop</s>
cboReliefPersonnel.DataSource = reliefPersonnelReader
cboReliefPersonnel.DataBind()
dbConn.close()

There is a property called "DisplayMember" which allows you to set which datasource property (field) is displayed in the list.

Peter
------------------------------------------------------
Work smarter, not harder.





Similar Threads
Thread Thread Starter Forum Replies Last Post
combobox nagarajuballa Java GUI 0 August 13th, 2008 03:49 AM
Filling Combobox with value from other Combobox ayazhoda Access VBA 6 June 5th, 2007 04:58 AM
Combobox stepdev Excel VBA 1 May 22nd, 2006 02:50 PM
New to ComboBox adriant42 Excel VBA 3 August 23rd, 2004 12:32 PM
combobox HenkRog VS.NET 2002/2003 2 March 17th, 2004 01:07 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.