AGuyCalledGerald
AGuyCalledGerald

Reputation: 8150

Second process seems to make first process crash (.net, c#)

In a .net-webapplication, I have a module with the following code:

protected void SaveButton_Click(object sender, EventArgs e)
{
   //..

   itemRepository.Create(store, validFrom);

   Item item = itemRepository.GetByStoreAndValidFrom(store, validFrom);

   //..

}

An item is created by the repository and then fetched from the database.
Sometimes, f.ex. if the user clicks on the button twice very quickly, the following seems to happen:
Two program processes of this module (please correct if the term not right!) start very closely after each other. Two items are created and the first process crashes because there is more than one item available in the database.
Is there a good way to handle this problem? I thought about putting a javascript blocker into the interface (to prevent the second button click) but would be thankful for hints.

Upvotes: 1

Views: 100

Answers (1)

devio
devio

Reputation: 37215

Disabling the control initiating a postback will cause the form not to be submitted.

The way I solved this problem on the UI is to add another div that will be displayed on top of the web form to prevent repeated submissions. Original article with solution is here.

Upvotes: 2

Related Questions