0

I need to append values to a list of Class Category.

I have tried several combinations including appending 1 row at the time or append a range.

marticle.ListOfCategories.Add(crow); THiS DID NOT WORK
  // marticle.ListOfCategories(ccarticle); THiS DID NOT WORK

I get the following error:

Object reference not set to an instance of an object.

Here is my category class:

public class Category
{
    public int ID { get; set; }

    [Required]
    public string Title { get; set; }
}

And here is my view model

public class ArticleEditViewModel
    {
        public int ID { get; set; }

        [Required]
        public Boolean Active { get; set; }

        [Required]
        public string Title { get; set; }

        public List<Category> ListOfCategories { get; set; }
    }

And here is my recordset append(s) plural

 public ArticleEditViewModel ArticleSelectById(int? id)
        {
            ArticleEditViewModel marticle = new ArticleEditViewModel();
            string connString =
                System.Configuration.ConfigurationManager.ConnectionStrings["mydb"].ConnectionString;
            using (SqlConnection conn = new SqlConnection(connString))
            {
                conn.Open();
                using (SqlCommand cmd = new SqlCommand())
                {
                    cmd.Connection = conn;
                    cmd.CommandType = System.Data.CommandType.StoredProcedure;
                    cmd.CommandText = "content.Article_Select_ById";
                    cmd.Parameters.Add("@ID", SqlDbType.Int).Value = id;
                    using (SqlDataReader rdr = cmd.ExecuteReader())
                    {
                        while (rdr.Read())
                        {
                            if (rdr["ID"] != DBNull.Value)
                            {
                                marticle.ID = Convert.ToInt32(rdr["ID"]);
                            }
                            if (rdr["Title"] != DBNull.Value)
                            {
                                marticle.Title = rdr["Title"].ToString();
                            }                            
                        }
                    }
                }
                conn.Close();

                //Append the list of categories

                 List<Category> ccarticle = new List<Category>();
                conn.Open();
                using (SqlCommand cmd = new SqlCommand())
                {
                    cmd.Connection = conn;
                    cmd.CommandType = System.Data.CommandType.StoredProcedure;
                    cmd.CommandText = "dbo.Category_SelectAllActive";

                    using (SqlDataReader rdr = cmd.ExecuteReader())
                    {
                        while (rdr.Read())
                        {
                            Category crow = new Category();
                            if (rdr["ID"] != DBNull.Value)
                            {
                                crow.ID= Convert.ToInt32(rdr["ID"]);
                            }
                            if (rdr["Description"] != DBNull.Value)
                            {
                                crow.Description = rdr["Description"].ToString();
                            }
                            crow.Active = true;
                            ccarticle.Add(crow);
                            marticle.ListOfCategories.Add(crow); //THiS DID NOT WORK
                        }
                    }
                }
               // marticle.ListOfCategories(ccarticle); THiS DID NOT WORK
            }
            return marticle;
        }

1 Answer 1

2

you don't show what crow as defined as. Here is a short hand way of adding it

marticle.ListOfCategories.Add(new Category() { ID = YourID, Title = YourTitle });

just replace yourid and yourtitle with your database values. Since it is a list of Class you need to define an object of that type and add that to the list.

Edit:

before setting a list you need to initialize it

marticle.ListOfCategories = new List<Category>();

I would recommend setting this in a constructor in your class

public ArticleEditViewModel(){
    ListOfCategories = new List<Category>();
}

this way everytime you create a new instance of your viewmodel the list will be automatically initialized

Edit2:

the constructor goes in the class like this

public class ArticleEditViewModel
{
    public ArticleEditViewModel(){
        ListOfCategories = new List<Category>();
    }
    public int ID { get; set; }

    [Required]
    public Boolean Active { get; set; }

    [Required]
    public string Title { get; set; }

    public List<Category> ListOfCategories { get; set; }
}
Sign up to request clarification or add additional context in comments.

4 Comments

Sure I do. CROW is defined as Category crow = new Category();
I tried your solution and it gives me the same error: Object reference not set to an instance of an object.
There is one problem with this solution. Now ListOfCategories is not accessible in the view....
I put the constructor in your class so you can see what it looks like together. let me know if you have any questions.

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.