Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9185861
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T19:22:51+00:00 2026-06-17T19:22:51+00:00

I am using MVC 3 and I am using a datareader to create a

  • 0

I am using MVC 3 and I am using a datareader to create a list of items that have subItems. When I add the subitem I get “Object reference not set to an instance of an object.” With the following code:

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

The models:

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

The whole code:
// what am I missing??

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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T19:22:53+00:00Added an answer on June 17, 2026 at 7:22 pm

    Your Lines property on your Questionaire instance is Null. Change to:

     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. stepping through your code would have shown you that.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a relatively simple ASP.net MVC 2 app that is using SubSonic. Everything
im using mvc framework and i have learned some techniques that help me with
Using MVC 3 I have a button that saves a mvc questionare form. The
Using MVC 3 I have a questionaire that has three sections. The page that
I have an MVC/Nhibernate app that is giving me the below. [WrongClassException: Object with
When using MVC Razor you often write @Url.Content(~/images/someimage.png) and you get intellisense on that
I am using MVC. I have a view model which contains a Business object.
I have my project using MVC and I've got my controller that instantiates a
I am using MVC, specifically Codeigniter. I have a need to create a sub
I am using MVC C# along with Jquery. I have a partial view that's

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.