Click here to Skip to main content
15,884,962 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all i write the below code to delete the selected employees if all conditions are true as follows

C#
if(max())
{
   if(seq()
   {
      b_rsltcnt=true;
   }
    else
    {
     if(dbseq()
     {
      b_rsltcnt=false;
     }
     else
     {
     }
}


If my condition works then i will write the below code

C#
if (b_rsltcnt)
{
    foreach (int iEmpID1 in oAdmin.odictAddValues.Keys)
    {
       oAdmin.EmpID = iEmpID1;
       foreach (int iPayPeriodIDs1 in oAdmin.odictAddValues[iEmpID1].lstPayPeriodID)
       {
         lstdictPayPeriodID.Add(iPayPeriodIDs1);
       }
         foreach (DateTime dtPaymentYear1 in Admin.odictAddValues[iEmpID1].lstPaymentDate)
       {
          lstdictDateTime.Add(dtPaymentYear1);
       }
       foreach (int iPayYears1 in oAdmin.odictAddValues[iEmpID1].lstPayYear)
      { 
        lstdictPayYear.Add(iPayYears1);
      }
        for (int ipayID = 0; ipayID < lstdictPayPeriodID.Count; ipayID++)
       {
          oAdmin.Payperiodnumber = lstdictPayPeriodID[ipayID];
          for (int ipayDate = idtcnt; ipayDate < lstdictDateTime.Count; ipayDate++)
          {
            oAdmin.PaymentDate = lstdictDateTime[ipayDate];
            idtcnt++;
            break;
          }
           for (int ipayYear1 = iPayYearcnt; ipayYear1 < lstdictPayYear.Count; ipayYear1++)
           {
              oAdmin.PayYear = lstdictPayYear[ipayYear1];
              iPayYearcnt++;
              break;
            }
            mlocal_strStoredProcName = "usp_delete_payroll";
            if (oAdmin.deletePayRoll(mlocal_strStoredProcName))
            {
              oMsg.Message = "Deleted Sucessfully";
              oMsg.AlertMessageBox(out m_locallblMessage);
              Page.Controls.Add(m_locallblMessage);
              oAdmin.FedTaxID = ddlFedTaxID.SelectedValue;
              oAdmin.PayFrequency = ddlPaymentType.SelectedValue.ToString();
              mlocal_strStoredProcName = "uspSearchPayRoll";
              oAdmin.getPayRollDetails(out mlocal_ds, mlocal_strStoredProcName);
             //grdPayroll.Visible = true;
             grdPayroll.DataSource = mlocal_ds;
             grdPayroll.DataBind();
            if (mlocal_ds != null)
            {
              btnDelete.Visible = true;
            }
            else
              btnDelete.Visible = false;
           }
        }
     }
 }


My class file consists of delete code as follows

<pre lang="c#">public bool deletePayRoll(string storeproc)
        {
            m_bFlag = false;
            this.m_oConn = Utilities.GetConnection();
            try
            {
                if (m_oConn.State != ConnectionState.Open)
                {
                    m_oConn.Open();
                    m_oSqlTrans = m_oConn.BeginTransaction();

                }
                m_oCmd = new MySqlCommand(storeproc, m_oConn);
                m_oCmd.CommandType = CommandType.StoredProcedure;
                m_oCmd.Transaction = m_oSqlTrans;
                m_oCmd.Parameters.AddWithValue("_EmpId", EmpID);
                m_oCmd.Parameters.AddWithValue("_FedTaxID", FedTaxID);
                m_oCmd.Parameters.AddWithValue("_payperiodnumber", Payperiodnumber);
                m_oCmd.Parameters.AddWithValue("_payyear", PayYear);
                m_oCmd.Parameters.AddWithValue("_paymentdate", PaymentDate);

                if (m_oCmd.ExecuteNonQuery() > 0)
                {
                    m_oSqlTrans.Commit();
                    m_bFlag = true;
                }
            }
            catch (MySqlException oSqlEx)
            {
                m_oSqlTrans.Rollback();      
                m_sbErrMsg.Length = 0;
                m_sbErrMsg = Utilities.SqlErrorMessage(oSqlEx);
                //DB write the Error Log
                m_oErrlog.Add(m_sbErrMsg.ToString(), DateTime.Now);
            }
            catch (Exception oEx)
            {

                m_sbErrMsg = Utilities.ErrorMessage(oEx);
                //DB write the Error Log
                m_oErrlog.Add(m_sbErrMsg.ToString(), DateTime.Now);
            }

            finally
            {
                m_oConn.Close();
            }
            return m_bFlag;
        }


Assume that i have 5 records to delete and all my conditions as per written are always true. For suppose among 5 records 4 deleted successfully and while coming to 5th one i had some connection issues or some other issues and assume that an Exception causes will all the previously deleted records will be rollbacked or not
Posted
Updated 11-Sep-11 22:31pm
v2
Comments
OriginalGriff 12-Sep-11 4:17am    
Please, edit your question and correct the indenting - it is very difficult to work out what is happening when you have to match brackets by eye. Use the "Improve question" widget to edit your question.

1 solution

It is difficult to work out what is going on in most of your application, but in essence, the answer is "No". As soon as you call Commit, the changes are irrevocably made, even if you try to roll back on the same transaction later.
You appear to commit after every successful change to your database, which negates the use of transactions in the first place.
If you want to roll back all changes on any failure, then begin your transaction before you do any changes, rollback on the first failure and exit, or commit when all transactions are complete.
 
Share this answer
 
Comments
demouser743 12-Sep-11 4:32am    
How can i do that as per my requirement any sample code please
OriginalGriff 12-Sep-11 5:20am    
I can't - your code only calls the delete routine once, so I have no idea how the outside code works.
However, if it works the same as the code you have shown, I'm not sure I want to. :laugh:
Why do you have so many "loops" which always only execute a single time?
Why is your indentation such a mess? VS automatically indents for you, in your preferred style...
Why are you using Hungarian notation? Acronym variable names? Neither is recommended for C#, and Intellisense help you to use more readable names.
demouser743 12-Sep-11 5:23am    
Hi if its a simple delete then i will delete in normal but i would like to check 3 conditions like Maximum element for the selected ID exists or not if exists the i have to check out for the sequence then i have to delete. So i have written that many conditions
OriginalGriff 12-Sep-11 5:27am    
Possibly - but I'm talking about code like this:
for (int ipayDate = idtcnt; ipayDate < lstdictDateTime.Count; ipayDate++)
{
oAdmin.PaymentDate = lstdictDateTime[ipayDate];
idtcnt++;
break;
}
Which can only ever execute a single time...
demouser743 12-Sep-11 5:30am    
Ya as i have to pass single value each time for the delete method i am doing as it can not check the list

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