locked
How to handle database transaction within multiple web forms? RRS feed

  • Question

  • User-381691765 posted

    I have a asp.net 2.0 web application. In one of the modules I have following scenario. In the UI there are 3 pages to take the input from the user. On the 1st page, we are inserting data in the master table and with the same we are beginning new sql transaction with the isolation level READ UNCOMMITTED. so that uncommited master table data can be read.  Master table has also has 4 detail tables. Then the user is navigated to 2nd and then to 3rd page where the user can enter multiple records in the detail tables. And only after finishing on the 3rd page, the transaction is commited.

    I want to ask that is it the right way of doing this kind of functionality?

    And is there any other way of doing this?

     

    Wednesday, December 12, 2007 4:07 AM

Answers

  • User1971872635 posted

    It would work something like this.  First, create a custom business class to store all your info

    class SomeInfo
    {
      public string Name
      {
       get {return name;}
       set {name = value;}
      }
      ....more properties
    }

    The, on page 1, if not a postback:

    myInfo = new SomeInfo();
    Session("myInfo") = myInfo;

    If a postback (still on pg 1) or just before you transfer to page 2 (you'll need to capture this) you'll want to retrieve the instance of SomeInfo from Session and set the properties:

    myInfo = (SomeInfo) Session("myInfo");
    myInfo.Name = textbox.Name;
    ...set more properties & save back to session
    Session("myInfo") = myInfo;

     

     

    On page 2 you can probably assume that myInfo already exists the first time through (ie, not a post back) so you don't have to create a new one, but on each post back you should retrieve the instance of SomeInfo and set the appropriate properties

    myInfo = (SomeInfo) Session("myInfo");  //Note that you have to cast to SomeInfo
    ...set more properties & save back to session
    Session("myInfo") = myInfo;.

    You can really simplify your life by adding the save/update/delet methods to SomeInfo so when the user clicks save you can just call the Save() method,

    myInfo.Save();

     

     

    Let me know if you need further clarification

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, December 13, 2007 8:52 AM

All replies

  • User436930739 posted

    What if the user close the browser on the 2nd or 3rd page and doesn't go through the entire process ?  You must also think about this.

    Wednesday, December 12, 2007 4:59 AM
  • User-381691765 posted

    To handle this,

    we have written the code that calls rollback when the same user logs in again into the system.

    Wednesday, December 12, 2007 6:18 AM
  • User1971872635 posted

    I want to ask that is it the right way of doing this kind of functionality

    No, it's a very bad way.  You should never hold locks across post-backs because it will block other users from even reading the same page (in fact, it may affect far more rows than you think because sql server will probably be holding a lock on the index page which contains references to potentially hundreds of different rows).

    Instead, store the transitional data in Session (my first choice) or a cookie.  Let us know if you need help doing this.

    Wednesday, December 12, 2007 8:42 AM
  • User-381691765 posted

    Thanks for replying,

    can you please provide an example of how doing it this way?

    Wednesday, December 12, 2007 11:47 PM
  • User1971872635 posted

    It would work something like this.  First, create a custom business class to store all your info

    class SomeInfo
    {
      public string Name
      {
       get {return name;}
       set {name = value;}
      }
      ....more properties
    }

    The, on page 1, if not a postback:

    myInfo = new SomeInfo();
    Session("myInfo") = myInfo;

    If a postback (still on pg 1) or just before you transfer to page 2 (you'll need to capture this) you'll want to retrieve the instance of SomeInfo from Session and set the properties:

    myInfo = (SomeInfo) Session("myInfo");
    myInfo.Name = textbox.Name;
    ...set more properties & save back to session
    Session("myInfo") = myInfo;

     

     

    On page 2 you can probably assume that myInfo already exists the first time through (ie, not a post back) so you don't have to create a new one, but on each post back you should retrieve the instance of SomeInfo and set the appropriate properties

    myInfo = (SomeInfo) Session("myInfo");  //Note that you have to cast to SomeInfo
    ...set more properties & save back to session
    Session("myInfo") = myInfo;.

    You can really simplify your life by adding the save/update/delet methods to SomeInfo so when the user clicks save you can just call the Save() method,

    myInfo.Save();

     

     

    Let me know if you need further clarification

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, December 13, 2007 8:52 AM
  • User1971872635 posted

    On other thing that I forgot to mention, you should probably mark your custom business object as serializable.  The reason to do this is because if you ever use a state server to maintain state, asp.net needs to serialize the data to put it in session.  See http://support.microsoft.com/default.aspx?scid=KB;EN-US;q312112&ID=KB;EN-US;q312112 

    [Serializable]
    [class SomeInfo
    {
      public string Name
      {
       get {return name;}
       set {name = value;}
      }
      ....more properties
    }

    Tuesday, December 18, 2007 2:15 PM