Solved

Visual Studio 2008 Populate a Combo Box with Distinct Values

Posted on 2013-12-20
2
1,766 Views
Last Modified: 2013-12-21
I have a Visual Studio 2008 Form  

i have called frm_1

On the Form a have a Combo Box
i have called cbo_1

i have a dataset

i have called dataset1

 link to a Table

i have called the table tbl_1

the Table have 4 Collums : ID_Name / Name / City / State /

I want use the Combo Box to pass a parameter to a Query  that i want to develop

The query will  filter by State ( but this question do not involve the Query , it related on how populate the Combo Box )
So what is the code to fill the cbo_1 with the States listed into the tbl_1 ( linked to the dataset1???

so the cbo_1 should list the state ( as unique value ) i mean i want be able to
Have unique state list on cbo_1
0
Comment
Question by:sanlorenzo
2 Comments
 
LVL 62

Expert Comment

by:Fernando Soto
ID: 39732650
Hi sanlorenzo;

I am assuming that you have the latest SP for Visual Studio 2008. You can use Linq to Object to create a list to feed the ComboBox as follows.

Dim stateList = (From t in tbl_1.AsEnumerable()
                 Select t.Field(Of String)("States")).Distinct().ToList()
			           
cbo_1.DataSource = stateList

Open in new window

0
 
LVL 13

Accepted Solution

by:
Jesus Rodriguez earned 500 total points
ID: 39733042
'This is because more than one city can be on the same state and you're getting the max(ID)
'for the state regarding any city if not you have to create a table for the states by itself
'with ID and state_name

Dim Qry as String="SELECT MAX(ID_NAME) AS ID ,[STATE] FROM TABLE_NAME GROUP BY [STATE]"
DIM T AS NEW DATATABLE
DIM ADT AS New SqlDataAdapter(QRY,Your_ConnectionString)
T.CLEAR
ADT.FILL(T)
Dim RW as DataRow=t.NewRow
 RW("ID")=0
 RW("STATE")="Select State"
 T.Add(Rw)
With ComBobox1
 .DataValueMember="ID"
 .DisplayMember="STATE"
 .DataSource=T
 .DataBind()
 .SelecteItem=0
End With

Or the Qery for the Combobox Will Be
SELECT MAX(ID_NAME) AS ID ,[STATE] FROM TABLE_NAME GROUP BY [STATE]
0

Featured Post

Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Join & Write a Comment

Let's review the features of new SQL Server 2012 (Denali CTP3). It listed as below: PERCENT_RANK(): PERCENT_RANK() function will returns the percentage value of rank of the values among its group. PERCENT_RANK() function value always in be…
Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now