Question

Im trying to bind my drop down list, i think is beacuse my asp code on the front end is not bound somehow

aspx

        <asp:Label ID="Label2" runat="server" />
        <asp:DropDownList Width="150px" ID="ddLocation" runat="server" 
            AppendDataBoundItems="True"
            DataTextField="Name" 
            DataValueField="Name" AutoPostBack="True" >

        </asp:DropDownList>

c# code

protected void Page_Load(object sender, EventArgs e)
{
    if (!Page.IsPostBack)
    {
        Label0.Text = "Specialities";
        Label1.Text = "Category";
        Label2.Text = "Locations";
        ddSpec.Items.Insert(0, new ListItem("Select a spec", "0"));
        ddCategory.Items.Insert(0, new ListItem("Select a Category", "0"));
        ddLocation.Items.Insert(0, new ListItem("<Select a Location>", "0"));
        populattePage();

    }
}


public void populattePage()
{
     getlocation();
     // getCategory()
}


public static void getlocation()
{

   DataClasses_DataContext dc = new DataClasses_DataContext();

    List<string> locations = (
        from a
            in dc.Locations
        select a.Name).ToList();

    DropDownList ddLocation = new DropDownList();

    ddLocation.DataSource = locations;

    ddLocation.DataValueField = "ID";
    ddLocation.DataTextField = "Name";

    ddLocation.SelectedIndex = 0;
    ddLocation.DataBind();

}

I now have the Error """"DataBinding: 'System.String' does not contain a property with the name 'Name'.""" the code in the """page load""" class adds items to the drop downs however when i call the get locations class I get this error, Please help thanks in advance

No correct solution

OTHER TIPS

There are two issues here. First - if you are going to use 2 properties of the Location object, you should use this object as a data source. No point to extract separate list of strings for this:

ddLocation.DataSource = dc.Locations.ToList();

This will resolve your exception. Also this line:

DropDownList ddLocation = new DropDownList();

should not be here, just remove it. You already have your drop down initialized.

Second issue - if you want some default item to appear in the list, you should insert it after the data binding:

    populattePage();
    ddLocation.Items.Insert(0, new ListItem("<Select a Location>", "0"));

ListItems have the properties "Text" and "Value". So when you create one, you can only use those properties for the drop down list text/value fields. You need to change

<asp:DropDownList Width="150px" ID="ddLocation" runat="server" 
    AppendDataBoundItems="True"
    DataTextField="Text" 
    DataValueField="Value" AutoPostBack="True" >

</asp:DropDownList>

A simple way to get your location would be

List<ListItem> locations = (from a in dc.Locations
    select new ListItem()
    {
        Text = a.Name,
        Value = a.Name
    }).ToList();

Then append that to your list.

So the problem is the data source you're binding to is primarily string. When you set the DataValueField and DataTextField properties, it calls the reflector and asks the object its being bound to, to give it the property. Since I'm assuming the a.Name in the query is a string, it would not have a "Name" or "Id" property. A simple solution to this would be to create an ID and Name property.

This might help illustrate what I'm trying to say. (also please camel-case your function names if you plan to do C# it helps us maintenance programmers :))

public static void GetLocation()
{

DataClasses_DataContext dc = new DataClasses_DataContext();

var locations = (
    from a
        in dc.Locations
    select new { Name = a.Name, Id = a.Id }).ToList(); // this can be Id = a.Name or whatever too

DropDownList ddLocation = new DropDownList();

ddLocation.DataSource = locations;

ddLocation.DataValueField = "ID";
ddLocation.DataTextField = "Name";

ddLocation.SelectedIndex = 0;
ddLocation.DataBind();

}

Hope that helps!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top