Member 11865955 wrote:
Label[] lb1 = new Label[d];
...
for (int l = 1; l < d; l++)
{
lb1[l].Text = ds11.Tables[0].Rows[0][0].ToString();
}
You've created an array which can hold a certain number of
Label
objects. When you create an array, all of the elements are set to the default value for the array type, which in this case is
null
.
You then attempt to set a property on each element in the array. But you haven't changed the default value of any of the elements, so you're trying to set a property on a
null
reference.
You need to create a new
Label
instance within the loop, set its
Text
property, and then store it in the array:
for (int l = 1; l < d; l++)
{
Label label = new Label();
label.Text = ds11.Tables[0].Rows[0][0].ToString();
lb1[l] = label;
}
You could have easily identified this problem yourself by using the debugger to step through your code.