0

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
    Try


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


        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.

  • And what is the problem? – Roope Feb 19 '17 at 14:51
  • i have inserted this code for a button and when i click nothing is happening label7 is not displaying any values –  Feb 19 '17 at 14:53
  • 1
    Don't you mean at the end to put Label7.Text = strmin – Shai Rado Feb 19 '17 at 14:54
  • im not getting can you please explain –  Feb 19 '17 at 14:58
  • what format is Total_Time really stored as in the DB? Why all the string conversions? – Trevor_G Feb 19 '17 at 15:07
  • i need to display the total in the label7.text on the button click –  Feb 19 '17 at 15:09
  • The Total_Time is in time(7) format -Trevor –  Feb 19 '17 at 15:10
  • Are you getting results back from your query? You haven't explained what debugging you have done and where the problem is. I would hope that you have at least verified whether your datareader contains any data and thus whether you are going into the loop or whether an exception is being thrown - if so you should definitely tell us what exception is being thrown... – Chris Feb 19 '17 at 15:16
  • Turn on Option Strict `tt = dr("Total_Time").ToString` wont compile if `tt` is defined as double – Ňɏssa Pøngjǣrdenlarp Feb 19 '17 at 15:51
  • `While dr.Read() 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 End While Catch ex As Exception` –  Feb 19 '17 at 15:56
  • this is what the code im using but still not working –  Feb 19 '17 at 15:58
  • You are erroring out. break on the Catch statement and see what the error is. BUt it looks like your Other value is not a number which would cause it to drop out. – Trevor_G Feb 19 '17 at 16:32
  • you ALWAYS should test for dbnull when using databases. DBNull has some weird behaviours in math and Boolean comparisons. – Trevor_G Feb 19 '17 at 16:34
  • is the value of "other" is just minutes i.e. integer or in the HH:MM:SS format ? . I have considered HH:MM:SS in my code. – Mukul Varshney Feb 19 '17 at 16:35

3 Answers3

2
Dim Total_minutes As Double = CDate("1:23:45").TimeOfDay.TotalMinutes          ' 83.75

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

Dim Total_Time As DateTime = Convert.ToDateTime(dr!Total_Time)
Dim Total_minutes# = Total_Time.TimeOfDay.TotalMinutes

Dim Other# = Val(dr!Other)
Dim total# = Total_minutes * Other

Label7.Text = total.ToString
Slai
  • 22,144
  • 5
  • 45
  • 53
0

Try below

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

    con = New System.Data.SqlClient.SqlConnection
    Try
        con.ConnectionString = "Data Source=Vicky-pc\sqlexpress;Initial Catalog=customer_details;Integrated Security=True;Pooling=False"
        con.Open()

        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 other As TimeSpan           
            Dim tt As TimeSpan

            If TimeSpan.TryParse(dr("Total_Time"), tt) Then
                If TimeSpan.TryParse(dr("Other"), other) Then
                    tt = tt.Add(other)
                Else
                    'Do something like show error message for incorrect data for dr("Other")
                End If
                Label7.Text = tt.TotalMinutes.ToString
            Else
                'Do something like show error message for incorrect data for dr("Total_Time")
            End If
        End While
    Catch ex As Exception

    End Try
End Sub

If time is more than 24:00:00, use below code

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

    con = New System.Data.SqlClient.SqlConnection
    Try
        con.ConnectionString = "Data Source=Vicky-pc\sqlexpress;Initial Catalog=customer_details;Integrated Security=True;Pooling=False"
        con.Open()

        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()     
            Try
                Dim dataTime As String = dr("Total_Time").ToString
                dataTime = dataTime.Split("."c)(0).ToString
                Dim tt As New TimeSpan(Integer.Parse(dataTime.Split(":"c)(0)), Integer.Parse(dataTime.Split(":"c)(1)), Integer.Parse(dataTime.Split(":"c)(2)))
                dataTime = dr("Other").ToString
                dataTime = dataTime.Split("."c)(0).ToString
                Dim other As New TimeSpan(Integer.Parse(dataTime.Split(":"c)(0)), Integer.Parse(dataTime.Split(":"c)(1)), Integer.Parse(dataTime.Split(":"c)(2)))
                tt = tt.Add(other)
                dataTime = tt.TotalMinutes.ToString

            Catch ex As Exception
                'do something here as string is not a time
            End Try
        End While
    Catch ex As Exception

    End Try
End Sub
Mukul Varshney
  • 3,131
  • 1
  • 12
  • 19
0

Don't keep switching to strings... and use a Timespan

If your label never changes then the routine must be erroring out, examine the error from the catch statement. However from your image it looks like Other has no value.

And you should ALWAYS test for dbnull when using databases. DBNull has some weird behaviours in Math and Boolean comparisons.

Replace your while loop with this

    If Not Dr.read OrElse IsDBNull(Dr("Total_time")) OrElse IsDBNull(Dr("Other")) Then
        Label7.text = "ERR"
    Else
        Dim ts As TimeSpan = TimeSpan.Parse(dr("Total_time").ToString)
        Label7.text = (ts.TotalMinutes * Dr("Other")).ToString
    End If

PS: Your question says multiply by Other but your form/code says Add... I went with the question.

Trevor_G
  • 1,331
  • 1
  • 8
  • 17