Results 1 to 4 of 4

Thread: Checking values between text boxes

  1. #1
    Join Date
    Apr 2010

    Checking values between text boxes

    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
        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
                    Dim rowsAffected As Integer = command.ExecuteNonQuery()
                Catch ex As Exception
                    myErrorMessageLabel.Text = ex.Message
                End Try
                thankyouLabel.Text = "Your Data Has Been Submitted"
            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.
    Last edited by dougancil; 10-12-2010 at 02:03 PM.

  2. #2
    Join Date
    Nov 2006
    Springfield, MO
    Six ways, half a dozen of another. If you want VB or C#, you might try this article:

    Ryan Butler

    Ryan Butler.org

  3. #3
    Join Date
    Apr 2010

    I've seen a few varieties of articles describing ways to do this. The part that confuses me is because I'm running both a regularexpressionvalidator and a requiredfieldvalidator on both the starttime and endtime check boxes, I just want to make sure that I'm putting my code in the right place. Here's my default.aspx page with the text boxes:

    <&#37;@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head id="Head1" runat="server">
    <title>Untitled Page</title>
    <style type="text/css">
    width: 131px;
    color: #FF0000;
    <script language="javascript" type="text/javascript">
    // <!CDATA[
    // ]]>
    <br />
    <form id="form1" runat="server">
    <div align="left">
    <asp:DropDownList ID="OperatorDropdown" runat="server"
    DataSourceID="SqlDataSource1" DataTextField="employeenumber"
    DataValueField="employeenumber" Height="23px" Width="130px">
    <asp:SqlDataSource ID="SqlDataSource1" runat="server"
    ConnectionString="<%$ ConnectionStrings:MDRConnectionString %>"
    SelectCommand="SELECT [Employeenumber] FROM [Employees]">
    &nbsp;&nbsp; Choose an Operator
    <br />
    <br />
    <br />
    <asp:TextBox ID="exceptiondateInput" runat="server" Width="130px" Height="23px"></asp:TextBox>
    &nbsp; <span class="style1"></span> Exception Date &nbsp;<asp:RegularExpressionValidator
    ID="DateValidator" runat="server"
    ErrorMessage="Please Enter the Date Correctly">
    <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"
    ErrorMessage="You Must Supply A Date"
    <br />
    <br />
    <br />
    <asp:TextBox ID="starttimeInput" runat="server" Width="130px" Height="23px"></asp:TextBox>
    &nbsp; <span class="style1"></span> Start Time &nbsp;<asp:RegularExpressionValidator
    ID="StartTimeValidator" runat="server"
    ValidationExpression="^((([0]?[1-9]|1[0-2])(:|\.)[0-5][0-9]((:|\.)[0-5][0-9])?( )?(AM|am|aM|Am|PM|pm|pM|Pm))|(([0]?[0-9]|1[0-9]|2[0-3])(:|\.)[0-5][0-9]((:|\.)[0-5][0-9])?))$"
    ErrorMessage="Start Time Incorrectly Formatted"
    <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server"
    ErrorMessage="This field cannot be empty"
    <br />
    <br />
    <br />
    <asp:TextBox ID="endtimeInput" runat="server" Width="130px" Height="23px"></asp:TextBox>
    &nbsp; <span class="style1"></span> End Time &nbsp;<asp:RegularExpressionValidator
    ID="EndTimeValidator" runat="server"
    ValidationExpression="^((([0]?[1-9]|1[0-2])(:|\.)[0-5][0-9]((:|\.)[0-5][0-9])?( )?(AM|am|aM|Am|PM|pm|pM|Pm))|(([0]?[0-9]|1[0-9]|2[0-3])(:|\.)[0-5][0-9]((:|\.)[0-5][0-9])?))$"
    ErrorMessage="You Must Supply an End Time"
    <asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server"
    ErrorMessage="This field cannot be empty" ControlToValidate="endtimeInput"></asp:RequiredFieldValidator>
    <br />
    <br />
    <br />
    <asp:TextBox ID="durationInput" runat="server" Width="130px" Height="23px"></asp:TextBox>
    &nbsp; <span class="style1"></span> Duration &nbsp;<asp:RequiredFieldValidator
    ID="DurationValidator" runat="server"
    ErrorMessage="Exception Duration Must Be Entered"
    <asp:RequiredFieldValidator ID="RequiredFieldValidator4" runat="server"
    ErrorMessage="This field cannot be empty" ControlToValidate="durationInput"></asp:RequiredFieldValidator>
    <br />
    <br />
    <br />
    <asp:DropDownList ID="code" runat="server" Height="23px" Width="206px">
    <asp:ListItem Value="NoSelection">--- No Selection --</asp:ListItem>
    <asp:ListItem>Approved Technical Reason</asp:ListItem>
    <asp:ListItem>Coaching Session</asp:ListItem>
    <asp:ListItem>Sick Leave</asp:ListItem>
    <asp:ListItem>Special Project</asp:ListItem>
    <asp:ListItem>Supervisor Meeting</asp:ListItem>
    &nbsp;Choose a reason for exception &nbsp;<asp:compareValidator id="codeValidator"
    runat="server" ControlToValidate="code"
    ValueToCompare="NoSelection" Operator="NotEqual"
    ErrorMessage="Please Select a Reason" >*</asp:CompareValidator>
    <br />
    <br />
    <br />
    <asp:DropDownList ID="Approvedby" runat="server" Width="130px" Height="23px">
    <asp:ListItem Value="NoSelection">--- No Selection --</asp:ListItem>
    <asp:ListItem>Patrice Paul</asp:ListItem>
    <asp:ListItem>Zach Cochran</asp:ListItem>
    &nbsp;Approved By &nbsp;<asp:compareValidator id="CompareValidator1"
    runat="server" ControlToValidate="Approvedby"
    ValueToCompare="NoSelection" Operator="NotEqual"
    ErrorMessage="Please Select either Patrice or Zach" >*</asp:CompareValidator>
    <br />
    <br />
    <br />
    <asp:Button ID="Button1" runat="server" Text="Submit" />
        <br />
    <br />
    <br />
    <span class="style1">* all fields must be filled out</span><br />
    <span class="style1">* times should have am or pm after them</span><br />
    <br />
    <asp:Label ID="myErrorMessageLabel" runat="server"></asp:Label>
        <br />
        <asp:Label ID="thankyouLabel" runat="server"></asp:Label>
    <br />
    <asp:ValidationSummary ID="ValidationSummary1" runat="server" Height="150px" />
    <br />
    <br />
    Granted at this point, the "duration" field is still a textbox, but that's going to be replaced with a label to show my results. Oh and I also want to pass the duration variable to the sql query on my default.aspx.vb page. I'm assuming that once I know what code to put into the page, that passing that variable won't be difficult.

  4. #4
    Join Date
    Nov 2006
    Springfield, MO
    Looks like everything is in the right place. Only thing I would advise is to break out your insert action into a separate class, then create an instance of that object in the page, that way, you abstract the functionality of db operations, instead of programming to the concrete class, which will eventually become a design problem.
    Ryan Butler

    Ryan Butler.org

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center



