dougancil
10-12-2010, 01:47 PM
I have two text boxes on my form that are going to be submitted to a sql server. They are both datetime sql db types and what I'm looking to do is to calculate the time difference between the boxes and present that to the user as duration. I know that this can be done with datediff on the sql side so what I was thinking would be the best manner to handle this would be just to add this
datediff (minute, starttime,endtime) to my sql query, store the return value as a variable to present in a label or should I do something different?
Here is the default.aspx.vb code that I have so far:
Imports System.Data.SqlClient
Imports System.Data
Partial Class _Default
Inherits System.Web.UI.Page
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 = ""
durationInput.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 = durationInput.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
I also know that currently I have a textbox for duration, but that is going to be a label to present the value of the difference between the starttime and endtime before the user presses the submit button.
Thank you.
datediff (minute, starttime,endtime) to my sql query, store the return value as a variable to present in a label or should I do something different?
Here is the default.aspx.vb code that I have so far:
Imports System.Data.SqlClient
Imports System.Data
Partial Class _Default
Inherits System.Web.UI.Page
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 = ""
durationInput.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 = durationInput.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
I also know that currently I have a textbox for duration, but that is going to be a label to present the value of the difference between the starttime and endtime before the user presses the submit button.
Thank you.