Results 1 to 2 of 2

Thread: Error when trying to add combobox items, Help?

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jan 2012
    Posts
    144

    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

  2. #2
    Hyperactive Member DavesChillaxin's Avatar
    Join Date
    Mar 2011
    Location
    WNY
    Posts
    451

    Re: Error when trying to add combobox items, Help?

    Quote Originally Posted by rafter_01 View Post
    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:
    1. If comboDobYear.SelectedIndex <> (-1) AndAlso comboDobMonth.SelectedIndex <> (-1) AndAlso
    2. comboDobDay.SelectedIndex <> (-1) Then
    3.     newStudentClass.dob = CType(comboDobYear.SelectedItem & "-" & comboDobMonth.SelectedValue & "-" & comboDobDay.SelectedItem, Date)
    4. Else
    5.     'Show your message here that a combobox does not have selected item.
    6. 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&#37; convert back into a DateTime object.

    vbnet Code:
    1. If comboDobYear.SelectedIndex <> (-1) AndAlso comboDobMonth.SelectedIndex <> (-1) AndAlso
    2. comboDobDay.SelectedIndex <> (-1) Then
    3.     Dim result As DateTime
    4.  
    5.     'FixedDateFormat() would be you're function that returns a consistent date format.
    6.     DateTime.TryParse(FixedDateFormat(comboDobYear.SelectedItem,  comboDobMonth.SelectedValue, comboDobDay.SelectedItem), result)
    7.     If result <> DateTime.MinValue Then
    8.         newStudentClass.dob = result
    9.     End If
    10. Else
    11.     'Show your message here that a combobox does not have selected item.
    12. End If


    I'm also wondering what values you have in your combo boxes...
    Last edited by DavesChillaxin; Feb 9th, 2012 at 03:32 PM.
    Please rate if my post was helpful!
    Per favore e grazie!




    Code Bank:
    Advanced Algebra Class *Update | True Gradient Label Control *Dev | A Smarter TextBox *Update | Register Global HotKey *Update
    Media Library Beta *Dev | Mouse Tracker (Available in VB.net and C#.net) *New | On-Screen Numpad (VB.net) *New

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width