Why does this produce a nullexception?

pachjo

Well-known member
Joined
Dec 12, 2006
Messages
370
Programming Experience
10+
I am trying to assign to datetimepickers to new variables and am getting a null exception error when the last line below executes and don't know why?

VB.NET:
Dim intSelectedTabPage As Integer

Dim dtmDateTimePickerFrom As New DateTimePicker, dtmDateTimePickerTo As New DateTimePicker

intSelectedTabPage = Me.tbcMain.SelectedIndex

dtmDateTimePickerFrom = DirectCast(Me.Controls("dtmFromDate" & intSelectedTabPage), DateTimePicker)

dtmDateTimePickerTo = DirectCast(Me.Controls("dtmToDate" & intSelectedTabPage), DateTimePicker)

taTableAdapter.SelectCommand.Parameters.Add("@from_date", SqlDbType.DateTime).Value = dtmDateTimePickerFrom.Value

However when I did it this way originally it worked fine?

VB.NET:
taTableAdapter.SelectCommand.Parameters.Add("@from_date", SqlDbType.DateTime).Value = Me.dtmFromDate.Value


Any clues please?
 
You want a DateTimePicker on selected tabpage, but you search for the control in the forms Controls collection. The tabpage has its own Controls collection where any control on that page is. Try something like:
Me.tbcMain.SelectedTab.Controls("dtmFromDate" & int)
 
Aha, that would explain a lot!

I have been trying to get your example to work with no joy however and then it occurred to me that the datetimepickers were on the tab page but inside a panel!

So after lots of trial and error I got this

VB.NET:
dtmDateTimePickerFrom = DirectCast(Me.tbcMain.SelectedTab.Controls("panel1").Controls("dtmFromDate" & intSelectedTabPage), DateTimePicker)

and it works a treat, thanks a million for the help :)

I am now brain drained and am off to bed happy with my new knowledge!

Again...thanks ;)
 
Back
Top