使用datareader填充mvc模型时解决对象引用错误?

时间:2013-01-22 14:21:26

标签: asp.net-mvc-3 model ado.net datareader

我正在使用MVC 3,我正在使用datareader来创建具有subItems的项目列表。当我添加子项时,我得到“对象引用未设置为对象的实例”。使用以下代码:

QuestionaireLine question = new QuestionaireLine();
         question.Question_ID = Convert.ToInt32(reader["Question_ID"]);
         question.Question_Answer = reader["Question_Answer"].ToString();
         ...etc..
         currentGroup.Lines.Add(question); //exception thrown here

模特:

 public class Questionaire
    {
        public int Question_Group_Id { get; set; }
        public string Question_Group_Name { get; set; }
        public int Question_Group_Indent { get; set; }
        public int Question_Group_Order { get; set; }
        public List<QuestionaireLine> Lines { get; set; }
    }



public class QuestionaireLine
    {
         public int Question_ID { get; set; }
         public string Question_Label { get; set; }
         public string Question_Answer { get; set; }
         public int Data_Type { get; set; }
         public int Control_Type { get; set; }
         public string Data_Choices { get; set; }
         public int Data_Max_Length { get; set; }
         public bool Issue_Tagged { get; set; }
         public int Question_Order { get; set; }
         public string NumberedQuestion
         {
             get { return String.Format("{0}. {1}", Question_Order, Question_Label); }
         }
    }

整个代码: //我错过了什么?

using (var conn = new SqlConnection(_connectionString))
            {
                List<Questionaire> groups = new List<Questionaire>();
                var com = new SqlCommand();
                com.Connection = conn;
                com.CommandType = CommandType.StoredProcedure;
                com.Parameters.Add(new SqlParameter
                {
                    ParameterName = "@Review_ID",
                    Value = reviewID
                });
                com.CommandText = "Review_Get_Question_Groups_Answers";

                conn.Open();

                // Get the reader
                SqlDataReader reader = com.ExecuteReader();

                // Process each result in the result set
                int currQuestionGroupId = 0;

                Questionaire currentGroup = null;
                while (reader.Read())
                {
                    var questionGroupId = Convert.ToInt32(reader["Question_Group_Id"]);
                    if (questionGroupId != currQuestionGroupId)
                    {
                        currQuestionGroupId = questionGroupId;
                        if (currentGroup != null)
                        {
                            groups.Add(currentGroup);   
                        }
                        currentGroup = new Questionaire();
                        currentGroup.Question_Group_Id = Convert.ToInt32(reader["Question_Group_Id"]);
                        currentGroup.Question_Group_Indent = Convert.ToInt32(reader["Question_Group_Indent"]);
                        currentGroup.Question_Group_Name = reader["Question_Group_Name"].ToString();
                        currentGroup.Question_Group_Order = Convert.ToInt32(reader["Question_Group_Order"]);
                    }
                    if (reader["Question_ID"] != DBNull.Value) 
                    {
                        QuestionaireLine question = new QuestionaireLine();
                        question.Question_ID = Convert.ToInt32(reader["Question_ID"]);
                        question.Question_Answer = reader["Question_Answer"].ToString();
                        question.Issue_Tagged = Convert.ToBoolean(reader["Issue_Tagged"]);
                        question.Data_Type = Convert.ToInt32(reader["Data_Type"]);
                        question.Data_Max_Length = Convert.ToInt32(reader["Data_Max_Length"]);
                        question.Data_Choices = reader["Data_Choices"].ToString();
                        question.Question_Label = reader["Question_Label"].ToString();
                        question.Question_Order = Convert.ToInt32(reader["Question_Order"]);
                        question.Control_Type = Convert.ToInt32(reader["Control_Type"]);
                        currentGroup.Lines.Add(question);
                    }
                    if (currentGroup != null)
                    {
                        groups.Add(currentGroup);
                    }
                }

                reader.Close();
                com.Dispose();
                return groups;
            }

1 个答案:

答案 0 :(得分:0)

Lines个实例上的Questionaire属性为空。改为:

 public class Questionaire
    {
        public int Question_Group_Id { get; set; }
        public string Question_Group_Name { get; set; }
        public int Question_Group_Indent { get; set; }
        public int Question_Group_Order { get; set; }
        public List<QuestionaireLine> Lines { get; set; }

        public Questionaire() {
            Lines = new List<QuestionaireLine>();

    }

b.t.w。单步执行代码会向您显示。