i am picking list of names from database, according to scenario each name exists many times in database, That's ok. Now i have filled a DROPDOWNLIST with those names but in drop down list each name appears several times but i want to display each name 1 time in DROPDOWN. I have used distict() but not working. I'm USING MVC 3, Linq to SQL.

EmployeeAtdDataContext DataContext = new EmployeeAtdDataContext();
public ActionResult ddl()
        {
            var names = (from n in DataContext.EmployeeAtds select n).Distinct();
            ViewData["EmplID"] = new SelectList(names, "EmplID", "EmplName");
            return View();
        }
        public ActionResult showDDL(string EmplID)
        {
            ViewBag.EmplID = EmplID;
            return View();
        }

Views:

@{
    ViewBag.Title = "ddl";
}

<h2>ddl</h2>

    @using (Html.BeginForm("showDDL", "Home", FormMethod.Get))
    {
        <fieldset>

                Employers 

                 @Html.DropDownList("EmplID", "Select Name")

            <p>

                <input type="submit" value="Submit" />

            </p>

        </fieldset>



}

not a .net guy but distinct is only going to help you if all the values on the line are the same
not sure what the command would be but better off grouping on emplid

try running the query in SQL Sever Management Studio or the like, so that you can see the result set. You will see the same "EmplID" and "EmplName" on multiple rows. On those rows look for whats different, that's the reason "EmplID", "EmplName" show up multiple time...Solution: change your select to only return "EmplID", "EmplName"

if you try to add items in dropdownlist at page_load means , whenever the page loads the data will be added more time
if true..use

if(!Page.isPostback)
{
//To do for Adding items in dropdownlist box
}

You should use distinct keyword for employee name in the stored procedure itself. then from the result you can easily bind the data to your dropdown
For example:

select Distinct([Employee].Name) from [Employee]
<%--Use this line in your stored procedure --%>

then for binding to your dropdown you can use like the following.

dropdownlist1.datasource=Your Source;<%--from linq --%>
dropdownlist1.databind();

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.