prevent duplicate row insert aspx


I have a page which takes some values from a querystring and then inserts them on a button click.

Everything is fine however it inserts a duplicate row - i think this is due to a page refresh on button click however im not entirely sure.

Attached is my button click code. has anyone seen this before?

Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim objConn As New System.Data.SqlClient.SqlConnection()
        objConn.ConnectionString = "Data Source=SERVERNAME;Password=PWD;User ID=UID;Initial Catalog=DBNAME"
        Dim objCmd As New System.Data.SqlClient.SqlCommand("leap29jobapply", objConn)
        objCmd.CommandType = System.Data.CommandType.StoredProcedure
        Dim jobid As System.Data.SqlClient.SqlParameter = objCmd.Parameters.Add("@jobid", System.Data.SqlDbType.[Int])
        Dim username As System.Data.SqlClient.SqlParameter = objCmd.Parameters.Add("@currentuser", System.Data.SqlDbType.[NVarChar])
        Dim jid As String = Request.QueryString("jobid")
        jobid.Direction = System.Data.ParameterDirection.Input
        jobid.Value = jid
        username.Direction = System.Data.ParameterDirection.Input
        username.Value = User.Identity.Name
    End Sub

Open in new window

Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Page refresh does not cause button clicks. The reason could be that the user clicks the button twice. Try disabling the button in the javascript when clicked.
leap29Author Commented:
its definately not being clicked twice (as i am clicking the button)

I will however try the disable
Is the stored procedure coded as expected?
PMI ACP® Project Management

Prepare for the PMI Agile Certified Practitioner (PMI-ACP)® exam, which formally recognizes your knowledge of agile principles and your skill with agile techniques.

leap29Author Commented:
Well its very very simple so i cant see its the SP fault

(@jobid int,
@currentuser nvarchar(50))
insert into tempjobapply values (@jobid, @currentuser)

Open in new window

Put a breakpoint on the codebehind of the button click and run in debug mode to see if that code is being executed twice.
leap29Author Commented:
ok ive done that and i have created a unique restraint on the table.

I am now getting the followin problem when running through the debug. It is throwing up a unique restraint error on the line objCmd.ExecuteNonQuery()
but it is only running through the code once??
That is totally wiered to me.
leap29Author Commented:
I have solved it.

this was the problem

Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click

it should read

Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs)

thanks for you help

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
That is wrong. It should have the Handles clause. Instead remove the OnClick handler you have provide in the aspx code.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.