473,326 Members | 2,168 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

Adding/Subtracting from text boxes

347 100+
I have two text boxes starttimeInput and endtimeInput that have regularexpression 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:

Expand|Select|Wrap|Line Numbers
  1. Public Sub DateTimeConvert()
  2.         Dim starttime, endtime As DateTime
  3.         starttime = CType(starttimeInput.Text, DateTime)
  4.         endtime = CType(endtimeInput.Text, DateTime)
  5.     End Sub
  6.  
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.

Expand|Select|Wrap|Line Numbers
  1. Imports System.Data.SqlClient
  2. Imports System.Data
  3.  
  4. Partial Class _Default
  5.     Inherits System.Web.UI.Page
  6.     Public Sub DateTimeConvert()
  7.         Dim starttime, endtime As DateTime
  8.         starttime = CType(starttimeInput.Text, DateTime)
  9.         endtime = CType(endtimeInput.Text, DateTime)
  10.     End Sub
  11.     Public Function Subtract(ByVal value As TimeSpan) As DateTime
  12.         Dim starttime As TimeSpan
  13.         Dim endtime As TimeSpan
  14.         Dim diff1 As System.TimeSpan
  15.         diff1 = starttime.Subtract(endtime)
  16.     End Function
  17.     Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
  18.         InsertRow()
  19.     End Sub
  20.     Public Sub ClearFields()
  21.         OperatorDropdown.SelectedIndex = -1
  22.         exceptiondateInput.Text = ""
  23.         starttimeInput.Text = ""
  24.         endtimeInput.Text = ""
  25.         durationLabel.Text = ""
  26.         code.SelectedIndex = -1
  27.         Approvedby.SelectedIndex = -1
  28.     End Sub
  29.     Public Sub InsertRow()
  30.         Dim myConnectionString As String = "Initial Catalog=mdr;Data Source=xxxxx;uid=xxxxx;password=xxxxxx;"
  31.  
  32.         Dim myConnection As New SqlConnection(myConnectionString)
  33.         Dim commandText As String = _
  34.         "insert into Exceptions (employeenumber, exceptiondate, starttime, endtime, duration, code, approvedby) " & _
  35.         "values(@employeenumber, @exceptiondate, @starttime, @endtime, @duration, @code, @approved)"
  36.  
  37.         Using connection As New SqlConnection(myConnectionString)
  38.             Dim command As New SqlCommand(commandText, connection)
  39.  
  40.             command.Parameters.Add("@employeenumber", SqlDbType.VarChar)
  41.             command.Parameters("@employeenumber").Value = OperatorDropdown.SelectedItem.Text
  42.  
  43.             command.Parameters.Add("@exceptiondate", SqlDbType.DateTime)
  44.             command.Parameters("@exceptiondate").Value = exceptiondateInput.Text
  45.  
  46.             command.Parameters.Add("@starttime", SqlDbType.DateTime)
  47.             command.Parameters("@starttime").Value = starttimeInput.Text
  48.  
  49.             command.Parameters.Add("@endtime", SqlDbType.DateTime)
  50.             command.Parameters("@endtime").Value = endtimeInput.Text
  51.  
  52.             command.Parameters.Add("@duration", SqlDbType.VarChar)
  53.             command.Parameters("@duration").Value = durationLabel.Text
  54.  
  55.             command.Parameters.Add("@code", SqlDbType.VarChar)
  56.             command.Parameters("@code").Value = code.SelectedItem.Text
  57.  
  58.             command.Parameters.Add("@approved", SqlDbType.VarChar)
  59.             command.Parameters("@approved").Value = Approvedby.SelectedItem.Text
  60.  
  61.             Try
  62.  
  63.                 connection.Open()
  64.                 Dim rowsAffected As Integer = command.ExecuteNonQuery()
  65.  
  66.             Catch ex As Exception
  67.                 myErrorMessageLabel.Text = ex.Message
  68.             End Try
  69.             thankyouLabel.Text = "Your Data Has Been Submitted"
  70.             ClearFields()
  71.         End Using
  72.  
  73.     End Sub
  74.  
  75.  
  76. End Class
  77.  

Thank you,

Doug
Oct 13 '10 #1

✓ answered by Frinavale

I'm really not sure why this is so mysterious to you.

What you need to do is retrieve the text entered in the TextBoxes that all users to enter the start time and the end time.

Then you convert these values into DateTime objects so that you can validate and do your calculation. I recommend that you use the DateTime.TryParse() method to convert the string values into DateTime objects because it does not throw any Exception when/if it has a problem converting the text into a DateTime Object (if it has a problem this method returns false).

So...to calculate the difference between two time values you would do the following (where starttimeInput and endtimeInput are TextBoxes):
Expand|Select|Wrap|Line Numbers
  1.    Dim startTime As DateTime    
  2.    Dim endTime As DateTime 
  3.    If DateTime.TryParse(starttimeInput.Text, startTime) AndAlso _
  4.       DateTime.TryParse(endtimeInput.Text, endTime) Then
  5.         Dim ts As TimeSpan = endTime - startTime
  6.    Else
  7.        'Start Time or End Time value is invalid
  8.    End If

