Wrox Programmer Forums
| Search | Today's Posts | Mark Forums Read
ASP.NET 2.0 Professional If you are an experienced ASP.NET programmer, this is the forum for your 2.0 questions. Please also see the Visual Web Developer 2005 forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 2.0 Professional section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
  #1 (permalink)  
Old June 10th, 2008, 03:23 PM
Banned
Points: 1,561, Level: 16
Points: 1,561, Level: 16 Points: 1,561, Level: 16 Points: 1,561, Level: 16
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jul 2005
Location: , , .
Posts: 317
Thanks: 0
Thanked 0 Times in 0 Posts
Default Newbie: Input Form Problem

I have an input form that submits user data to a DB table using a stored procedure only after the ASP.NET validations have been met. Once the page is valid, the form disappears to the user, and they instead see a confirmation message.

It works great, except for if I then push the refresh button on that same page. If I do that, the stored procedure is run again, and the data is re-submitted to the DB table.

I know that I'm missing a simple step in this, but I'm not sure how to proceed. I've included all of the related code below. Any and all help would be appreciated.

feedback.aspx.vb:
Code:
Imports System.Data.SqlClient
Partial Class forms_feedback
    Inherits System.Web.UI.Page

    Private ds As New DataSet()
    Private conn As SqlConnection
    Private cmd As New SqlCommand()

    Sub SubmitBtn_Click(ByVal Sender As Object, ByVal E As EventArgs)
        'Declare form variables
        Dim category_form As String = selectCategory.SelectedItem.Value
        Dim address_form As String = txtAddress.Text
        Dim fullname_form As String = txtFullName.Text
        Dim city_form As String = txtCity.Text
        Dim state_form As String = selectState.SelectedItem.Value
        Dim zipcode_form As String = txtZipCode.Text
        Dim email_form As String = txtEmailAddress.Text
        Dim phone_form As String = txtPhone.Text
        Dim comments_form As String = txtComments.Text

        'Assign date variables
        Dim dtCurrDate As DateTime = DateTime.Now 'Assign current date
        Dim strDateTimeISO As String = dtCurrDate.ToString("s") 'ISO format
        hfDateTime.Value = strDateTimeISO
        Session("SubmitTime") = dtCurrDate.ToString("t")

        'Assign server variables
        hfRemoteAddr.Value = Request.ServerVariables("REMOTE_ADDR")
        hfRemoteHost.Value = Request.ServerVariables("REMOTE_HOST")

        If Page.IsValid Then
            'Display confirmation message
            lblOutput.Text = "Thank you for completing the Feedback Form. If contact was requested, you should be contacted by someone within 1-3 days during normal business hours."

            'Insert into database
            conn = New SqlConnection("server=SERVERNAME;database=DBNAME;uid=USERNAME;pwd=PASSWORD")
            cmd = New SqlCommand("spFeedback", conn)
            cmd.CommandType = CommandType.StoredProcedure

            'Assign form paramaters
            cmd.Parameters.Add("@category_id", SqlDbType.VarChar, 16).Value = category_form
            cmd.Parameters.Add("@fullname", SqlDbType.VarChar, 50).Value = fullname_form
            cmd.Parameters.Add("@address", SqlDbType.VarChar, 50).Value = address_form
            cmd.Parameters.Add("@city", SqlDbType.VarChar, 50).Value = city_form
            cmd.Parameters.Add("@state", SqlDbType.VarChar, 2).Value = state_form
            cmd.Parameters.Add("@zipcode", SqlDbType.VarChar, 10).Value = zipcode_form
            cmd.Parameters.Add("@email", SqlDbType.VarChar, 75).Value = email_form
            cmd.Parameters.Add("@phone", SqlDbType.VarChar, 20).Value = phone_form
            cmd.Parameters.Add("@comments", SqlDbType.VarChar, 1000).Value = comments_form
            cmd.Parameters.Add("@remoteaddr", SqlDbType.VarChar, 20).Value = hfRemoteAddr.Value
            cmd.Parameters.Add("@remotehost", SqlDbType.VarChar, 20).Value = hfRemoteHost.Value
            cmd.Parameters.Add("@datetime", SqlDbType.VarChar, 50).Value = hfDateTime.Value

            conn.Open()
            cmd.ExecuteNonQuery()
            conn.Close()
        End If
    End Sub

