VB icon

Don't add the Duplicates to Listsboxs or DropDownLists

Email
Submitted on: 1/1/2015 12:11:00 PM
By: Joseph Chewning (from psc cd)  
Level: Beginner
User Rating: By 7 Users
Compatibility: VB.NET, ASP.NET
Views: 676
 
     This code will connect to an Oracle Database and automatically add items to a list control (i.e. DropDownList and Listboxes) and will not generate duplicates. You can modify the code to hit other types of databases. --Have Fun !--
 
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
//**************************************
// for :Don't add the Duplicates to Listsboxs or DropDownLists
//**************************************
'none
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
				
//**************************************
// Name: Don't add the Duplicates to Listsboxs or DropDownLists
// Description:This code will connect to an Oracle Database and automatically add items to a list control (i.e. DropDownList and Listboxes) and will not generate duplicates. You can modify the code to hit other types of databases. --Have Fun !--
// By: Joseph Chewning (from psc cd)
//
// Inputs:'none
//
// Returns:'none
//
// Assumes:need to point the connection string to your database. This is an Oracle connection so you may have to change the type of connection. Change the name of the control.
//
// Side Effects:'none
//**************************************

Sub LoadList()
yourlistcontrolname.Items.Clear() 'Want to make it fresh
Dim ds As New ListItem()
Dim cConnString As String = "Provider=MSDAORA;Data Source="need to add your source";User ID="add your user name";Password="add your password"" 'connection string to Oracle Database
Dim cn As New OleDbConnection(cConnString)
Dim Sql As String = "SELECT LOCATION, CHEMICAL_NAME FROM TC_INVENTORY"
Dim daCmd5 As New OleDbCommand(Sql, cn)
cn.Open()
Dim datinfo As OleDbDataReader = daCmd5.ExecuteReader() 'build the reader
While datinfo.Read() 'start looping through the records
ds.Text = datinfo(0) 'This will load the "LOCATION" part of the Sql Statement into the control
If yourlistcontrolname.Items.Contains(ds) = False Then 'if they are not equal add it
yourlistcontrolname.Items.Add(datinfo(0))
End If
End While
cn.Close()
End Sub


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Beginner category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments


 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.