[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 671
  • 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

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