0

I am new to link and was following How to bind LINQ data to dropdownlist to try and bind my data to a drop down list using LINQ but I am getting the error "Data binding directly to a store query (DbSet, DbQuery, DbSqlQuery) is not supported. Instead populate a DbSet with data, for example by calling Load on the DbSet, and then bind to local data. For WPF bind to DbSet.Local. For WinForms bind to DbSet.Local.ToBindingList()." I followed the code example so i am not sure what I am doing wrong or how to fix it. Can anyone please adivse as this is day two of using LINQ.

            using (var db = new FullContext())
            {
                ddlItemType.DataSource = from t in db.ItemTypes
                                         select new { t.ID, t.Name };
                ddlItemType.DataTextField = "Name";
                ddlItemType.DataValueField = "ID";
                ddlItemType.DataBind();
            }
Community
  • 1
  • 1

3 Answers3

1

You are binding dropdown with query instead of data object. The statement you have return query but not the actual data. You need to bind it with data object for instance list.

Change query

ddlItemType.DataSource = from t in db.ItemTypes
     select new { t.ID, t.Name };

To data source

ddlItemType.DataSource =  (from t in db.ItemTypes
     select new { t.ID, t.Name }).ToList();
Adil
  • 139,325
  • 23
  • 196
  • 197
0

Can you try:

ddlItemType.DataSource = (from t in db.ItemTypes
                          select new { t.ID, t.Name }).ToList()
KaeL
  • 3,509
  • 2
  • 24
  • 52
0
public static void ListItem(DropDownList container) {
        DbDataContext db = new DbDataContext();
        var rs = from t in db.ItemTypes
                         select new { t.ID, t.Name };

        foreach (var r in rs)
            container.Items.Add(new ListItem(t.ID + " - " + t.Name, t.ID));
}

to call :

ListItem(YourDropdownListID);

Firma Agnes
  • 69
  • 1
  • 2
  • 8