C# .NET - Insert Problem - Asked By sam patil on 10-Sep-13 12:46 PM

I have insert a data in a database, but i have click again in this button to insert double in a database i have already use disable button property but button enable then again insert in a database .So how to solve a problem to insert a data only one time in adatabase and again insert to message box "already inserted"
Robbe Morris replied to sam patil on 10-Sep-13 05:36 PM
The optimum way is to use a Save oriented stored procedure that attempts to update the record first and then performs an insert if it isn't there.

The less than optimum way is to query the database first and then only perform the insert if it isn't there and attempt an update if it is.

The reality is though, your issue far more likely to be a user interface issue where you are not creating/setting some sort of flag (ie hidden field) to say that you successfully inserted a record and that subsequent clicks of the button will trigger an attempt to update that record versus inserting it again.  Typically, this is done by retrieving the primary key of the newly inserted record and storing it "somewhere" either in the hidden field, session, cookie or some other relevant place depending on your need.  On subsequent button clicks, you'd check to see if the primary key value is there as a means to determine whether an insert has taken place.