My SQL transaction throws exception on Commit(). This is the first time I use transactions with SqlCommands so maybe I'm making some error in code. I saw other questions regardings same error but none of them helped. I tried explicitly calling Close() on reader but no use.
using (var selectModifiedCmd = new SqlCommand(selectModified, conn, trans))
{
try
{
decimal qty, qtyPerUOM, weight, weightKg;
string no, binCode, binText, shelfNo, mainZone, sourceTu, destNo, cluster;
int lineNo, corridor, sortAsc, sortDesc, rowOrder;
short pricePerKg;
using (var reader = selectModifiedCmd.ExecuteReader())
{
while (reader.Read())
{
........
using (var updateModifiedCmd = new SqlCommand(updateModified, conn, trans))
{
........
updateModifiedCmd.ExecuteNonQuery();
}
using (var returnModifiedCmd = new SqlCommand(returnModified, conn, trans))
{
returnModifiedCmd.Parameters.AddWithValue("no", no);
returnModifiedCmd.Parameters.AddWithValue("lineNo", lineNo);
returnModifiedCmd.ExecuteNonQuery();
}
trans.Commit();
Globals.WriteLog(MethodBase.GetCurrentMethod().Name, String.Format(logSuccess, no, lineNo, binCode, qty));
}
}
}
catch (SqlException ex)
{
var trace = new StackTrace(ex, true);
Globals.WriteLog(
MethodBase.GetCurrentMethod().Name,
ex.Message + " At line: " + trace.GetFrame(trace.FrameCount - 1).GetFileLineNumber());
try
{
trans.Rollback();
}
catch (Exception exRollback)
{
Globals.WriteLog("Rollback error: ", exRollback.Message);
}
}
}