简体   繁体   中英

ADO.Net Data Binding Error to ASP DropDownList

Error Message:

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

Has something changed in VS2012 - I've used this same strategy before?

Data Layer code to retrieve data:

namespace DataLibrary
{
    public class DLgetMasterData
    {
        POLLINATORSEntities db = new POLLINATORSEntities();

        public IEnumerable<string> getSoil()
        {
            var soilList = from SOTYPE in db.SOTYPEs
                           orderby SOTYPE.SOTYPE_DESC ascending
                           select SOTYPE.SOTYPE_DESC;
            return soilList;
        }

Code behind page in UI:

   protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            try
            {
                BLgetMasterData obj = new BLgetMasterData();

                var soilList = obj.getSoil();
                Soil.DataSource = soilList;
                Soil.DataBind();
                Soil.Items.Insert(0, "Any");
                Soil.SelectedValue = "Any";

You can't bind to a query, which is usually some IQueryable (at least not anymore, not sure if you could in the past), so you need to bind to some local collection.

 var soilList = from SOTYPE in db.SOTYPEs orderby SOTYPE.SOTYPE_DESC ascending select SOTYPE.SOTYPE_DESC; 

soilList represents a query that will be sent to a database, so if you add ToList() at the end, that will create a local collection of the results for your query, and you will be able to bind to that.

See: Data binding directly to a store query (DbSet, DbQuery, DbSqlQuery) is not supported

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM