Dan272
Dan272

Reputation: 213

Error - cannot get value when using variable at datetimepicker name

I am using the following code,

I am getting error that timeStart is a null - it dont get its value.

no idea why is that...

here is the code:

for (int i = 1; i < 4; i++)
{
    DateTimePicker timeStart = (DateTimePicker)this.Controls["datetimeBi"+i];
    MessageBox.Show(timeStart.Value.ToString());
}

i have tried also

DateTimePicker timeStart = (DateTimePicker)this.Controls["datetimeBi"+i.ToString()];

but it didnt worked also..

getting error always at the showbox becouse of the "no value" (NULL).

Upvotes: 0

Views: 133

Answers (1)

S.N
S.N

Reputation: 5140

There is no issue with the first approach. I presume you are trying to iterate over few DataTimerPicker controls. If so, can you check the Name property of each DataTimerPicker control instances. I suppose it should be, datetimeBi1,datetimeBi2,datetimeBi3 ( just guessing after seeing your code).

Meantime, you can also check the immediate parent of your child controls ( DateTimePicker).

Upvotes: 1

Related Questions