0

I am new in this all,and I have problem with adding data to list.

For example I declare it like :

List<ColorL> Color   = new List<ColorL>(); 

Whero ColorL is

 public class ColorL
    {
        public int Color_No { get; set; }
        public string Color_Name { get; set; }
    }

Now I try:

Color[i].Color_No  = Convert.ToInt32(txtColorCode.Text);

Where i is some number.

Now i get NullReferenceException for this problem.

I think that this wold work for reading data, but I am not sure how to make insert.

Thanks

Frink
  • 221
  • 4
  • 23

1 Answers1

0

From error you have I can tell that you have null in i index of your Color list. Assign ColorL instance to index i of list. E.g. filling of list may look like

List<ColorL> Color = new List<ColorL>(); 
for(int i = 0; i < 10; i++)
   Color.Add(new ColorL());

That will create 10 new instances of ColorL and add them to list. Thus you will be able reference them by indexes 0..9.

Less probable reason is that your txtColorCode text box is not instantiated.

Sergey Berezovskiy
  • 232,247
  • 41
  • 429
  • 459