If you need to store the difference between the start time and the end time in the database (even though you are storing the start-time and end-time values already)...then create a DateTime object that represents this value based on the TimeSpan that resulted in subtracting the start time from the end time:
Expand|Select|Wrap|Line Numbers
  1.   Dim startTime As DateTime    
  2.    Dim endTime As DateTime 
  3.    If DateTime.TryParse(starttimeInput.Text, startTime) AndAlso _
  4.       DateTime.TryParse(endtimeInput.Text, endTime) Then
  5.         Dim ts As TimeSpan = endTime - startTime
  6. 'This will be stored in the database....
  7.          Dim timeDiff As DateTime = New DateTime(Today.Year, Today.Month, Today.Day, ts.Hours, ts.Minutes, ts.Seconds)
  8. 'Or if you are storing the time difference as a string then create a string based on the ts:
  9.          Dim strTimeDiff As String = ts.Hours.ToString + ":" + ts.Minutes.ToString + ":" + ts.Seconds.ToString
  10.    Else
  11.        'Start Time or End Time value is invalid
  12.    End If

Since you need the time-difference value in order to store it in the database.....it is plainly obvious that you need to calculate it first....

-Frinny

1 3339
Frinavale
9,735 Expert Mod 8TB
I'm really not sure why this is so mysterious to you.

What you need to do is retrieve the text entered in the TextBoxes that all users to enter the start time and the end time.

Then you convert these values into DateTime objects so that you can validate and do your calculation. I recommend that you use the DateTime.TryParse() method to convert the string values into DateTime objects because it does not throw any Exception when/if it has a problem converting the text into a DateTime Object (if it has a problem this method returns false).

So...to calculate the difference between two time values you would do the following (where starttimeInput and endtimeInput are TextBoxes):
Expand|Select|Wrap|Line Numbers
  1.    Dim startTime As DateTime    
  2.    Dim endTime As DateTime 
  3.    If DateTime.TryParse(starttimeInput.Text, startTime) AndAlso _
  4.       DateTime.TryParse(endtimeInput.Text, endTime) Then
  5.         Dim ts As TimeSpan = endTime - startTime
  6.    Else
  7.        'Start Time or End Time value is invalid
  8.    End If

If you need to store the difference between the start time and the end time in the database (even though you are storing the start-time and end-time values already)...then create a DateTime object that represents this value based on the TimeSpan that resulted in subtracting the start time from the end time:
Expand|Select|Wrap|Line Numbers
  1.   Dim startTime As DateTime    
  2.    Dim endTime As DateTime 
  3.    If DateTime.TryParse(starttimeInput.Text, startTime) AndAlso _
  4.       DateTime.TryParse(endtimeInput.Text, endTime) Then
  5.         Dim ts As TimeSpan = endTime - startTime
  6. 'This will be stored in the database....
  7.          Dim timeDiff As DateTime = New DateTime(Today.Year, Today.Month, Today.Day, ts.Hours, ts.Minutes, ts.Seconds)
  8. 'Or if you are storing the time difference as a string then create a string based on the ts:
  9.          Dim strTimeDiff As String = ts.Hours.ToString + ":" + ts.Minutes.ToString + ":" + ts.Seconds.ToString
  10.    Else
  11.        'Start Time or End Time value is invalid
  12.    End If

Since you need the time-difference value in order to store it in the database.....it is plainly obvious that you need to calculate it first....

-Frinny
Oct 13 '10 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Frank | last post by:
I have a large form, that has text boxes of numbers in rows and columns. I need to sum the values in the columns, and put the total at the bottom of the column. But I also need to sum the values in...
3
by: Derek | last post by:
I am trying to add the text of several textboxes as a sum using the + sign but even converting to integer it is concatenating. Any help would be greatly appreciated. Dim price1 As...
9
by: B-Dog | last post by:
I have a form that has about 10 text boxes on it, they all have to be filled out before submitting is there a quick way to make sure that none are null or do I have to call out each textbox? Say...
11
by: Edson Peacock | last post by:
I have a report with sub reports, one of the subreports have 12 text boxes that are 2" high and I want them all to grow if one goes to 3" high. If anyone has any suggestions they are very much...
9
by: Bill (Unique as my name) | last post by:
Please suggest some links or sources which show how to set up form text boxes in VBA. Thank you so much.
4
by: David Plotts | last post by:
I'm a beginner with VB.net, only had one class in college on it. I can't seem to remember how to add text boxes up. I want to add the values in text boxes together, and put the value into a...
11
by: amanda27 | last post by:
I have a form and there are some text boxes that show a score depending on what someone picks in the drop down next to that box. I did this part with some coding and therefore, for example if they...
2
by: Wernerh | last post by:
Hi guys, I must be the dumb one in these posts. I have 2 simple (2 everyone else out there) questions. I have just started using vb. I am able to use vb in excel with no problem and can code...
1
by: semomaniz | last post by:
I have a form where i have created the form dynamically. First i manually added a panel control to the web page. Then i added another panel dynamically and inside this panel i created tables. I have...
11
by: jwessner | last post by:
I have a form (Form1) which contains basic Project data and a subform listing the personnel assigned to the Project as a continuous form. Selecting a person on that project and clicking on a command...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.