• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 674
  • Last Modified:

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

0
leap29
Asked:
leap29
  • 5
  • 4
1 Solution
 
CodeCruiserCommented:
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.
0
 
leap29Author Commented:
its definately not being clicked twice (as i am clicking the button)

I will however try the disable
0
 
CodeCruiserCommented:
Is the stored procedure coded as expected?
0
Cloud Class® Course: Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

 
leap29Author Commented:
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

0
 
CodeCruiserCommented:
Put a breakpoint on the codebehind of the button click and run in debug mode to see if that code is being executed twice.
0
 
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??
0
 
CodeCruiserCommented:
That is totally wiered to me.
0
 
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
0
 
CodeCruiserCommented:
That is wrong. It should have the Handles clause. Instead remove the OnClick handler you have provide in the aspx code.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

  • 5
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now