X vBulletin 4.2.2 Debug Information

  • Page Generation 0.10171 seconds
  • Memory Usage 2,897KB
  • Queries Executed 15 (?)
More Information
Template Usage (33):
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_global_above_footer
  • (1)ad_global_below_navbar
  • (1)ad_global_header1
  • (1)ad_global_header2
  • (1)ad_navbar_below
  • (1)ad_showthread_firstpost_sig
  • (1)ad_showthread_firstpost_start
  • (1)ad_thread_first_post_content
  • (1)ad_thread_last_post_content
  • (2)bbcode_code
  • (1)footer
  • (1)forumjump
  • (1)forumrules
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (1)headinclude_bottom
  • (4)memberaction_dropdown
  • (1)navbar
  • (4)navbar_link
  • (1)navbar_moderation
  • (1)navbar_noticebit
  • (1)navbar_tabs
  • (2)option
  • (4)postbit
  • (4)postbit_onlinestatus
  • (4)postbit_wrapper
  • (1)spacer_close
  • (1)spacer_open
  • (1)tagbit_wrapper 

Phrase Groups Available (6):
  • global
  • inlinemod
  • postbit
  • posting
  • reputationlevel
  • showthread
Included Files (26):
  • ./showthread.php
  • ./global.php
  • ./includes/class_bootstrap.php
  • ./includes/init.php
  • ./includes/class_core.php
  • ./includes/config.php
  • ./includes/functions.php
  • ./includes/functions_navigation.php
  • ./includes/class_friendly_url.php
  • ./includes/class_hook.php
  • ./includes/class_bootstrap_framework.php
  • ./vb/vb.php
  • ./vb/phrase.php
  • ./includes/functions_facebook.php
  • ./includes/functions_calendar.php
  • ./includes/functions_bigthree.php
  • ./includes/class_postbit.php
  • ./includes/class_bbcode.php
  • ./includes/functions_reputation.php
  • ./includes/functions_notice.php
  • ./packages/vbattach/attach.php
  • ./vb/types.php
  • ./vb/cache.php
  • ./vb/cache/db.php
  • ./vb/cache/observer/db.php
  • ./vb/cache/observer.php 

Hooks Called (70):
  • init_startup
  • friendlyurl_resolve_class
  • init_startup_session_setup_start
  • database_pre_fetch_array
  • database_post_fetch_array
  • init_startup_session_setup_complete
  • global_bootstrap_init_start
  • global_bootstrap_init_complete
  • cache_permissions
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • load_show_variables
  • load_forum_show_variables
  • global_state_check
  • global_bootstrap_complete
  • global_start
  • style_fetch
  • global_setup_complete
  • showthread_start
  • showthread_getinfo
  • strip_bbcode
  • friendlyurl_clean_fragment
  • friendlyurl_geturl
  • forumjump
  • cache_templates
  • cache_templates_process
  • template_register_var
  • template_render_output
  • fetch_template_start
  • fetch_template_complete
  • parse_templates
  • fetch_musername
  • notices_check_start
  • notices_noticebit
  • process_templates_complete
  • friendlyurl_redirect_canonical
  • showthread_post_start
  • showthread_query_postids
  • showthread_query
  • bbcode_fetch_tags
  • bbcode_create
  • showthread_postbit_create
  • postbit_factory
  • postbit_display_start
  • postbit_imicons
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • postbit_display_complete
  • memberaction_dropdown
  • tag_fetchbit_complete
  • forumrules
  • navbits
  • navbits_complete
  • build_navigation_data
  • build_navigation_array
  • check_navigation_permission
  • process_navigation_links_start
  • process_navigation_links_complete
  • set_navigation_menu_element
  • build_navigation_menudata
  • build_navigation_listdata
  • build_navigation_list
  • set_navigation_tab_main
  • set_navigation_tab_fallback
  • navigation_tab_complete
  • fb_like_button
  • showthread_complete
  • page_templates