Link to home
Start Free TrialLog in
Avatar of leap29
leap29Flag for Afghanistan

asked on

prevent duplicate row insert aspx

hi,

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"
        objConn.Open()
 
        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
 
        objCmd.ExecuteNonQuery()
        objConn.Close()
 
    End Sub

Open in new window

Avatar of Nasir Razzaq
Nasir Razzaq
Flag of United Kingdom of Great Britain and Northern Ireland image

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.
Avatar of leap29

ASKER

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?
Avatar of leap29

ASKER

Well its very very simple so i cant see its the SP fault


(@jobid int,
@currentuser nvarchar(50))
 
as
 
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.
Avatar of leap29

ASKER

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.
ASKER CERTIFIED SOLUTION
Avatar of leap29
leap29
Flag of Afghanistan image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
That is wrong. It should have the Handles clause. Instead remove the OnClick handler you have provide in the aspx code.