Click here to Skip to main content
15,916,417 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to use a single button to insert records as well as update records to table.which is the Best and less error prone way to insert/update record by using single button.or is it better to use two different buttons for insert and update?
Posted

Check the id of the record on button click. If the Id is Zero then write the logic for inserting the record. If Id is greater than zero then write logic for the update.
[or]
Write one stored procrdure which inserts and updates the record based on Id of record. And call the stored procedure on button click.
 
Share this answer
 
Comments
OP is asking for a suggestion. Read again. :)
I would go with two Buttons. That would be the best from User point of view.

You can work with one Button, but you need to add extra codes to controls their Texts and other properties. Then while Inserting/Updating, you have to check whether User is trying to Insert or Update.

So, in my opinion, go for two Buttons. Simple, yet useful. :)
 
Share this answer
 
Use Entity framework. its simple and can do multiple operations on single click.

If you are happy with your ORM, than move logic to store procedure. Its more better option. Check if record exist, then updated else insert.


Otherwise your ORM must smart enough to create dynamic script based on row state.(You must maintain Row state of your record.)
 
Share this answer
 
v2

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