locked
Bind data to dropdown list RRS feed

  • Question

  • User-1664485818 posted

    Hi folks, trying to bind data from a linq query to a dropdown list, but receiving the following error;

    DataBinding: '<>f__AnonymousType0`2[[System.Int32, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089],[System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]' does not contain a property with the name 'VehicleID'.

    My code explodes at  ddlModels.DataBind();

    private void PopulateGridview()
            { 
    
                if (!Page.IsPostBack)
                {
                    using (NormalisationTestEntities dbcontext = new NormalisationTestEntities())
                    {
                        ddlModels.DataSource = (from d in dbcontext.Model_View
                                                select new {d.VehilceID, d.ModelName }).Distinct().ToList();
    
                        ddlModels.DataValueField = "VehicleID";
                        ddlModels.DataTextField = "ModelName";
    
                        ddlModels.DataBind();
                
                     }

    Thursday, August 18, 2016 11:11 AM

Answers

  • User-1496088595 posted

    you are creating anonymous type result which doesn't have property with name that you are using, try with below code.

    ddlModels.DataSource = (from d in dbcontext.Model_View
                            select new 
                                      {
                                         VehilceID = d.VehilceID, 
                                         ModelName = d.ModelName 
                                      }).Distinct().ToList();

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, August 18, 2016 11:38 AM