www.webdeveloper.com
Results 1 to 8 of 8

Thread: Checking the date before passing on to stored procedure

  1. #1
    Join Date
    Jun 2004
    Posts
    24

    Checking the date before passing on to stored procedure

    Hello All. I have a form that sumbits data into a database, along with the current date and time as a stamp. However, I would like to create something that checks the date before any data is inserted into my table. I only want to allow the data to be entered if the current date is between the first and the 25th of the month. In other words, If today's date is between the 26th and the 31st (or the last day of the month) I want an error message to appear that says "You cannot insert this data right now. Please try again next month." How would i go about doing this?

  2. #2
    Join Date
    Jun 2004
    Posts
    24

    Message/Error Box to Appear

    How do i get the text "You cannot insert this data right now. Please try again next month" into an message/error box?

    Code:
       Sub InsertData()
    
    Dim rightNow as DateTime = DateTime.Now
    
    If day(RightNow) > 14 Then 
    "you cannot insert this data right now. Please try again next month."
     
    Else
        Dim conn as SQLConnection
        Conn = New SQLConnection(connstr)
        Dim Cmd1 as SqlCommand
        Cmd1 = New SQLCommand("Insertaccounts",Conn)
        'Declare the command type - a stored proc in this case
        Cmd1.commandType = CommandType.storedProcedure
        'Add the parameters
        Cmd1.parameters.add("@AccountID",AccNumber.SelectedItem.Value)
        
        dim cmd2 as SqlCommand
        Cmd2 = New SQLCommand("UpdateToTerritory",Conn)
        'Declare the command type - a stored proc in this case
        Cmd2.commandType = CommandType.storedProcedure
        Cmd2.parameters.add("@AccountID",AccNumber.SelectedItem.Value)
        Cmd2.parameters.add("@ToName",ToName.SelectedItem.Value)
        
        dim cmd3 as SqlCommand
        Cmd3 = New SQLCommand("UpdateFromTerritory",Conn)
        'Declare the command type - a stored proc in this case
        Cmd3.commandType = CommandType.storedProcedure
        Cmd3.parameters.add("@AccountID",AccNumber.SelectedItem.Value)
        Cmd3.parameters.add("@FromName",FromName.SelectedItem.Value)
        conn.open()
        cmd1.ExecuteNonQuery()
        cmd2.ExecuteNonQuery()
        cmd3.ExecuteNonQuery()
    
        conn.Close()
        
        Response.redirect(return_page)
         end sub

  3. #3
    Join Date
    Nov 2002
    Location
    Auburn, AL
    Posts
    9,222
    To get the error into an annoying little alert box you would have to use asp.net to write out java script. response.write("java script here"). But I prefer to keep it all server side, I would just write the text on the screen and not write out the form or make the form invisible server side.

  4. #4
    Join Date
    Jun 2004
    Location
    Honolulu, Hawaii
    Posts
    13
    You are better off using a RangeValidator control. It is all client side. I can elaborate if you need it.
    Robert M. Teague
    Senior Programmer/Analyst
    Kamehameha Schools
    http://www.ksbe.edu

  5. #5
    Join Date
    Nov 2002
    Location
    Auburn, AL
    Posts
    9,222
    Originally posted by roteague
    You are better off using a RangeValidator control. It is all client side. I can elaborate if you need it.
    if its all client side then it will fail for 13% of the internet. The best thing would be to make this server side, then go back and write java script for use conveiniance and a little extra bandwidth you can save. But if you put your trust in anything completely client side, expect it to fail for a certain percent for your users and cause flaws in your application.

  6. #6
    Join Date
    Jun 2004
    Location
    Honolulu, Hawaii
    Posts
    13
    RangeValidators are still the way to go. By default they are client-side if the browser supports them, otherwise they do their validation on the server. If you want to do all validation on the server, you can still use RangeValidators. See the June 2004 issue of Visual Studio Magazine, an article titled "Validate Complex Client Data" for example on how to use these on the server.

  7. #7
    Join Date
    Nov 2002
    Location
    Auburn, AL
    Posts
    9,222
    I would still prefer to do it in raw code because when using a validator, of any type, it is still going to output bad code. It will output javascript without the type attribute etc. I like control over my output. Until the output has been cleaned up in another version I refuse to use things like built in pageing, validators, or the calander control. I actualyl would prefer to use html forms too. The control that I find mose conveiniant is the repeater, because I still get complete markup control and it saves a lot of code from looping through data like in asp classic. But I refuse to loose control over the markup. I want my sites to be valid and accessible.

  8. #8
    Join Date
    Nov 2003
    Posts
    655

    Re: Checking the date before passing on to stored procedure

    Performance tip:

    Instead of calling 3 stored procedures in the code you show, create a new stored procedure to handle all three actions and call it instead. Even if the new stored procedure just EXECs the other 3 stored procedures, it'll still be much more efficient.

    Code:
        Dim conn as SQLConnection
        Conn = New SQLConnection(connstr)
        Dim Cmd1 as SqlCommand
        Cmd1 = New SQLCommand("Insertaccounts",Conn)
        'Declare the command type - a stored proc in this case
        Cmd1.commandType = CommandType.storedProcedure
        'Add the parameters
        Cmd1.parameters.add("@AccountID",AccNumber.SelectedItem.Value)
        
        dim cmd2 as SqlCommand
        Cmd2 = New SQLCommand("UpdateToTerritory",Conn)
        'Declare the command type - a stored proc in this case
        Cmd2.commandType = CommandType.storedProcedure
        Cmd2.parameters.add("@AccountID",AccNumber.SelectedItem.Value)
        Cmd2.parameters.add("@ToName",ToName.SelectedItem.Value)
        
        dim cmd3 as SqlCommand
        Cmd3 = New SQLCommand("UpdateFromTerritory",Conn)
        'Declare the command type - a stored proc in this case
        Cmd3.commandType = CommandType.storedProcedure
        Cmd3.parameters.add("@AccountID",AccNumber.SelectedItem.Value)
        Cmd3.parameters.add("@FromName",FromName.SelectedItem.Value)
        conn.open()
        cmd1.ExecuteNonQuery()
        cmd2.ExecuteNonQuery()
        cmd3.ExecuteNonQuery()
    would shrink to

    Code:
        Dim conn as SQLConnection
        Conn = New SQLConnection(connstr)
        Dim Cmd1 as SqlCommand
        Cmd1 = New SQLCommand("ThreeInOne",Conn)
        'Declare the command type - a stored proc in this case
        Cmd1.commandType = CommandType.storedProcedure
        'Add the parameters
        Cmd1.parameters.add("@AccountID",AccNumber.SelectedItem.Value)
        Cmd1.parameters.add("@ToName",ToName.SelectedItem.Value)
        Cmd1.parameters.add("@FromName",FromName.SelectedItem.Value)    
        conn.open()
        cmd1.ExecuteNonQuery()

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



Recent Articles