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

Posted

技术标签:

【中文标题】使用 datareader 填充 mvc 模型时解决对象引用错误?【英文标题】:Troubleshoot object reference error when filling mvc models with datareader? 【发布时间】:2013-01-22 14:21:25 【问题描述】:

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

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】:

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

 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>();

    

顺便说一句。单步执行您的代码会向您展示这一点。

【讨论】:

谢谢,不知道为什么忽略这一点?粗糙的早晨:(

以上是关于使用 datareader 填充 mvc 模型时解决对象引用错误?的主要内容,如果未能解决你的问题,请参考以下文章

如何通过DataReader / DataTable进行查看?

使用 DataReader 填充嵌套集合

我应该如何测试从 DataReader 填充列表的方法?

使用 datareader 用数据库多列中的数据填充文本框

来自 JQuery 填充字段的值未绑定到 MVC 模型

根据 DataReader 数据填充对象数组