Dropdown list with duplicates

Last post 09-26-2006 11:34 AM by PeterBrunone. 6 replies.

Sort Posts:

  • Dropdown list with duplicates

    05-23-2006, 10:05 PM
    • Loading...
    • Bidler
    • Joined on 05-24-2006, 1:47 AM
    • Posts 5

    I have a dropdownlist with duplicate values that I want to eliminate.  The datasource for the list is a complex stored procedure.  In the stored procedure I use select distinct.  When I run the procedure and enter the parameter values from within the server explorer the list does not contain duplicates.  When I select the dropdown from the webform it contains the duplicates.  Any suggestions on how to resolve this?  I would really appreciate some help as I have spent a number of hours trying to resolve it.

     

    Joel
  • Re: Dropdown list with duplicates

    05-24-2006, 10:48 AM
    • Loading...
    • wannesh
    • Joined on 08-17-2005, 4:10 PM
    • Posts 132

    if you populate your DDL in your sub page_load

    try putting your code between

     If not Page.IsPostBack Then

    .....

    end if

  • Re: Dropdown list with duplicates

    05-25-2006, 10:01 PM
    • Loading...
    • Bidler
    • Joined on 05-24-2006, 1:47 AM
    • Posts 5

    Thanks for the reply wannesh, but the code is already in the

    if not Page.IsPostBack

    ...

    endif

    Joel
  • Re: Dropdown list with duplicates

    09-14-2006, 11:22 AM
    • Loading...
    • KD34XBR960
    • Joined on 09-14-2006, 2:51 PM
    • Posts 2

    I needed to remove duplicate years from a DropDownList and so I created the following class in C#:

    public class Ddl

    {

        public Ddl()

        {

        }

        public static void RemoveDuplicateItems(DropDownList ddl)

        {

            for (int i = 0; i < ddl.Items.Count; i++)

            {

                ddl.SelectedIndex = i;

                string year = ddl.SelectedItem.ToString();

                for (int counter = i + 1; counter < ddl.Items.Count; counter++)

                {

                    ddl.SelectedIndex = counter;

                    string compareYear = ddl.SelectedItem.ToString();

                    if (year == compareYear)

                    {

                        ddl.Items.RemoveAt(counter);

                        counter = counter - 1;

                    }

                }

            }

        }

    }

    After I created the class, I placed the file in my project's App_Code folder. I then used the following code to call the method:

    Ddl.RemoveDuplicateItems(ddlYear);

    In this example, ddlYear is the name of the dropdown.

    Filed under: ,
  • Re: Dropdown list with duplicates

    09-15-2006, 12:19 AM

    A better solution

    select the distinct years from the database and display it in the dropdownlist

     

    Jai Ganesh. J , GSD ,India

    Please Mark As Answer If my reply helped you.
  • Re: Dropdown list with duplicates

    09-25-2006, 11:43 PM
    • Loading...
    • edwardwong
    • Joined on 07-01-2006, 2:13 PM
    • Posts 10

    Hi, can anyone help to correct the below codes, below are the DropDownList, SQL dataSource and GridView. What I want was when selected the item in the dropDownList, the gridView will displays the selected data. But somehow, some error occurred and can't display the data. Can anyonehHelp?

     

     

    <%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %>

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

    <html xmlns="http://www.w3.org/1999/xhtml" >

    <head runat="server">

        <title>Untitled Page</title>

        <script runat="server">

        Sub Get_Book_Type (Src As Object, Args As EventArgs)

                Dim SQLString As String

                SQLString = "SELECT BookID, BookTitle, BookPrice, BookQty FROM Books " & _

                            "WHERE BookType = '" & TypeList.SelectedValue & "' " & _

                            "ORDER BY BookID"

                BookSource2.SelectCommand = SQLString

            End Sub

    </SCRIPT>

    </head>

    <body>

        <form id="Form1" Runat="Server">

    <h3>View Book Types</h3>

    <asp:SqlDataSource ID="BookSource1" runat="server"

      ConnectionString="<%$ ConnectionStrings:wongConnectionString1 %>"

      ProviderName="<%$ ConnectionStrings:wongConnectionString1.ProviderName %>"

      SelectCommand="SELECT [BookID], [BookTitle], [BookPrice], [BookQty], [BookType] FROM [Books]">

      </asp:SqlDataSource>

    <asp:DropDownList id="TypeList" Runat="Server"

      DataSourceID="BookSource1"

      DataTextField="BookType"

      DataValueField="BookType"

      AutoPostBack="True"

      OnSelectedIndexChanged="Get_Book_Type"/><br/><br/>

    <asp:SqlDataSource ID="BookSource2" runat="server"

      ConnectionString="<%$ ConnectionStrings:wongConnectionString1 %>"

      ProviderName="<%$ ConnectionStrings:wongConnectionString1.ProviderName %>"

      SelectCommand="SELECT [BookID], [BookTitle], [BookPrice], [BookQty]FROM [Books]WHERE [BookType]=@BookType">

      </asp:SqlDataSource>

    <asp:GridView id="BookGrid" DataSourceID="BookSource2" Runat="Server"/>

            <br />

    </form>

    </body>

    </html>

  • Re: Dropdown list with duplicates

    09-26-2006, 11:34 AM
    • Loading...
    • PeterBrunone
    • Joined on 06-19-2002, 9:15 AM
    • I'm standing behind you.
    • Posts 3,663
    • TrustedFriends-MVPs

        It's really not a good idea to post unrelated questions in old threads.  If you have a new question, start a new thread and then more people will see your problem and try to help.

        Also, "some error occurred" is not nearly as helpful as "I saw this error message ______ and it pointed to this line _____ in my code".

    Cheers,
     

    Peter Brunone
    MS MVP, ASP.NET
    Founder, EasyListBox.com
    Do the impossible, and go home early.
Page 1 of 1 (7 items)
Microsoft Communities
Page view counter