kwilliams
06-10-2008, 03:26 PM
I have an ASP.NET (2.0) 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:
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
***Note: feedback.aspx code to come on next post due to character limit.
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:
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
***Note: feedback.aspx code to come on next post due to character limit.