Click here to Skip to main content
15,921,203 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all. In presentation layer(code behind of a form)Inside a for loop I do multiple insert. I Want If an error occurred during Insert operation in loop for,previous inserts will be Rollback.
I don't know how I use transaction
C#
 foreach (var loan in _loanlist)
{
    if (!pda.InsertLoan(new Loan{
        prsnlcd = prsncod,
        LoanCode = loan.LoanCode,
        LoanStatus = loan.LoanAmountRemain == 0 ? true : false}))
      Do Something;
}

"pda" is a business class for handling insert Operation:
C#
//----------InsertLoan--------
public bool InsertLoan(Loan loanMoney)
{
    lasterror = string.Empty;
    if (loanMoney == null)
        return false;
    List<SqlParameter> lst = loanMoneyToParameter(loanMoney);
    if (dbh.Insert("InsertLoanInfo", CommandType.SP, lst, false) == -1)
    {
        if (dbh.lasError != string.Empty)
        {
            lasterror = string.Empty;
            lasterror = dbh.lasError;
        }
        return false;
    }
    else
        return true;
}

dbh is a class in data layer which includes Insert function
C#
public int Insert(String cmdText, CommandType comType, List<SqlParameter> ParameterList, Boolean isRetVal)
       {
           SqlCommand Command = new SqlCommand();
           Command.CommandText = cmdText;
           Command.Connection = Sqlc;
           if (comType == CommandType.SP)
               Command.CommandType = System.Data.CommandType.StoredProcedure;
           else
               Command.CommandType = System.Data.CommandType.Text;
           if (ParameterList != null)
           {
               foreach (SqlParameter item in ParameterList)
                   Command.Parameters.Add(item);
           }
           OpenConnection(Sqlc);
               try
               {
                   Command.ExecuteNonQuery();
                   CloseConnection(Sqlc);
                   return 1;
               }
               catch (Exception ex)
               {
                   CloseConnection(Sqlc);
               }
           return -1;
       }
Posted

Refer:
1. First, understand the concept of Using Transactions in ADO.NET[^]
2. You could try transactionscope[^] in your situation.
 
Share this answer
 
Comments
mit62 10-May-14 1:44am    
I read these articles and changed my program.please tell me if it's correct.
bool statusFlag=true;
using (TransactionScope scope = new TransactionScope())
{
foreach (var loan in _loanlist)
{
if (!pda.InsertLoan(new Loan{
prsnlcd = prsncod,
LoanCode = loan.LoanCode,
LoanStatus = loan.LoanAmountRemain == 0 ? true : false}))
{
statusFlag=false;
break;
}
}
if(statusFlag=true)
scope.Complete();
}
Try something like this

//Pass your values in List of class object
   private static bool Insert(List<loan> loanList)
   {
       bool Result = false;
       SqlConnection db = new SqlConnection("connstringhere");
       SqlTransaction transaction;

       db.Open();
       transaction = db.BeginTransaction();
       try
       {
           foreach (Loan item in loanList)
           {
               new SqlCommand("INSERT INTO TransactionDemo " +
            "( prsnlcd ,LoanCode,LoanStatus) VALUES ('"+item.prsnlcd+"','"+item.LoanCode+"','"+item.LoanStatus+"');", db, transaction)
            .ExecuteNonQuery();
           }
           transaction.Commit();
           Result = true;
       }
       catch (SqlException sqlError)
       {
           transaction.Rollback();
           Result = false;
       }
       db.Close();
       return Result;
   }
 
Share this answer
 
v2
Comments
mit62 10-May-14 10:59am    
thanks for your reply.I have another question, how can I send this list of object to a Stored Procedure?
[no name] 10-May-14 13:38pm    
It's so simple use you can do it easily. I have just given a format below see it

void fronInsertMethod()//You can give you methodname anything
{
List<loan> loanList=new List<loan>();
foreach ( var loan in _loanlist)
{
loan ln=new loan();
ln.prsnlcd = prsncod;
ln.LoanCode = loan.LoanCode;<br>
ln.LoanStatus = loan.LoanAmountRemain == 0 ? true : false})) ;
loanList.Add(ln);
}
//Call your insert method and pass this list parameter
Insert(loanList);
}

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