Click here to Skip to main content
15,886,806 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
have a table in sql server .When Inserting record in table some time it inserting duplicate with some value in two rows but cant able to reproduce

Example: my 2 rows in table
Col1   Col2     C3   C4
157    Job157   158  IlluminaParking Building 

158    Job157   158  IlluminaParking Building
Posted
Updated 20-Aug-15 0:23am
v2
Comments
Andy Lanng 20-Aug-15 6:26am    
There are so many places this could be happening.
It could be a code issue. POST YOUR CODE
It could be a design issue. Is the user clicking the button twice? POST YOUR MARKUP
It could be human error. Is someone entering the same data twice? Perform a data check before your insert and / or add unique indexing
It could be a database issue. Are you using any Routines or Triggers? POST YOUR QUERIES

There is no possible way we can be specific unless your question is.
SathishRam 20-Aug-15 8:01am    
Thanks Andy.
Detail about the issue
1)Currently i didn't set unique key constraint to that column.
2) i written onclick="btnSave1_Click" id="btnSave" this is my design part.id and onclick name differs.
3) I used stored procedure to insert
Andy Lanng 20-Aug-15 8:16am    
1) well add one, then
2) dangerous. It is possible to click the button many times before the page reloads. That'll fire several Click events
3) I can't help unless you post the code
SathishRam 20-Aug-15 8:57am    
thanks Andy,
My code block(vb.net code behind)
Protected Sub btnSave_Click1(sender As Object, e As EventArgs)
If Not String.IsNullOrEmpty(ViewState("JobId")) Then
UpdateJobList()
UpdateProjectMangement()
Else
InsertJobList()
End If
BindTopTab()
lblSuccessMsg.Text = "Database updated successfully"
End Sub
SathishRam 21-Aug-15 5:03am    
Hi Andy Lanng,
Today i found the issue, it happens due button double click.Now how can i fix this?

If you can't reproduce it, you can't cure it.
You may be able to spot something in your code which could - under the right circumstances - cause a similar problem, but fixing that doesn't guarantee that the actual problem has been fixed.

So start by looking at the data, and when it had a problem, and try to work out if there are any common factors. Then start trying to cause it manually. When you can cause it at will (or at least 50% of the time) you can be sure that when you make changes you have fixed the problem. But until then, you are just guessing. And intermittent problems are always difficult to find.

Sorry, but we probably can't help you with that, even if we had access to your code, your data, and your users - but we don't, so you are going to have to do the "leg work" on your own.
 
Share this answer
 
In my opinion it is easy to fing when it is occurs according to find where it is occurs

To find when it is occuring you can put constrait to table that does not allow to dublucate records based on col2 c3 and c4 colums. So that you can catch the when it occuring an then fix it.
 
Share this answer
 
Comments
SathishRam 20-Aug-15 7:56am    
Thanks ayhanUGUR.
Thank u so much for a advice. it happens once in 500 records approximately.Please help me try to find the issue soon
ayhanUGUR 20-Aug-15 8:14am    
can you put code block that make insertion
SathishRam 20-Aug-15 8:53am    
thanks ayhan,
My code.vb(vb.net codew behind)
Protected Sub btnSave_Click1(sender As Object, e As EventArgs)
If Not String.IsNullOrEmpty(ViewState("JobId")) Then
UpdateJobList()
UpdateProjectMangement()
Else
InsertJobList()
End If
BindTopTab()
lblSuccessMsg.Text = "Database updated successfully"
End Sub
ayhanUGUR 20-Aug-15 9:00am    
If there is no bug inside InsertJobList func. , It maybe because of clicking button twice


try that

Protected Sub btnSave_Click1(sender As Object, e As EventArgs)
btnSave.Enabled = False
If Not String.IsNullOrEmpty(ViewState("JobId")) Then
UpdateJobList()
UpdateProjectMangement()
Else
InsertJobList()
End If
BindTopTab()
lblSuccessMsg.Text = "Database updated successfully"
btnSave.Enabled = True
End Sub
SathishRam 20-Aug-15 9:14am    
Ok, thanks ayhan.
Ok - After comments we discovered that the issue is in the button double click (a click by the user after the first post but before the page reloads)


There are two (main) ways to solve this:

1: disable the button after the first click
2: sanitize the data before inserting.

If you are a developer worth your salt, then you should do both!

ok. first how to disable a button after the first click. This needs to be done via javascript. I can be done at the server side but that's just hiding functionality the user should be able to see.

JavaScript
function preventSecondClick(button){
    $(button).click(function(e){
        e.preventDefault;
    });
}

Add this to your buttons onclientclick: OnClientClick="preventSecondClick(this);". It will change the click event to prevent postbacks, but will still postback the current click.


Second: Sanitize your data. You say that you are using a Stored Proc to insert? Then data sanity is easy:

insert into table (a,b,c)
select @a,@b,@c
where not exists (select * from table where a = @a AND b = @b AND c=@c)

This will not insert a value that already exists. When you execute this from VB you would use ExecuteNotQuery(cmd) which will return an int. The int is the number of rows affected. If 0 then the data is not inserted, if 1 then it is. You can use this to flag possible issues, but it's not required.

Hope that helps.

let me know how you get on ^_^
Andy
 
Share this answer
 
Comments
SathishRam 21-Aug-15 9:40am    
Thanks Andy ,
1)The second one suits me (sanitize the data before inserting.)
2)i cant use the first one because after inserting i have sub tab to insert i used this same button to insert sub tab
Andy Lanng 21-Aug-15 9:45am    
Once the page reloads after the postback then the click events will be reset, but one is better than none. Try it and accept the solution please ^_^
GL
Andy
SathishRam 25-Aug-15 5:37am    
Thanks Andy,
i added preloader,User can't click the button till the page submit complete.
Andy Lanng 25-Aug-15 5:47am    
Nice!
I often add a spinner.gif to the button so the user knows it's busy. My BP uses a div that covers the whole page (with alpha 0.3) and a spinner.gif so the user knows they cannot interact with the page. It's up to you ^_^
Good luck
SathishRam 25-Aug-15 5:54am    
Thanks a lot AndyLanng.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900