Click here to Skip to main content
15,904,024 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, I have a column name (Time_IN, Time_OUT, Total) in sql database.
In time_IN, the stored value is String '07:00:00 AM" and in Time_OUT=Current Time or lblTime.text

I want to Add the two columns and insert into column total using vb.net.

Thanks in advance.

This is my code but I dont know how to select TimeIN from table TimeLog


VB
Dim dFrom As DateTime  
                Dim dTo As DateTime  
                Dim sDateFrom As String = 'I want to select value from time_IN
                Dim sDateTo As String = lblTime.Text 'Current Time  
                If DateTime.TryParse(sDateFrom, dFrom) AndAlso DateTime.TryParse(sDateTo, dTo) Then  
                    Dim TS As TimeSpan = dTo - dFrom  
                    Dim hour As Integer = TS.Hours  
                    Dim mins As Integer = TS.Minutes  
                    Dim secs As Integer = TS.Seconds  
                    Dim timeDiff As String = ((hour.ToString("00") & ":") + mins.ToString("00") & ":") + secs.ToString("00")  
                    txttotal.Text = timeDiff  
                End If


What I have tried:

VB
Dim dFrom As DateTime  
                Dim dTo As DateTime  
                Dim sDateFrom As String = lbltime.text 'current time
                Dim sDateTo As String = lblTime.Text 'Current Time  
                If DateTime.TryParse(sDateFrom, dFrom) AndAlso DateTime.TryParse(sDateTo, dTo) Then  
                    Dim TS As TimeSpan = dTo - dFrom  
                    Dim hour As Integer = TS.Hours  
                    Dim mins As Integer = TS.Minutes  
                    Dim secs As Integer = TS.Seconds  
                    Dim timeDiff As String = ((hour.ToString("00") & ":") + mins.ToString("00") & ":") + secs.ToString("00")  
                    txttotal.Text = timeDiff  
                End If


'no error but the result is 0:00:00
Posted
Updated 7-Jun-17 3:20am
Comments
Richard MacCutchan 1-Jun-17 8:00am    
Why are you storing times as strings, when SQL has specific types for dates and times?
Richard Deeming 1-Jun-17 9:11am    
Date and Time types[^]

Sounds like you want the time[^] type.
Maciej Los 1-Jun-17 15:49pm    
Please, provide sample data...

1 solution

You should use the correct data types for storing the data in sql but this will get you going for now

just change the strConnectionString for your own.


VB
Dim sDateFrom As String = Nothing
   Dim dFrom As DateTime
   Dim dTo As DateTime
   Dim sDateTo As String = lblTime.Text 'Current Time
   Dim connection As New SqlConnection(strConnectionString)
   Using connection
       Dim command As SqlCommand = New SqlCommand("select top 1 TimeIN from TimeLog;", connection)
       connection.Open()
       Dim reader As SqlDataReader = command.ExecuteReader()
       If reader.HasRows Then
           Do While reader.Read()
               sDateFrom = reader.GetValue(0) ' TimeIn value
           Loop
       End If
   End Using

   sDateFrom = DateTime.TryParse(sDateFrom, dFrom)
   sDateTo = DateTime.TryParse(sDateTo, dTo)
   Dim TS As TimeSpan = dFrom - dTo
   Dim hour As Integer = TS.Hours
   Dim mins As Integer = TS.Minutes
   Dim secs As Integer = TS.Seconds
   Dim timeDiff As String = ((hour.ToString("00") & ":") + mins.ToString("00") & ":") + secs.ToString("00")
   txttotal.Text = timeDiff
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900