I have two text boxes starttimeInput and endtimeInput that have regularexpressi on validators to only accept input of time variables (11:00 am, 12:15 pm, etc) and then a durationLabel where I want to calculate between the start time and end time and present it to the user as minutes. It was suggested that I first convert the text to date and so I think I did that with the following:
and then it was suggested that I use a function of Subtract to minus those values.
Here's my complete aspx.vb code as it stands right now. Can anyone verify that this is the correct way to do this and also how to
1. pass the result of endtime - starttime into my durationLabel.
2. pass the durationlabel. value to my sql query.
Thank you,
Doug
Code:
Public Sub DateTimeConvert()
Dim starttime, endtime As DateTime
starttime = CType(starttimeInput.Text, DateTime)
endtime = CType(endtimeInput.Text, DateTime)
End Sub
Here's my complete aspx.vb code as it stands right now. Can anyone verify that this is the correct way to do this and also how to
1. pass the result of endtime - starttime into my durationLabel.
2. pass the durationlabel. value to my sql query.
Code:
Imports System.Data.SqlClient
Imports System.Data
Partial Class _Default
Inherits System.Web.UI.Page
Public Sub DateTimeConvert()
Dim starttime, endtime As DateTime
starttime = CType(starttimeInput.Text, DateTime)
endtime = CType(endtimeInput.Text, DateTime)
End Sub
Public Function Subtract(ByVal value As TimeSpan) As DateTime
Dim starttime As TimeSpan
Dim endtime As TimeSpan
Dim diff1 As System.TimeSpan
diff1 = starttime.Subtract(endtime)
End Function
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
InsertRow()
End Sub
Public Sub ClearFields()
OperatorDropdown.SelectedIndex = -1
exceptiondateInput.Text = ""
starttimeInput.Text = ""
endtimeInput.Text = ""
durationLabel.Text = ""
code.SelectedIndex = -1
Approvedby.SelectedIndex = -1
End Sub
Public Sub InsertRow()
Dim myConnectionString As String = "Initial Catalog=mdr;Data Source=xxxxx;uid=xxxxx;password=xxxxxx;"
Dim myConnection As New SqlConnection(myConnectionString)
Dim commandText As String = _
"insert into Exceptions (employeenumber, exceptiondate, starttime, endtime, duration, code, approvedby) " & _
"values(@employeenumber, @exceptiondate, @starttime, @endtime, @duration, @code, @approved)"
Using connection As New SqlConnection(myConnectionString)
Dim command As New SqlCommand(commandText, connection)
command.Parameters.Add("@employeenumber", SqlDbType.VarChar)
command.Parameters("@employeenumber").Value = OperatorDropdown.SelectedItem.Text
command.Parameters.Add("@exceptiondate", SqlDbType.DateTime)
command.Parameters("@exceptiondate").Value = exceptiondateInput.Text
command.Parameters.Add("@starttime", SqlDbType.DateTime)
command.Parameters("@starttime").Value = starttimeInput.Text
command.Parameters.Add("@endtime", SqlDbType.DateTime)
command.Parameters("@endtime").Value = endtimeInput.Text
command.Parameters.Add("@duration", SqlDbType.VarChar)
command.Parameters("@duration").Value = durationLabel.Text
command.Parameters.Add("@code", SqlDbType.VarChar)
command.Parameters("@code").Value = code.SelectedItem.Text
command.Parameters.Add("@approved", SqlDbType.VarChar)
command.Parameters("@approved").Value = Approvedby.SelectedItem.Text
Try
connection.Open()
Dim rowsAffected As Integer = command.ExecuteNonQuery()
Catch ex As Exception
myErrorMessageLabel.Text = ex.Message
End Try
thankyouLabel.Text = "Your Data Has Been Submitted"
ClearFields()
End Using
End Sub
End Class
Thank you,
Doug
Comment