How can I solve this "System.Data.Entity.DynamicProxies" error

asp.net c# ef-code-first entity-framework linq

Question

I am new to Linq and EF Code First and came across an issue that I just can't figure out. I am developing a proof of concept examination program using ASP.Net Web Forms, EF Code First and Linq. The ideas is that I generate the page's server controls dynamically based on the database. I've built this same application before without EF but was hoping to learn that on this project. I can get the correct results if I use a literal but am receiving an error while databinding a RadioButtonList.

DataBinding: 'System.Data.Entity.DynamicProxies.Choice_EA448AD48C19F54FBB6BF09B7A03BA899DBE75EC189635A8982E7C3B1D8F4ABD' does not contain a property with the name '4'.

Line 34:                 content.Controls.Add(ql);
Line 35:                 ql.DataSource = choices;
Line 36:                 ql.DataBind();            
Line 37:         }

The property with the name of '4' is just a value from my database. I'm not sure if this problem is related to Lazy Loading or just lack of knowledge in proper Linq but I would really appreciate it if someone can point me in the right direction.

Here are my three Class files I am using and the C# page that I am trying to build. Keep in mind I plan to separate the data access into another class but was just trying to get a demo working for myself. Exam.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;

/// <summary>
/// Summary description for Exam
/// </summary>
public class Exam
{    
    public int ExamId { get; set; }

    public string Title { get; set; }

    public string Description { get; set; }

    public bool Status { get; set; }

    public DateTime DateCreated { get; set; }

    public string CreatedBy { get; set; }

    public virtual ICollection<Question> Questions { get; set; }
}

Question.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

/// <summary>
/// Summary description for Question
/// </summary>
public class Question
{
    public int QuestionId { get; set; }

    public string QuestionText { get; set; }

    public int Order { get; set; }

    public DateTime DateCreated { get; set; }

    public bool Status { get; set; }

    public string CreatedBy { get; set; }

    public string QuestionType { get; set; }

    public int ExamId { get; set; }

    public virtual Exam Exam { get; set; }

    public virtual ICollection<Choice> Choices { get; set; }
}

Choice.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

/// <summary>
/// Summary description for Choice
/// </summary>
public class Choice
{
    public int ChoiceId { get; set; }

    public string ChoiceText { get; set; }

    public int Order { get; set; }

    public bool Status { get; set; }

    public bool Correct { get; set; }

    public DateTime DateCreated { get; set; }

    public string CreatedBy { get; set; }

    public int QuestionId { get; set; }

    public virtual Question Question { get; set; }
}

Testing.aspx.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Testing : System.Web.UI.Page
{

    protected void Page_Load(object sender, EventArgs e)
    {
        ExamsContext db = new ExamsContext();

        var questions = (from c in db.Exams
                     from q in c.Questions
                     where c.ExamId == 1
                     select q).ToList();
        ContentPlaceHolder content = (ContentPlaceHolder)this.Master.FindControl("MainContent");
        foreach (var question in questions)
        {
            Literal questionLabel = new Literal();
            questionLabel.Text = Convert.ToString(question.QuestionId) + ".&nbsp;" + question.QuestionText + "<br/>";
            content.Controls.Add(questionLabel);

            var choices = (from c in question.Choices select c).ToList();

            RadioButtonList ql = new RadioButtonList();   
                foreach (var choice in choices)
                {
                    ql.DataValueField = Convert.ToString(choice.ChoiceId);
                    ql.DataTextField = choice.ChoiceText;
                }
                content.Controls.Add(ql);
                ql.DataSource = choices;
                ql.DataBind();            
        }        
    }
}
1
4
2/11/2015 11:15:29 PM

Accepted Answer

It's a while since I used ASP.NET WebForms, but I think your problem is here:

ql.DataValueField = Convert.ToString(choice.ChoiceId);
ql.DataTextField = choice.ChoiceText;

I think that Convert.ToString(choice.ChoiceId) is telling the control to use a property named "4".

Try this:

ql.DataValueField = "ChoiceId";
ql.DataTextField = "ChoiceText";

i.e. you need to specify the name of the property to use, not the value

2
2/12/2015 9:04:54 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow