You must Sign In to post a response.
  • Category: .NET

    Preventing duplicate values being added to a dropdown menu

    The forum thread has not been reviewed by Editors yet. Readers are advised to use their best judgement before accessing this thread.
    This thread will be reviewed shortly.
    If you think this thread contain inappropriate content, please report to webmaster.

    I'm trying to populate a dropdown menu with values from a sql database table however I can't work out how to stop duplicate values being added.

    I have created a datareader object which pulls the info out of the database then I use this datareader to populate the dropdown as follows:

    menuLanguages.DataSource = drLanguages

    menuLanguages.DataTextField = "language_name"
    menuLanguages.DataValueField = "language"

    But this of course then lists all the records in the datareader. How do I skip any values that have already appeared in the list once?
  • #7616
    hey stuart
    write a stored procedure or a select query to select the distinct values from the table and display it in the drop down. thats how u can eliminate the duplication r else for each and every value in datareader u need to check for the duplication . if i would have been in ur situation i would go for the stored procedure r a query.

    if u have any ?? feel free to contact

  • #7618
    Hi Stuart,

    Try the following :
    1.First, Create a class called Database.cs
    2.Next, include the following set of code within the Database{ } Class.

    public DataTable SelectDistinct(string TableName, DataTable SourceTable, string FieldName,string FieldName2)
    DataTable dt = new DataTable(TableName);
    dt.Columns.Add(FieldName2, SourceTable.Columns[FieldName2].DataType);
    object LastValue = null;
    object IndexValue = null;
    foreach (DataRow dr in SourceTable.Select("", FieldName))
    if ( LastValue == null || !(ColumnEqual(LastValue, dr[FieldName])) )
    LastValue = dr[FieldName];
    IndexValue = dr[FieldName2];
    dt.Rows.Add(new object[]{LastValue,IndexValue});
    return dt;

    private bool ColumnEqual(object A, object B)
    if ( A == DBNull.Value && B == DBNull.Value )
    return true;
    if ( A == DBNull.Value || B == DBNull.Value )
    return false;
    return ( A.Equals(B) );

    3.Finally, call the SelectDistinct() method from the required .aspx page and assign it to your DropDownList.

    Database db = new Database();
    myDropdownList.DataSource =db.SelectDistinct ("urTBLname",ursourceTBL,"urfield1","urfield2");
    myDropdownList.DataTextField = " urfield1";
    myDropdownList.DataValueField = " urfield2";

    The Codes are self explanatory.
    The methods : SelectDistinct() & ColumnEqual() helps us pick out the unique values.
    Run Your program.
    hope its working the way you want it now :-)
    - Anita Joseph

  • #7623
    I'll give them both a try

    Many thanks for the help guys!

  • This thread is locked for new responses. Please post your comments and questions as a separate thread.
    If required, refer to the URL of this page in your new post.