Click here to Skip to main content
15,881,600 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi guys im new in vb
i just want to ask if how can i store the elapsed time to my database?

i have 2 forms
in form one i have a timer and a button
VB
Public Class Form1
    Public _elapseTimerRunning As Boolean = False
    Public _elapseStartTime As DateTime
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        _elapseStartTime = DateTime.Now
        _elapseTimerRunning = True
      
    End Sub

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        End

    End Sub

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
  
        Timer1.Interval = 1000
        Timer1.Enabled = True
    End Sub

    Public Sub Timer1_Tick(sender As Object, e As EventArgs)
        ''Form_Cert.Label5.Text = Now.ToString("h:mm:ss tt")
        If _elapseTimerRunning = True Then
            Dim elapsedtime = DateTime.Now.Subtract(_elapseStartTime)
            Form2.Label5.Text = String.Format("{0}hr : {1}min : {2}sec", elapsedtime.Hours, elapsedtime.Minutes, elapsedtime.Seconds)
        End If
    End Sub
End Class

NOW I WANT TO DISPLAY THE ELAPSED TIME IN LABEL 5 OF FORM 2
AFTER THAT I WANT TO SAVE TIT TO MY DB?

HOW CAN I DO THAN?


THAKS IN ADVANCE
Posted
Updated 11-Sep-14 17:31pm
v2
Comments
PIEBALDconsult 12-Sep-14 0:22am    
If you want to use a string, might I suggest you use an ISO 8601-compliant "Duration" format?
http://en.wikipedia.org/wiki/ISO_8601#Durations

1 solution

Concerning showing the elapsed time on another form: I would suggest that operation you're doing when the button is clicked would be separated to a different, non-UI class. A method in this class would make the operation and for example using an event it would inform that operation is on-going and the amount of elapsed time.

In order for the UI to actually show the results, the operation should be run in another thread. Both forms would wire the event so that when the event from the class is raised, both forms could do what they want with the information.

So the class structure could be something like
- Form1
--- button
--- eventhandler for elapsed

- Form2
--- eventhandler for elapsed

- Class SomeName
--- ElapsedEvent
--- MethodName

In order to actually do this you should go through the documenation for:
- BackgroundWorker[^] for the actual operation
- InvokeRequired[^] tho show the elapsed time in UI thread
- EventArgs[^] to create custom event arguments for your event

To save the data you would need a number column in a table where you can store for example the amount of elapsed ticks. To manipulate the data you would need:
- SqlConnection[^]
- SqlCommand[^]

and to do the saving properly in the first place, also SqlTransaction[^]
 
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