Click here to Skip to main content
15,895,142 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a ASPX form with a Save Button. User enters transaction data and saves it on saving the data will not get cleared and remain intact and only TransactionID(PrimaryKey Value) is updated in the page from DB which is a hidden field.

Everything works fine except when I enter new data and accidentally clicked on Save Button multiple times, then the same data is getting saved multiple times in the DB even overriding the Duplication checking in my DAO.

Do anyone have a solution for fixing this.
Posted
Comments
Jim Jos 30-May-12 1:16am    
Data getting added as new record or existing record is updated?
Rajesh Kariyavula 30-May-12 1:26am    
The same record gets inserted multiple times
Jim Jos 30-May-12 1:34am    
If you primary key constraint properly set insertion will fail right? Are you using autoincrement as primary key - then it won't fail

Before inserting check all fields present in table or not if present means exit from the method else save the data

ex:
C#
public void save()
{
saldataadapter da=new SqlDataAdapter("select *from sample",con);
dataset ds=new dataset();
da.fill(ds);
for(int i=o;i<ds.tables[0].rows.count;i++)>
{
if(txtname.text==ds.tables[0].rows[i][1].tostring() &&txtaddr.text==ds.tables[0].rows[i][2])
{
response.write("Data exist");

}
else
{
//write insert logic here

}
}
}



or

after inserting data disable the save button

btnsave.visible=false;
 
Share this answer
 
v2
Comments
Rajesh Kariyavula 30-May-12 1:52am    
This will not work for me we are using nHibernate along with spring.net not the ADO.Net, anyways thanks for your answer
When your data is saved successfully, take out the value that is unique for that record and save it in the viewstate/hiddenfield. now every time the save is clicked check for this value and compare it with the unique value that exist on form. if they are same then discard it, if they are different then save it as you have new data. also if the viewstate value is null you have to save it as it would mean this page is opened for first time.

I Hope this will help. tell me if I understood incorrectly. i will try to improve the solution.
 
Share this answer
 
Comments
Rajesh Kariyavula 30-May-12 2:04am    
Even before the HiddenField value is updated multiple records gets saved.

Suppose I pressed on Save 3 times then 3 Save operations are firing Which will call a WCF Service Method and that WCF service method calls a RepoMethod where Duplication is checked and data is saved using nHibernate.

Three records will get inserted into the table with same data and the hidden field will get the last record ID
Rahul Rajat Singh 30-May-12 2:08am    
Here is an idea. when the user hits the save button disable it for some time and re-enable it when the save is successful and you have kept that value in your viewstate. i guess the button will be disabled for half a second and that should be fine. also you can show the text of button as "saving..." while it is disabled that would be good from usability standpoint too.
when you click on savebutton then diable the whole page while data save and after save data enable the page. you can do it by using div.
if you want to solve your problem in this way than i will give explain it by code. if yes than comment.
 
Share this answer
 
are you using UpdatePanel. its most likely that on IsPostBack, you are not refreshing your page or you are not setting the new values into the fields inside IsPostBack body

try to debug the page at OnLoad event over page.IsBostBack Method. hope you will reach to the solution.
 
Share this answer
 

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