mctuna
mctuna

Reputation: 871

Dropdownlist Datasource by LINQ Query

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).

Upvotes: 0

Views: 8348

Answers (3)

mctuna
mctuna

Reputation: 871

My problem was pretty specific; at the main form tag of the master.site there was a warning. End tag of the form was already there but because of some not allowed controls or tags, it was not detected and didn't rendered and that caused the reason of that strange dropdownlist problem. Then I have edited my html and main form tag and got rid of that "Form1 has no end tag" warning and everything was ok again.

Upvotes: 0

senthilkumar2185
senthilkumar2185

Reputation: 2566

string selected = ddlMedicalName.SelectedItem.Value;
            int slct = Convert.ToInt32(selected);

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

            ddlCity.DataSource = orders.ToList();
            ddlCity.DataTextField = "CityName";
            ddlCity.DataValueField = "CityID";
            ddlCity.DataBind();

Check Autopostback="true"

<asp:Dropdownlist id="ddlMedicalName" runat="Server" Autopostback="true" ></Dropdownlist>

Upvotes: 1

MarcinJuraszek
MarcinJuraszek

Reputation: 125630

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

Upvotes: 3

Related Questions