End Class
feedback.aspx:
Code:
<%@ Page Language="VB" MasterPageFile="~/MasterPage.master" CodeFile="feedback.aspx.vb" Inherits="forms_feedback" AutoEventWireup="true" title="Feedback Form" %>
<asp:content id="Content1" contentplaceholderid="ContentPlaceHolder1" runat="server">
    <asp:label id="lblOutput" runat="server" /><br />
    <% If Not Page.IsPostBack Then %>
        <form name="formFeedback" action="" method="post" runat="server">
            <asp:ValidationSummary ID="valSummary" runat="server"
                HeaderText="You must fix the following form entries:"
                Font-Names="verdana" 
                Font-Size="10pt"
                Font-Bold="True" 
            />
            <br />
        <table class="tableborder_black" width="98%" id="Feedback Form">
            <tbody>
                <tr class="tablecell_black">
                    <th scope="col" colspan="2">Feedback Form</th>
                </tr>
                <tr>
                    <td class="tablecell1" align="right" valign="top">Category</td>
                    <td class="tablecell3">
                        <asp:DropDownList id="selectCategory" runat="server">
                            <asp:ListItem Value="general" Text="General" Selected="True" />
                            <asp:ListItem Value="cat1" Text="Category 1" />
                            <asp:ListItem Value="cat2" Text="Category 2" />
                            <asp:ListItem Value="cat3" Text="Category 3" />
                        </asp:DropDownList>
                    </td>
                </tr>
                <tr>
                    <td class="tablecell1" align="right" valign="top" style="height: 26px">Full Name</td>
                    <td class="tablecell3" style="height: 26px">
                        <asp:Textbox id="txtFullName" columns="20" runat="server" />
                        <asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ErrorMessage="Allowed characters: A-Z, a-z,  and dashes (-)." ValidationExpression="^[a-zA-Z-]{0,50}$" ControlToValidate="txtFullName"></asp:RegularExpressionValidator>
                    </td>
                </tr>
                <tr>
                    <td class="tablecell1" align="right" valign="top">Address</td>
                    <td class="tablecell3"><asp:Textbox id="txtAddress" columns="20" runat="server" />
                        <asp:RegularExpressionValidator ID="RegularExpressionValidator2" runat="server" ControlToValidate="txtAddress"
                            ErrorMessage="Allowed characters: 0-9, A-Z, a-z, dashes (-), periods (.), and number signs (#)." ValidationExpression="^[a-zA-Z0-9 \.#-/]{0,50}$"></asp:RegularExpressionValidator>
                    </td>
                </tr>
                <tr>
                    <td class="tablecell1" align="right" valign="top">City</td>
                    <td class="tablecell3"><asp:Textbox id="txtCity" columns="20" runat="server" />
                        <asp:RegularExpressionValidator ID="RegularExpressionValidator3" runat="server" ControlToValidate="txtCity"
                            ErrorMessage="Allowed characters: A-Z, a-z, dashes (-), and periods (.)." ValidationExpression="^[a-zA-Z \.-]{0,50}$"></asp:RegularExpressionValidator>
                    </td>
                </tr>
                <tr>
                    <td class="tablecell1" align="right" valign="top">State</td>
                    <td class="tablecell3">
                        <!--<asp:DropDownList id="stateoption" runat="server" />
                        <br />-->
                        <asp:DropDownList id="selectState" runat="server">
                            <asp:ListItem Value="" Text="- Select -" Selected="true" />
                            <asp:ListItem Value="AK" Text="Arkansas" />
                            <asp:ListItem Value="AL" Text="Alabama" />
                            <asp:ListItem Value="AR" Text="Arkansas" />
                            <asp:ListItem Value="AZ" Text="Arizona" />
                            <asp:ListItem Value="CA" Text="California" />
                            <asp:ListItem Value="CO" Text="Colorado" />
                            <asp:ListItem Value="CT" Text="Connecticut" />
                            <asp:ListItem Value="DE" Text="Deleware" />
                            <asp:ListItem Value="FL" Text="Florida" />
                            <asp:ListItem Value="GA" Text="Georgia" />
                            <asp:ListItem Value="HI" Text="Hawaii" />
                            <asp:ListItem Value="IA" Text="Iowa" />
                            <asp:ListItem Value="ID" Text="Idaho" />
                            <asp:ListItem Value="IL" Text="Illinois" />
                            <asp:ListItem Value="IN" Text="Indiana" />
                            <asp:ListItem Value="KS" Text="Kansas" />
                            <asp:ListItem Value="KY" Text="Kentucky" />
                            <asp:ListItem Value="LA" Text="Louisiana" />
                            <asp:ListItem Value="MA" Text="Massachusetts" />
                            <asp:ListItem Value="MD" Text="Maryland" />
                            <asp:ListItem Value="ME" Text="Maine" />
                            <asp:ListItem Value="MI" Text="Michigan" />
                            <asp:ListItem Value="MN" Text="Minnesota" />
                            <asp:ListItem Value="MO" Text="Missouri" />
                            <asp:ListItem Value="MS" Text="Mississippi" />
                            <asp:ListItem Value="MT" Text="Montana" />
                            <asp:ListItem Value="NC" Text="North Carolina" />
                            <asp:ListItem Value="ND" Text="North Dakota" />
                            <asp:ListItem Value="NE" Text="Nebraska" />
                            <asp:ListItem Value="NH" Text="New Hampshire" />
                            <asp:ListItem Value="NJ" Text="New Jersey" />
                            <asp:ListItem Value="NM" Text="New Mexico" />
                            <asp:ListItem Value="NV" Text="Nevada" />
                            <asp:ListItem Value="NY" Text="New York" />
                            <asp:ListItem Value="OH" Text="Ohio" />
                            <asp:ListItem Value="OK" Text="Oklahoma" />
                            <asp:ListItem Value="OR" Text="Oregon" />
                            <asp:ListItem Value="PA" Text="Pennsylvania" />
                            <asp:ListItem Value="RI" Text="Rhode Island" />
                            <asp:ListItem Value="SC" Text="South Carolina" />
                            <asp:ListItem Value="SD" Text="South Dakota" />
                            <asp:ListItem Value="TN" Text="Tennessee" />
                            <asp:ListItem Value="TX" Text="Texas" />
                            <asp:ListItem Value="UT" Text="Utah" />
                            <asp:ListItem Value="VA" Text="Virginia" />
                            <asp:ListItem Value="VT" Text="Vermont" />
                            <asp:ListItem Value="WA" Text="Washington" />
                            <asp:ListItem Value="DC" Text="Washington D.C." />
                            <asp:ListItem Value="WI" Text="Wisconsin" />
                            <asp:ListItem Value="WV" Text="West Virginia" />
                            <asp:ListItem Value="WY" Text="Wyoming" />
                            <asp:ListItem Value="OT" Text="Other (specify in comments)" />
                        </asp:DropDownList>
                        <asp:RequiredFieldValidator ID="RequiredFieldValidator2" ControlToValidate="selectState" runat="server"
                            ErrorMessage="Must select a state."></asp:RequiredFieldValidator>
                    </td>
                </tr>
                <tr>
                    <td class="tablecell1" align="right" valign="top" style="height: 26px">Zip Code</td>
                    <td class="tablecell3" style="height: 26px"><asp:Textbox id="txtZipCode" columns="20" runat="server" />
                        <asp:RegularExpressionValidator ID="RegularExpressionValidator4" runat="server" ControlToValidate="txtZipCode"
                            ErrorMessage="Zip code must be valid (Format: xxxxx or xxxxx-xxxx)." ValidationExpression="\d{5}(-\d{4})?"></asp:RegularExpressionValidator>
                    </td>
                </tr>
                <tr>
                    <td class="tablecell1" align="right" valign="top">Email Address</td>
                    <td class="tablecell3"><asp:Textbox id="txtEmailAddress" columns="20" runat="server" />
                        <asp:RegularExpressionValidator ID="RegularExpressionValidator5" runat="server" ControlToValidate="txtEmailAddress"
                            ErrorMessage="Email must be valid (Format: you@domain.com)." ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"></asp:RegularExpressionValidator>
                    </td>
                </tr>
                <tr>
                    <td class="tablecell1" align="right" valign="top">Phone</td>
                    <td class="tablecell3"><asp:Textbox id="txtPhone" columns="20" runat="server" />
                        <asp:RegularExpressionValidator ID="RegularExpressionValidator7" runat="server" ControlToValidate="txtPhone"
                        ErrorMessage="Phone number must be valid (Format: xxx-xxx-xxxx)." ValidationExpression="((\(\d{3}\) ?)|(\d{3}-))?\d{3}-\d{4}"></asp:RegularExpressionValidator>
                    </td>
                </tr>
                <tr>
                    <td class="tablecell1" align="right" valign="top" style="height: 50px">Comments</td>
                    <td class="tablecell3" style="height: 50px">
                        <asp:TextBox ID="txtComments" Columns="40" Rows="4" runat="server" Height="60px" TextMode="MultiLine"></asp:TextBox>
                        <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="txtComments"
                            ErrorMessage="Must enter comments to complete this form." Display="Dynamic"></asp:RequiredFieldValidator>
                        <asp:RegularExpressionValidator ID="RegularExpressionValidator8" runat="server" ControlToValidate="txtComments"
                            ErrorMessage="Must enter between 5 and 1000 characters." ValidationExpression="^[a-zA-Z-0-9]{5,1000}$" Display="Dynamic"></asp:RegularExpressionValidator>
                    </td>
                </tr>
                <tr class="tablecell_black">
                    <td scope="col" colspan="2" align="center">
                        <asp:HiddenField ID="hfRemoteAddr" runat="server" />
                        <asp:HiddenField ID="hfRemoteHost" runat="server" />
                        <asp:HiddenField ID="hfDateTime" runat="server" />
                        <input id="btnReset" type="Reset" runat="server">
                        <asp:Button ID="btnSubmit" Text="Submit" OnClick="SubmitBtn_Click" runat="server" />
                    </td>
                </tr>
                </tbody>
            </table>
        </form>
    <% End If %>      
