Click here to Skip to main content
15,893,401 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
How Can Remove Duplicate In The Com box



//Sub Category
try
{
DataTable dt9 = new DataTable();
//string CS9 = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;

{
string query9 = "select DISTINCT catName1 from tblcatProducts ";

SqlDataAdapter da9 = new SqlDataAdapter(query9, con2);
con2.Open();
DataSet ds9 = new DataSet();
da9.Fill(ds9, "tblcatProducts");
comboBoxSub1.DisplayMember = " catName1";
comboBoxSub1.ValueMember = "catId";
comboBoxSub1.DataSource = ds9.Tables["tblcatProducts"];
}
}
catch { }
Posted
Comments
Afzaal Ahmad Zeeshan 12-Jun-15 16:03pm    
Where is the duplicate?

Above code won't even compile, because C# requires you to append the (Exception) after catch keyword.
Dave Kreskowiak 12-Jun-15 16:07pm    
No it doesn't. A catch {} is perfectly legal. There's nothing that says you must use the exception in your catch code block.

In his code sample, it's pretty much crap code because he's eating the exception so there's no way to tell if a problem shows up unless you see something unexpected in the UI.
Afzaal Ahmad Zeeshan 12-Jun-15 16:10pm    
Perhaps then I was having an empty parenthesis which caused the problem. Stupid me!
Samira Radwan 12-Jun-15 16:38pm    
you select DISTINCT one column, there won't be duplicate values for sure!
Samira Radwan 12-Jun-15 16:40pm    
Could you please review your code, you select one column and when binding the combobox you display catName1 and combobox value is catId !!

1 solution

What's a "Com box"? I take it you're refering to a ComboBox?

You SQL code is already getting rid of duplicates, so you're going to have to define in precise terms what you mean by "duplicate" and provide examples.

Too bad you're SQL query doesn't return the catId column from your database. It looks like it would be pretty important to do so.

Also, your variable names suck ass. Use descriptive names, not this query9 crap. All you're doing is making your code harder to debug and maintain by doing what you're doing.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900