2017-02-19 73 views
0

This was the error msg im gettingenter image description here我有一個數據庫有一行「Total_Time」(時間)。 格式爲HH:MM:SS。我需要將代碼「Total_time」轉換爲分鐘。如何將總時間轉換爲分鐘

例如,如果Total_time = 01:30:00,答案應該是Total_minutes = 90, ,我想將total_minutes乘以「Other」(int變量)。

下面是我曾嘗試:

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 [email protected]", 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 

但是當我點擊什麼也沒有發生label7沒有顯示任何值提前 感謝。

+0

這是什麼問題? – Roope

+0

我已經插入了這段代碼的一個按鈕,當我點擊什麼都沒有發生label7是不顯示任何值 –

+1

你是不是意味着在年底Label7.Text = strmin –

回答

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

爲了避免類似的錯誤,我會強烈建議使用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 
+0

我很好奇 - 如果你沒有閱讀這個問題,你爲什麼回答? – Chris

+0

@Chris,因爲我閱讀了標題,並且可能對搜索類似內容的其他人有所幫助 – Slai

+0

我不知道代碼如何符合我的代碼,請你幫忙..... –

0

嘗試以下

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 [email protected]", 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 

如果時間超過24:00:00,使用下面的代碼

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 [email protected]", 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 
+0

label7.text上仍然沒有顯示任何內容 –

+0

@vigneshs我認爲它應該是'如果dr.Read()然後'語句,如果你正在閱讀只有一個記錄,並確保查詢返回任何結果 – Slai

+0

DId你選擇一個正確的ID在文本框5 ??? –

0

不要切換到字符串...並使用Timespan

如果您的標籤永遠不會更改,那麼例程必須出錯,從catch語句中檢查錯誤。但是從你的形象看來,他看起來沒有任何價值。

而且您應該使用ALWAYS在使用數據庫時測試dbnull。 DBNull在Math和Boolean比較中有一些奇怪的行爲。

與此

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替換while循環:你的問題由其他乘說,但你的表格/代碼表示添加...我的問題去了。

+0

它是投擲錯誤 –

+0

我已經uplaoaded錯誤消息 –

+0

我倒了病,我無法工作的總和一天,但我現在沒事了,我已經嘗試了所有上面的代碼,它不工作 –

相關問題