</asp:content>
KWilliams
  #2 (permalink)  
Old June 12th, 2008, 08:15 AM
planoie's Avatar
Friend of Wrox
Points: 16,481, Level: 55
Points: 16,481, Level: 55 Points: 16,481, Level: 55 Points: 16,481, Level: 55
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2003
Location: Clifton Park, New York, USA.
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

Sorry, but I haven't read through all the code.

The problem isn't with your code, it's all working from the sounds of it. The problem is simply the nature of how the page is living in the browser. When you refresh it, you are refreshing the last page action you performed which is to post the form to send feedback.

One reliable way to eliminate this problem is to redirect to a message/confirmation page so the user can't refresh on the posted page.

Another way, although more complex, could be to set some flag in session that indicates that the feedback was posted (perhaps you can reset on initial page load when not postback so the user could submit additional feedback). Check that flag in the button click handler to eliminate a second post of the exact same data.

-Peter
compiledthoughts.com


Similar Threads
Thread Thread Starter Forum Replies Last Post
change the form input value maggiefeng Classic ASP Basics 2 December 10th, 2006 10:28 AM
Email Form - Question from a Newbie morgan270 ASP.NET 2.0 Basics 8 September 13th, 2006 03:58 PM
input form ( onclick) bubblegirl Javascript How-To 1 March 6th, 2006 01:48 AM
form input as database name gtmm9 Classic ASP Databases 1 October 21st, 2003 03:12 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.