C# .NET - problem in handing exception - Asked By Neeraj on 29-Mar-13 02:50 AM

i have function like
function()
{  try
                    {                      
                            InsertFieldData(Field.FieldId, Field.FieldCode, Field.FieldName, Field.FieldType,objAggrMBSDAL);
                            InsertFieldData_SMA(Field.FieldId, Field.FieldCode, Field.FieldName, Field.FieldType,objSMAMBSDAL);                        
                    }
                    catch (Exception ex)
                    {
                        objAggrMBSDAL.RollbackTransaction();
                        objSMAMBSDAL.RollbackTransaction();
                      
                    }
}
public void InsertFieldData(string fieldId, string fieldCode, string fieldName, string fieldType MBSDAL mbsDAL)
        {
            try
            {
                mbsDAL.AddParameter("FieldId", fieldId, false);
                mbsDAL.AddParameter("FieldCode", fieldCode, false);
                mbsDAL.AddParameter("FieldName", fieldName, false);
                mbsDAL.AddParameter("FieldType", fieldType, false);
                mbsDAL.ExecuteNonQuery("InsertFieldData", CommandType.StoredProcedure, MBSDAL.ConnectionState.KeepOpen);
            }
            catch (Exception ex)
            {
                mbsDAL.CloseConnection();
                Logging.LogEvent(ex, Category.Exception, Priority.Normal, Convert.ToInt32(VendorSynchGlobal.EventId_Manager.Page_VendorSync), VendorSynchGlobal.strGuid, "InsertFieldData", VendorSynchGlobal.strAppliaction);


            }
        }
and in 
InsertFieldData() function i have wrritten my code with try catch block but when any exception comes rollback is working.. Should i remove try Catch from InsertFieldData function or there there is some other way to make roll back function working ..



Danasegarane Arunachalam replied to Neeraj on 29-Mar-13 01:03 PM
Yes you can remove that, as long as you are not going to anything there. As if the any errors are thrown at InsertFieldData  are caught at function and this will force the rollback of transaction. If you still want the try catch there at InsertFieldData and you can rethrow the exception as


public void InsertFieldData(string fieldId, string fieldCode, string fieldName, string fieldType MBSDAL mbsDAL)
        {
            try
            {
                mbsDAL.AddParameter("FieldId", fieldId, false);
                mbsDAL.AddParameter("FieldCode", fieldCode, false);
                mbsDAL.AddParameter("FieldName", fieldName, false);
                mbsDAL.AddParameter("FieldType", fieldType, false);
                mbsDAL.ExecuteNonQuery("InsertFieldData", CommandType.StoredProcedure, MBSDAL.ConnectionState.KeepOpen);
            }
            catch (Exception ex)
            {
                mbsDAL.CloseConnection();
                Logging.LogEvent(ex, Category.Exception, Priority.Normal, Convert.ToInt32(VendorSynchGlobal.EventId_Manager.Page_VendorSync), VendorSynchGlobal.strGuid, "InsertFieldData", VendorSynchGlobal.strAppliaction);
    throw ;


            }
        }

and this will re throw the exception and this will be handled by the calling method.
Neeraj replied to Danasegarane Arunachalam on 02-Apr-13 03:32 AM
thank u so much for help..