0

How can I receive a transaction as argument in methods parameter?

    private void method_A(/*How to receive command-transaction here? */)
    {
    /* doing tons of db stuff.. */
    }

    protected void Insert_OnClick(object sender, EventArgs e)
    {
           //begin transaction
           var connection = WhatEverMyConnectionsIs();
           using (var transaction = connection.BeginTransaction())
           {

           try
           {
               var command = new OldDbCommand();
               command.Transaction = transaction;

               Method_A(command);

               Method_B(command);

               Method_C(command);

               transaction.Commit();
           }
           catch(Exception ex)
           {
               //if there was an exception rollback.
               transaction.Rollback();
           }
         }    

     }

Also, once I pass the transaction into a methods parameter where I got some db read/insert/update syntax, do I have to do anything specific within the method as well? I have never used a transaction before.

Nyprez
  • 173
  • 1
  • 12
  • `Method_A(transaction, command)`? – CodeCaster Jun 23 '16 at 10:32
  • It's not entirely clear what you're trying to do or what you asking. Are you simply asking how you pass parameters to a method? – Charles Mager Jun 23 '16 at 10:33
  • http://stackoverflow.com/questions/224689/transactions-in-net – Leon Jun 23 '16 at 10:33
  • And we can't help with that description. What have you tried, what happened **exactly**? Please read [ask]. – CodeCaster Jun 23 '16 at 10:35
  • @CharlesMager how to pass command(where it got transaction function) to methods, so I guess, yes. – Nyprez Jun 23 '16 at 10:36
  • @CodeCaster I couldn't make it work. EDIT: Sorry my bad. It says, cannot resolve symbol "transaction". Using Visual Studio – Nyprez Jun 23 '16 at 10:40
  • @Nyprez you wouldn't get that error with the code you've included, the code you've included doesn't even compile. It's stil not clear what you're asking. – Charles Mager Jun 23 '16 at 10:46
  • @CharlesMager Do you mean I the way I try to use transaction doesn't wont work? – Nyprez Jun 23 '16 at 10:48
  • 1
    @Nyprez No, I mean you're passing an argument to a method that doesn't have a parameter. I'm struggling to work out if this is a basic 'how do I make method with parameters' (in which case the context of commands and transactions is irrelevant, find a basic C# tutorial), or if you're asking something else. – Charles Mager Jun 23 '16 at 10:51
  • @CharlesMager Yeah you're probably right. It's kinda a basic question where I'm asking "How to receive X as argument in a method". I know how to send string, int etc. But in this case, I've no idea what I'm sending to the method. Don't know if it's string, or what so ever. That's what my question is mainly about. EDIT: What is `command` in this case? – Nyprez Jun 23 '16 at 11:20
  • 1
    @Nyprez so instead of string, you use e.g. the command type: `OleDbCommand`. – Charles Mager Jun 23 '16 at 11:22
  • @CharlesMager Oh thanks, bit obviously once you said that! Silly me. I'll try that. – Nyprez Jun 23 '16 at 11:26
  • @CharlesMager Worked as `private void Method_A(OleDbCommand transaction)`. But it says `'transaction' is never used` which I fully understand. Is this the proper way to use transaction in this way: `transaction.CommandText = /*db syntax*/` instead of using a standard OleDbCommand like: `cmd.CommandText = /*db syntax*/` ? Thanks – Nyprez Jun 23 '16 at 11:39

0 Answers0