0

For some reason when the user click on the submit button and he re-fresh the page the same data get's uploaded again to my SQL Server 2005 database. I do not what this to happen........... Why is this happening? I am making use of a SQL Data Source!!

My code

 Try

        'See if user typed the correct code.
        If Me.txtSecurity.Text = Session("Captcha") Then

            If Session("NotifyMe") = "Yes" Then
                SendEmailNS()
            End If

            RaterRate.Insert()
            RaterRate.Update()

            DisableItems()

            lblResultNS.Text = "Thank you for leaving a comment"

            LoadCompanyList()
            LoadRateRecords()

            txtCommentNS.Text = ""
            txtSecurity.Text = ""
            lblResultNS.Focus()

        Else

            Session("Captcha") = GenerateCAPTCHACode()
            txtSecurity.Text = ""
            txtSecurity.Focus()
            Validator10.Validate()

        End If


    Catch ex As Exception

        lblResultNS.Visible = True     
        lblResultNS.Text = ex.Message.ToString
        lblResultNS.Focus()

    End Try

2 Answers 2

1

When user visiting your page, generating new captcha. after when post or else, its checking captcha but not generating new. Move 'captcha set' code to outer scope.

If Me.txtSecurity.Text = Session("Captcha") Then
    ...
Else
    ...
End If

Session("Captcha") = GenerateCAPTCHACode()
Sign up to request clarification or add additional context in comments.

Comments

0

This is happening because of the nature of WebForms relying on the PostBack model. Every time you click on a button the single form you have inside the page is posted to the server along with the viewstate. When you press F5 you normally get a warning that the form will be reposted and if you click Yes the same action is executed on the server. One way to avoid this is to perform a redirect after you save to the database:

Response.Redirect("~/success.aspx");

1 Comment

Another way is to reset the form values or change the ID of form

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.