2

So I have the following ASP.NET code. Essentially, I'm trying to display an "exam" object. The exam consists of a few strings, then a nested collection of Questions. Questions have a nested collection of answers. When I run it, I receive an error that says: Unable to cast object of type 'System.Char' to type 'PracticeNet.DataEntities.Question'.

System.InvalidCastException: Unable to cast object of type 'System.Char' to type 'PracticeNet.DataEntities.Question'.

Line 15: <%#:Item.QuestionTitle %>

I have no idea why I'm receiving this error. I don't have any char types anywhere near the Question class. My only guess is that I'm somehow fundamentally misunderstanding the nested ListView object model. Can anyone shed any insight on this? Full Code is below.

<asp:Content ID="Content2" ContentPlaceHolderID="FeaturedContent" runat="server">
    <asp:FormView ID="examDetails" runat="server" ItemType="PracticeNet.DataEntities.Exam" SelectMethod="GetExam" RenderOuterTable="false">
        <ItemTemplate>
            <div>
                <h1><%#:Item.ExamTitle %></h1>
                <h2><%#:Item.Description %></h2>
            </div>
            <br />
            <asp:ListView ID="examQuestions" runat="server" ItemType="PracticeNet.DataEntities.Question" DataSource="<%#:Item.Questions %>">
                <ItemTemplate>
                    <div>
                        <h1><%#:Item.QuestionTitle %></h1>
                        <h2><%#:Item.QuestionText %></h2>
                    </div>
                    <br />
                    <asp:ListView ID="questionAnswers" runat="server" ItemType="PracticeNet.DataEntities.Answer" DataSource="<%#:Item.AnswerOptions %>">
                        <ItemTemplate>
                            <div>
                                <h1><%#:Item.AnswerText %></h1>
                            </div>
                        </ItemTemplate>
                    </asp:ListView>
                </ItemTemplate>
            </asp:ListView>
        </ItemTemplate>
    </asp:FormView>
</asp:Content>
<asp:Content ID="Content3" ContentPlaceHolderID="MainContent" runat="server">
</asp:Content>

Exam Class:

public class Exam
    {
        [Required]
        public Guid ExamId
        {
            get;
            set;
        }
        [Required, StringLength(512),Display(Name="Title")]
        public string ExamTitle
        {
            get;
            set;
        }
        [Required, StringLength(1024),Display(Name="Details")]
        public string Description
        {
            get;
            set;
        }

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

Question Class:

public class Question
    {
        public Question()
        {
            QuestionId = Guid.NewGuid();
            AnswerOptions = new List<Answer>();
        }
        [Key]
        public Guid QuestionId
        {
            get;
            set;
        }
        [Required, StringLength(120), Display(Name="Title")]
        public string QuestionTitle
        {
            get;
            set;
        }
        [Required,StringLength(255), Display(Name="Text")]
        public string QuestionText
        {
            get;
            set;
        }
        [Required]
        public virtual ICollection<Answer> AnswerOptions
        {
            get;
            set;
        }
    }

And finally, Answer Class:

public class Answer
    {
        public Answer()
        {
            AnswerId = Guid.NewGuid();
        }
        [Key]
        public Guid AnswerId
        {
            get;
            set;
        }
        [Required, StringLength(250), Display(Name="Answer")]
        public string AnswerText
        {
            get;
            set;
        }

    }
3
  • 1
    I believe you are using the wrong ASP tag for the DataSource field. It should be <%# Item.AnswerOptions %> you have : in there which is unnecessary. Commented Sep 27, 2013 at 2:52
  • 2
    Have you tried replacing <%#: with <%# that looks suspect? Commented Sep 27, 2013 at 5:58
  • 1
    Yes, replacing the <%#: with <%# seems to have done the trick. Thanks! Commented Sep 27, 2013 at 6:34

1 Answer 1

2

(since every question needs an answer in the form of an answer)

As already indicated by the comments of @James and @debracey your server tag is wrongly formulated:

Replace

<%#: Item.AnswerOptions %>

With

<%# Item.AnswerOptions %>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.