Question Need help regarding conversion errir

karthik82.vk

Member
Joined
Oct 31, 2012
Messages
8
Programming Experience
Beginner
Hi,

I am facing a conversion error in my code. Regarding the code explanation, i have two text box one with start date and end date. the end date needs to be populated automatically based on the start date value.i have a combo box with three levels namely Low, Medium and High. When I select Low in the combo box, the start date textbox will be populated with current date and time (format: 27-01-2013 17:56:02) and the duedate will be filled with 6 hrs from the start date. like same when i select medium 8 hrs will be added to the startdate and when i select High 14 hrs needs to be added with time.

My working hours starts from 09:00:00 and ends by 18:00:00 in the evening

If the enddate falls after my working time, then the off work time needs to be neglected.

say for example the level is medium and my startdate is 27/01/2013 16:58:18 then my enddate will be calculated as 28/01/2013 12:58:18. ie 8 hrs will get added from the start date.

below is the code for the sub procedure i am using.

HTML:
    Public Sub AddHrs()
        If frmInProgress.txtComplexity.Text = "Low" Then
            frmInProgress.txtDueDate.Text = Format(due6Hrs(frmInProgress.txtStartdate.Text), "dd-MM-yyyy HH:mm:ss")
        ElseIf frmInProgress.txtComplexity.Text = "Medium" Then
            frmInProgress.txtDueDate.Text = FormatDateTime(due8Hrs(frmInProgress.txtStartdate.Text), "dd-MM-yyyy HH:mm:ss")
        ElseIf frmInProgress.txtComplexity.Text = "High" Then
            frmInProgress.txtDueDate.Text = Format(due14Hrs(frmInProgress.txtStartdate.Text), "dd-MM-yyyy HH:mm:ss")
        End If
    End Sub
    Private Function due6Hrs(ByVal st As DateTime) As DateTime
        If st.Hour < 13 Then
            Return st.AddHours(6)
        Else
            Return st.AddHours(21)
        End If
    End Function
    Private Function due8Hrs(ByVal st As DateTime) As DateTime
        If st.Hour < 10 Then
            Return st.AddHours(8)
        Else
            Return st.AddHours(23)
        End If
    End Function
    Private Function due14Hrs(ByVal st As DateTime) As DateTime
        If st.Hour > 9 Then
            Return st.AddHours(28)
        End If
        Return st.AddHours(28)
    End Function

the above code is throwing conversion error stating "Conversion from string "27-01-2013 18:20:48" to type 'Date' is not valid." on the code

frmInProgress.txtDueDate.Text = FormatDateTime(due8Hrs(frmInProgress.txtStartdate.Text), "dd-MM-yyyy HH:mm:ss")

Can anyone please help me to over come this error?

Thanks in advance for your kindly help.

Regards,

Karthik Venkatraman
 
You're calling a method that expects a DateTime and you're passing it a String. You're assuming that it will be implicitly converted but it can't be. Implicit conversions are bad. You should turn Option Strict On so that they are not allowed and then make all your conversions explicit. In this case though, why make a conversion at all? If you want a DateTime then why not use a DateTimePicker?
 
You're calling a method that expects a DateTime and you're passing it a String. You're assuming that it will be implicitly converted but it can't be. Implicit conversions are bad. You should turn Option Strict On so that they are not allowed and then make all your conversions explicit. In this case though, why make a conversion at all? If you want a DateTime then why not use a DateTimePicker?

Hi,

I need the date and time to be automatically calculated from the txtstartdate.text. in this case how can i use the datetimepicker?
 
You use a DateTimePicker control instead of a TextBox. As the name suggests, a DateTimePicker is designed specifically for dates and/or times, so if that's the data you're working with then that's the control you should use.

If you're determined to use a TextBox then don't just assume that the system will convert the data for you. Explicitly convert your String to a DateTime. If you're using a specific format then specify that format when converting. The DateTime type has various methods for conversion, each designed for a different set of circumstances.
 
Back
Top