Hi
I have below code & i want if error comes then it should RollBack all transactions
C#
foreach (var colum in Result)
{
DateTime? vSessionDate = null;
DateTime? vSessionStartTime = null;
DateTime? vSessionEndTime = null;
SessionDeliveryCalendar Rec = bALSessionDeliveryCalendar.GetRecordDetail(colum.SessionDeliveryCalendarID);
String vTime = Convert.ToDateTime(colum.SessionStartTime).ToString("HH") + ":" + Convert.ToDateTime(colum.SessionStartTime).ToString("mm");
String vDT1 = Convert.ToDateTime(Rec.SessionDate).AddDays(7 * Convert.ToInt32(ddlPush.SelectedValue)).ToString("dd-MM-yyyy") + " " + vTime;
DateTime StartDate = DateTime.ParseExact(vDT1, "dd-MM-yyyy HH:mm", System.Globalization.CultureInfo.InvariantCulture);
vSessionStartTime = Convert.ToDateTime(StartDate.ToString("yyyy-MM-dd HH:mm"));
int Res = bALSessionDeliveryCalendar.UpdateSessionDeliveryCalendar(Rec);
if (Res != 0)
{
vStatus = "T";
}
else
{
vStatus = "F";
ShowMessage("Error", "Record Update Failed, Please Try Again", "error");
BindData(Convert.ToInt32(hdfGroupID.Value), Convert.ToInt32(hdfModuleID.Value), true);
GetGroupInfo();
}
}Thanks

Uday DodiyaRank #239 · 10.3K reputationPosted Apr 4, 2023, 2:39 AM EDT
Use TransactionScope as per following
try { using (TransactionScope scope = new TransactionScope()) { foreach (var colum in Result) { DateTime? vSessionDate = null; DateTime? vSessionStartTime = null; DateTime? vSessionEndTime = null; SessionDeliveryCalendar Rec = bALSessionDeliveryCalendar.GetRecordDetail(colum.SessionDeliveryCalendarID); String vTime = Convert.ToDateTime(colum.SessionStartTime).ToString("HH") + ":" + Convert.ToDateTime(colum.SessionStartTime).ToString("mm"); String vDT1 = Convert.ToDateTime(Rec.SessionDate).AddDays(7 * Convert.ToInt32(ddlPush.SelectedValue)).ToString("dd-MM-yyyy") + " " + vTime; DateTime StartDate = DateTime.ParseExact(vDT1, "dd-MM-yyyy HH:mm", System.Globalization.CultureInfo.InvariantCulture); vSessionStartTime = Convert.ToDateTime(StartDate.ToString("yyyy-MM-dd HH:mm")); int Res = bALSessionDeliveryCalendar.UpdateSessionDeliveryCalendar(Rec); if (Res != 0) { vStatus = "T"; } else { scope.Dispose(); // Rollback the transaction if an error occurs ShowMessage("Error", "Record Update Failed, Please Try Again", "error"); BindData(Convert.ToInt32(hdfGroupID.Value), Convert.ToInt32(hdfModuleID.Value), true); GetGroupInfo(); break; } } // If we get here, no errors occurred so we can commit the transaction scope.Complete(); } } catch (Exception ex) { }Manendra Paswan5 reputationPosted Apr 4, 2023, 2:15 AM EDT
Use TransactionScope
Jignesh KumarRank #91 · 22.9K reputationPosted Apr 4, 2023, 2:41 AM EDT
Hello Ramco,
Please refer this one which will help you, you need to use SQL transaction with try catch block,
if any error while execution then it will go to catch block. in catch block you can write rollback transaction
https://www.c-sharpcorner.com/uploadfile/john_charles/using-transactions-in-ado-net/
Anandu G NathRank #681 · 3.5K reputationPosted Dec 27, 2023, 3:42 AM EST
You can use
try-catchblocks in combination with theTransactionScopeclass in C# to manage transactions and rollback in case of an exception.