vignesh s vignesh s - 27 days ago 6 Question

How to convert total time to minutes

This was the error msg im gettingenter image description hereI have a database that has a row "Total_Time" (time).
It is in the format HH:MM:SS. I need the code to convert the "Total_time" to minutes.

For example, if Total_time = 01:30:00, the answer should be Total_minutes = 90,
and I want to multiply the total_minutes with "Other" (int variable).

Below is what I have tried:

Private Sub Button5_Click(sender As Object, e As EventArgs) Handles Button5.Click

con = New System.Data.SqlClient.SqlConnection

con.ConnectionString = "Data Source=Vicky-pc\sqlexpress;Initial Catalog=customer_details;Integrated Security=True;Pooling=False"

Dim cm As SqlClient.SqlCommand
cm = New SqlClient.SqlCommand("SELECT * FROM customer_details WHERE Id=@id", con)
cm.Parameters.AddWithValue("@id", TextBox5.Text)
dr = cm.ExecuteReader()
While dr.Read()
Dim tt As Double
tt = dr("Total_Time").ToString
Dim other As Double
other = dr("Other").ToString

Dim str() As String
Dim strmin As Double
str = Split(tt.ToString, ":")
strmin = (CDbl(str(1)) * 60 + CDbl(str(2)) + CDbl(str(3)) / 60).ToString

Dim total As Decimal
total = strmin + other
Label7.Text = total.ToString
End While
Catch ex As Exception

End Try
End Sub

but when i click nothing is happening label7 is not displaying any values
Thanks in advance.

Answer Source

To avoid similar errors, I would highly recommend using Option Strict

Dim Total_Time As DateTime = Convert.ToDateTime(dr("Total_Time"))
Dim minutes As Double = Total_Time.TimeOfDay.TotalMinutes

Dim Other As Double = Val(dr("Other"))
Dim total As Double = minutes * Other

Label7.Text = total.ToString