locked
populating drop down list using EF6 RRS feed

  • Question

  • User-962670764 posted

    I am trying to upgrade my site to asp.net.identity, but having problems almost every step. I have a simple drop down that just displays a list of states for the user to select from. There are several  post on how to do this.

    1. add dropdown to form w autopostback= true
    2. Add new class library and delete the class1.cs from lib
    3.  Add a new ADO.Net Entity Data model to the class library project. 
    4. name it appropriately (statelist.edmx)
    5. in Wizard select > Generate from Database and select States table
    6. finish Wizard.

    This creates the edmx file, a several other files context, diagram, etc, and lastly a state dataset that contains all of the column names. But nowhere is there a ctx.

    The code for the page that has the drop down is:

    try
      {
      if (!IsPostBack)
      {
      using (var ctx = new CompanyEmployeesEntities())
      {
                   
      var companies = from comp in ctx.Companies
          where comp.CompanyName.StartsWith("Ad")
          orderby comp.CompanyName
          select new { comp.CompanyID, comp.CompanyName };
     
         DropDownList1.DataValueField = "CompanyID";
         DropDownList1.DataTextField = "CompanyName";
         DropDownList1.DataSource = companies;
         DataBind();
     
     
         }
     
       }
     }
     
            
     catch (Exception ex)
     {
                
     Response.Write(ex.Message);
     }

    Since the ctx is not there the code errors I have had the same problem with two posts.

    I am using VS2015, EF6 and aspnet.identity. I think the code in the posts may be EF4?

     

    Monday, March 13, 2017 4:25 AM

All replies

  • User-1509636757 posted

    Since the ctx is not there the code errors I have had the same problem with two posts

    From the code above that you have posted, is not apparent what issue you are facing, can you elaborate?

    Monday, March 13, 2017 5:46 AM
  • User-271186128 posted

    Hi dieseldave,

    Do you have the error when you bind data to the drop down list?
    The type of companies is store query. Data binding directly to a store query (DbSet, DbQuery, DbSqlQuery, DbRawSqlQuery) is not supported.
    You could try to use ToArray() or ToList() Method on the query to get the result.
    Then bind it to the drop down list.
    For example:

    if (!IsPostBack)
                {
    
                    using (var ctx = new CompanyEmployeesEntities())
                    {
    
                        var companies = from comp in ctx.Companies
                                        where comp.CompanyName.StartsWith("Ad")
                                        orderby comp.CompanyName
                                        select new { comp.CompanyID, comp.CompanyName };
    
                        DropDownList1.DataValueField = "CompanyID";
                        DropDownList1.DataTextField = "CompanyName";
                        DropDownList1.DataSource = companies.ToList();
                        DataBind();
    
                    }
                  }
                }
    
    

    Best regards,
    Dillion

    Tuesday, March 14, 2017 6:34 AM