Error when trying to add combobox items, Help?
Hi All,
I'm trying to add three comboboxes DAY, MONTH, YEAR to the student database column DOB.
I'm using the code:
Code:
newStudentClass.dob = CType(comboDobYear.SelectedItem & "-" & comboDobMonth.SelectedValue & "-" & comboDobDay.SelectedItem, Date)
newStudentClass.dob is a function that adds the student DOB to the SQL student database.
This code comes under the Add Student button but if i select no values in the comboboxes and click ADD i get the following error:
Conversion from string "-1-" to type 'Date' is not valid.
why is that? Most likely my coding is completely wrong
What i want is a message informing me that comboboxes need to be selected and when i do select them it converts them to date and successfully adds the info to the database.
your help will be appreciated.
thanks
Re: Error when trying to add combobox items, Help?
Quote:
Originally Posted by
rafter_01
Hi All,
I'm trying to add three comboboxes DAY, MONTH, YEAR to the student database column DOB.
I'm using the code:
Code:
newStudentClass.dob = CType(comboDobYear.SelectedItem & "-" & comboDobMonth.SelectedValue & "-" & comboDobDay.SelectedItem, Date)
newStudentClass.dob is a function that adds the student DOB to the SQL student database.
This code comes under the Add Student button but if i select no values in the comboboxes and click ADD i get the following error:
Conversion from string "-1-" to type 'Date' is not valid.
why is that? Most likely my coding is completely wrong
What i want is a message informing me that comboboxes need to be selected and when i do select them it converts them to date and successfully adds the info to the database.
your help will be appreciated.
thanks
You should check to be sure your comboboxes, and all of them, have a valid selected index before processing your code...
vbnet Code:
If comboDobYear.SelectedIndex <> (-1) AndAlso comboDobMonth.SelectedIndex <> (-1) AndAlso
comboDobDay.SelectedIndex <> (-1) Then
newStudentClass.dob = CType(comboDobYear.SelectedItem & "-" & comboDobMonth.SelectedValue & "-" & comboDobDay.SelectedItem, Date)
Else
'Show your message here that a combobox does not have selected item.
End If
You could also make use of DateTime.TryParse(), if the object returned is nothing(DateTime.MinValue) then the string values are unable to be parsed into a date and can easily be checked.
Also I found dates sometimes are tricky to handle, since as your finding out parsing them into a DateTime object are very specific with the format... I would suggest what I've done. Which was to create a function that takes in the day, month and year integers and returns those in a format that can 100% convert back into a DateTime object.
vbnet Code:
If comboDobYear.SelectedIndex <> (-1) AndAlso comboDobMonth.SelectedIndex <> (-1) AndAlso
comboDobDay.SelectedIndex <> (-1) Then
Dim result As DateTime
'FixedDateFormat() would be you're function that returns a consistent date format.
DateTime.TryParse(FixedDateFormat(comboDobYear.SelectedItem, comboDobMonth.SelectedValue, comboDobDay.SelectedItem), result)
If result <> DateTime.MinValue Then
newStudentClass.dob = result
End If
Else
'Show your message here that a combobox does not have selected item.
End If
I'm also wondering what values you have in your combo boxes...