Dropdownlist Datasource by LINQ Query

asp.net c# entity-framework linq

Question

I don't have any clue why ddlCity is not populated when the value of ddlMedicalName is changed. I am just trying to build the function "ddlMedicalName_SelectedIndexChanged" so that if someone choose any MedicalName from the dropdownlist ddlMedicalName, another dropdownlist "ddlCity" datasource should be populated according to the selectedvalue of ddlMedicalName.

Code behind:

  protected void ddlMedicalName_OnSelectedIndexChanged(object sender, EventArgs e)
    {
        //string entityString = ConfigurationManager.ConnectionStrings["medicaldb2Entities"].ConnectionString;
        //EntityDataSource eds = new EntityDataSource();
        using (Entity.medicaldbEntities context = new Entity.medicaldbEntities())
        {
            string selected = ddlMedicalName.SelectedItem.Value;
            int slct = Convert.ToInt32(selected);

            var orders = from order in context.Cities
                         where order.CityID == slct
                         select new { order.CityID, order.CityName };

            var lstSrc = orders.ToList();

            ddlCity.DataSource = lstSrc;
            ddlCity.DataTextField = "CityName";
            ddlCity.DataValueField = "CityID";

            ddlCity.DataBind();
        }

    }

ASPX:

<tr id="SearchDropDown">

 <asp:EntityDataSource ID="EntityDataSource1" runat="server" 
    ConnectionString="name=medicaldbEntities"  
    DefaultContainerName="medicaldbEntities" EnableFlattening="False" 
    EntitySetName="Medicals" Select="it.[medicalName],it.[CityFK]">
 </asp:EntityDataSource>

<td>
<asp:DropDownList ID="ddlMedicalName" runat="server" AutoPostBack="false"
OnSelectedIndexChanged="ddlMedicalName_OnSelectedIndexChanged"
    DataSourceID="EntityDataSource1" DataTextField="MedicalName"
    DataValueField="CityFK">
</asp:DropDownList>
</td>
<td>
<asp:DropDownList ID="ddlRegion" runat="server" AutoPostBack="true"
OnSelectedIndexChanged="ddlRegion_SelectedIndexChanged"
    DataSourceID="EntityDataSource1" DataTextField="MedicalName" 
    DataValueField="MedicalName">
</asp:DropDownList>
</td>
<td>
<asp:DropDownList ID="ddlCity" runat="server" AutoPostBack="false"
OnSelectedIndexChanged="ddlCity_SelectedIndexChanged"
     DataTextField="CityID" 
    DataValueField="CityID">
</asp:DropDownList>
</td>
<td>
<asp:Button ID="SearchButton" runat="server" Text="Suche" onclick="SearchButton_Click" />
</td>
</tr>

And thats the error I get:

enter image description here

If I change it to:

List<string> lstSrc = orders.ToList();

It doesn't throw any error but it doesn't work either (ddlCity is not populated).

1
0
9/7/2013 5:31:58 PM

Popular Answer

You prepare query but never actually use it.

var orders = from order in context.Cities
             where order.CityID == slct
             select order.CityName;

List<Entity.City> lstSrc = orders.ToList();

Edit

Another thing is, you have to take more than just CityName from your query:

var orders = from order in context.Cities
             where order.CityID == slct
             select new { order.CityID, order.CityName };

It will return anonymous type with two properties: CityID and CityName. But because it's anonymous type, you have to use var when declaring your List<T> variable:

var lstSrc = orders.ToList();

or assign it directly to DataSource property:

ddlCity.DataSource = orders.ToList();
3
9/7/2013 4:48:26 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow