0

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>



}
5
Contributors
4
Replies
22
Views
3 Years
Discussion Span
Last Post by sushmaja.arumalla.9
-1

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

0

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"

0

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
}

Edited by Sulaiman_1

0

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();

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.