Q:

String was not recognized

I have 1 datagridview with 3 columns

Column1 = StartLoading
Column2 = FinishLoading
Column3 = TotalLoadingHours

If the user entered the date and time in StartLoading and FinishLoading Columns, the total hours from start to finish will show in TotalLoadingHours Column.
My problem is, If the user entered the date and time ONLY in StartLoading Column, there is always an error showing up - "String was not recognized as a valid DateTime".
Appreciate your help. Below is my code.
Dim StartLoading As New System.DateTime
Dim FinishLoading As New System.DateTime

    For x As Integer = 0 To LoadingStatusDataGridview.Rows.Count - 2

        Dim StartLoadingvalue As String = LoadingStatusDataGridview.Rows(x).Cells(1).Value.ToString()
        StartLoading = DateTime.Parse(StartLoadingvalue)

        Dim FinishLoadingvalue As String = LoadingStatusDataGridview.Rows(x).Cells(2).Value.ToString()
        FinishLoading = DateTime.Parse(FinishLoadingvalue)

        Dim TotalLoadingHours1 As TimeSpan = (FinishLoading - StartLoading)

        String.Format("{0:00}:{1:00}:{2:00}", TotalLoadingHours1.TotalHours, TotalLoadingHours1.Minutes, TotalLoadingHours1.Seconds)

        Dim TotalLoadingHours2 As TimeSpan = (DateTime.Now - StartLoading)

        String.Format("{0:00}:{1:00}:{2:00}", TotalLoadingHours2.TotalHours, TotalLoadingHours2.Minutes, TotalLoadingHours2.Seconds)

        If IsDBNull(LoadingStatusDataGridview.Rows(x).Cells(1).Value()) OrElse LoadingStatusDataGridview.Rows(x).Cells(1).Value() Is Nothing Then
            LoadingStatusDataGridview.Rows(x).Cells(3).Value() = Nothing

        ElseIf IsDBNull(LoadingStatusDataGridview.Rows(x).Cells(2).Value()) OrElse LoadingStatusDataGridview.Rows(x).Cells(2).Value() Is Nothing Then
            LoadingStatusDataGridview.Rows(x).Cells(3).Value() = TotalLoadingHours2
        Else
            LoadingStatusDataGridview.Rows(x).Cells(3).Value() = TotalLoadingHours1
        End If

    Next

End Sub

A:

Its working now using below code..
    Dim startloading As New System.DateTime
    Dim finishloading As New System.DateTime

    For x As Integer = 0 To LoadingStatusDataGridview.Rows.Count - 2

        Dim startloadingvalue As String = LoadingStatusDataGridview.Rows(x).Cells(1).Value.ToString()

        If Not IsDBNull(LoadingStatusDataGridview.Rows(x).Cells(1).Value) AndAlso LoadingStatusDataGridview.Rows(x).Cells(1).Value.ToString.Length <> 0 Then
            startloading = DateTime.Parse(startloadingvalue)
        End If

        Dim finishloadingvalue As String = LoadingStatusDataGridview.Rows(x).Cells(2).Value.ToString()

        If Not IsDBNull(LoadingStatusDataGridview.Rows(x).Cells(2).Value) AndAlso LoadingStatusDataGridview.Rows(x).Cells(2).Value.ToString.Length <> 0 Then
            finishloading = DateTime.Parse(finishloadingvalue)
        End If

        If IsDBNull(LoadingStatusDataGridview.Rows(x).Cells(1).Value()) OrElse LoadingStatusDataGridview.Rows(x).Cells(1).Value() Is Nothing OrElse LoadingStatusDataGridview.Rows(x).Cells(1).Value.ToString.Trim() = "" Then
            LoadingStatusDataGridview.Rows(x).Cells(3).Value() = Nothing
        End If

        If IsDBNull(LoadingStatusDataGridview.Rows(x).Cells(2).Value()) OrElse LoadingStatusDataGridview.Rows(x).Cells(2).Value() Is Nothing OrElse LoadingStatusDataGridview.Rows(x).Cells(2).Value.ToString.Trim() = "" Then
            Dim LoadingHours2 As TimeSpan = (DateTime.Now - startloading)

            String.Format("{0:00}:{1:00}:{2:00}", LoadingHours2.TotalHours, LoadingHours2.Minutes, LoadingHours2.Seconds)

            LoadingStatusDataGridview.Rows(x).Cells(3).Value() = LoadingHours2
        Else
            Dim LoadingHours1 As TimeSpan = (finishloading - startloading)

            String.Format("{0:00}:{1:00}:{2:00}", LoadingHours1.TotalHours, LoadingHours1.Minutes, LoadingHours1.Seconds)

            LoadingStatusDataGridview.Rows(x).Cells(3).Value() = LoadingHours1
        End If

    Next

