Question

I have 2 Forms: enter image description here 1.MainForm :

in which users are asked to input name, Family, then there is button named as "payment detail"(which opens Form2).

2.Form2

The user input the payment detail and click on Save button. Then this transaction insert the new row in sql table and this form will be closed (Step 2). While MainFrom is still Open I would like, on user click on Save button on MainForm (Step 3), transaction of Form2 committed, but If user close the Main form then Form2 transaction roll backed.

I would appreciate your suggestions for this scenario ?

go throw steps 1,2,3

Was it helpful?

Solution

I think you are looking for something like this:

class MainForm()
{
    private TransactionScope _scope;
    public void SaveButton_Click()
    {
        _scope.Complete();
        Close();
    }
    public MainForm()
    {
        _scope = new TransactionScope();
    }
    ~MainForm()
    {
        _scope.Dispose();
    }
}

However, I strongly advise you NOT to do this. Since you will possibly have to wait a long time for the user to complete the inputs, you will run into all kinds of problems with transaction lifetime and locked database records.

Depending on your needs, consider picking one of the following solutions instead:

  • If the user clicks cancel in MainForm, simply delete the created Transaction again.

  • Pass the inputted values from the Transaction Form to the Main Form and only save the Transaction if the user clicks the Main Form's Save button.

OTHER TIPS

My understanding is that your final goal is to save in a DB name and surname and then eventually also payment details such as date and amount. For your own reasons you wont to display these input fields in two different forms. So, Form 1 could stay as it is. When Form 2 is open you need to prevent the user to close Form1 until save or cancel on Form 2 is clicked. There are various possible approach to such scenario, the simple one could be to remove the close button from Form 1 when and until Form 2 is open. To achieve such, you can implement something like this:

private const int CP_NOCLOSE_BUTTON = 0x200;
protected override CreateParams CreateParams
{
   get
  {
   CreateParams myCp = base.CreateParams;
   myCp.ClassStyle = myCp.ClassStyle | CP_NOCLOSE_BUTTON ;
   return myCp;
  }
}

reference for the above is codeproject

Another alternative could be to make your Form1 and Form 2 not movable on the screen and overlapping each other so that the user cannot access the closing button of Form 1 so he is forced to close Form 2 to get access to Form 1. Reference for such feature can be found here on SO. There other ways to do the above but this is what I had in mind now. Let us know.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top