I am getting an error of
My code is
public async Task START_PROCESSING_ISIN_FILE_IMPORT(int eximId, int userId, string MasterName)
{
Utl.Write_Process_Log(MasterName + " ISIN Master Table Insertion Satred", "Start");
EximAppService Exim = new();
await Exim.UpdateEximDesc(new EximUpdateDto { EximId = eximId, Remark = MasterName + " ISIN File Import Started." });
var PendingISINFile = await GetPendingISINFileExecution(eximId);
//var ISINData = _dbContext.IsinMasterCore.ToList();
var incomingIsins = PendingISINFile
.Where(x => x.Flag == "N")
.Select(x => x.FILE_INPUT.Split(',')[3])
.Distinct()
.ToList();
var ISINData = _dbContext.IsinMasterCore.Where(x => incomingIsins.Contains(x.ISIN_CODE)).ToList();
using (var transaction = await _dbContext.Database.BeginTransactionAsync())
{
try
{
int totalInsertRecords = 0;
int totalUpdateRecords = 0;
int totalRecords = 0;
if (PendingISINFile.Count > 0)
{
List InsertResult = new();
List UpdateResult = new();
foreach (var Line in PendingISINFile)
{
var Data = new List();
if (Line.Flag == "N")
{
//Data = await GetCommaSepratedData(Line.FILE_INPUT);
//string ISIN1 = Data[3];
var IsinMatches = PendingISINFile.Where(e => e.FILE_INPUT.Split(',')[3].Contains(Line.FILE_INPUT.Split(',')[3]) && e.Flag == "N").ToList();
if (IsinMatches.Count > 1)
{
var lastRecord = IsinMatches.Last();
Data = await GetCommaSepratedData(lastRecord.FILE_INPUT);
PendingISINFile.Where(e => e.FILE_INPUT.Split(',')[3].Contains(Line.FILE_INPUT.Split(',')[3])).ToList().ForEach(e => e.Flag = "Y");
}
else
{
Data = await GetCommaSepratedData(Line.FILE_INPUT);
}
string ISIN = Data[3];
Decimal PAR_VALUE = 0;
Decimal.TryParse(Data[38], out PAR_VALUE);
string[] formats = { "MM/dd/yyyy", "M/d/yyyy" };
var MATURITY_DATE = DateTime.ParseExact(Data[32], formats, CultureInfo.InvariantCulture, DateTimeStyles.None);
var ISSUE_DATE = DateTime.ParseExact(Data[11], formats, CultureInfo.InvariantCulture, DateTimeStyles.None);
if (ISINData != null)
{
var ISIN_Data = ISINData.Where(e => e.ISIN_CODE.Equals(ISIN)).FirstOrDefault();
if (ISIN_Data != null)
{
ISIN_Data.SHORTNAME = Data[4].Replace("\"", string.Empty);
ISIN_Data.ISIN_DESCRIPTION = Data[5].Replace("\"", string.Empty);
ISIN_Data.TYPE = Data[8];
ISIN_Data.STATUS = Data[9];
ISIN_Data.ISSUE_DATE = ISSUE_DATE;
ISIN_Data.MATURITY_DATE = MATURITY_DATE;
ISIN_Data.PAR_VALUE = PAR_VALUE;
UpdateResult.Add(ISIN_Data);
}
else
{
InsertResult.Add(new IsinMasterCore
{
ISIN_CODE = ISIN,
SHORTNAME = Data[4].Replace("\"", string.Empty),
ISIN_DESCRIPTION = Data[5].Replace("\"", string.Empty),
TYPE = Data[8],
STATUS = Data[9],
ISSUE_DATE = ISSUE_DATE,
MATURITY_DATE = MATURITY_DATE,
PAR_VALUE = PAR_VALUE,
IS_APPROVED = "N",
CHECKED = "Y",
MAKER_DATETIME = DateTime.Now,
MAKER_ID = userId,
CHECKER_DATETIME = DateTime.Now,
CHECKER_ID = userId
});
}
}
else
{
InsertResult.Add(new IsinMasterCore
{
ISIN_CODE = ISIN,
SHORTNAME = Data[4].Replace("\"", string.Empty),
ISIN_DESCRIPTION = Data[5].Replace("\"", string.Empty),
TYPE = Data[8],
STATUS = Data[9],
ISSUE_DATE = ISSUE_DATE,
MATURITY_DATE = MATURITY_DATE,
PAR_VALUE = PAR_VALUE,
IS_APPROVED = "N",
CHECKED = "Y",
MAKER_DATETIME = DateTime.Now,
MAKER_ID = userId,
CHECKER_DATETIME = DateTime.Now,
CHECKER_ID = userId
});
}
}
}
totalInsertRecords = InsertResult.Count;
totalUpdateRecords = UpdateResult.Count;
totalRecords = totalInsertRecords + totalUpdateRecords;
if (totalInsertRecords > 0)
{
int processed = 0;
const int batchSize = 500;
while (processed < totalInsertRecords)
{
var batch = InsertResult
.Skip(processed)
.Take(batchSize)
.ToList();
await _dbContext.AddRangeAsync(batch);
await _dbContext.SaveChangesAsync();
//Move to the next batch
processed += batchSize;
}
}
if (totalUpdateRecords > 0)
{
int processed = 0;
const int batchSize = 500;
while (processed < totalUpdateRecords)
{
var batch = UpdateResult
.Skip(processed)
.Take(batchSize)
.ToList();
_dbContext.IsinMasterCore.UpdateRange(batch);
await _dbContext.SaveChangesAsync();
// Move to the next batch
processed += batchSize;
}
}
}
else
{
await Exim.Log_Update(new LogUpdateDto { EximId = eximId, Status = "F", Remark = "No Records Found For Exim :" + eximId });
await Exim.UpdateExim(new EximUpdateDto { EximId = eximId, Status = "F", Remark = "No Records Found For Exim :" + eximId });
return;
}
await Exim.Log_Update(new LogUpdateDto { EximId = eximId, Status = "S", Remark = MasterName + " ISIN File Import Sucessfully Completed." });
await Exim.UpdateExim(new EximUpdateDto { EximId = eximId, Status = "S", Remark = MasterName + " ISIN File Import Sucessfully Completed." + totalRecords + " - Records Imported." });
await transaction.CommitAsync();
Utl.Write_Process_Log(MasterName + " ISIN Table Insertion Ended", "END");
}
catch (Exception ex)
{
await transaction.RollbackAsync();
await Exim.Log_Update(new LogUpdateDto { EximId = eximId, Status = "F", Remark = "Failed To Import " + MasterName + " ISIN File Records" });
await Exim.UpdateExim(new EximUpdateDto { EximId = eximId, Status = "F", Remark = ex.Message });
Utl.Write_Log(ex.Message, ex.StackTrace, "ISINFileImportAppService", "START_PROCESSING_ISIN_FILE_IMPORT");
}
}
}
The database operation was expected to affect 1 row(s), but actually affected 0 row
Sophia CarterPosted Apr 30, 2025, 1:40 PM
It seems like the error you are encountering is related to your database operation not affecting any rows even though it was expected to affect 1 row. This discrepancy could be due to various reasons such as data not being present in the database as expected, incorrect data matching, or issues with the logic of the operation.
In your code, you are first retrieving a list of PendingISINFile and then filtering out certain entries based on a condition. Subsequently, you are performing operations to insert or update records in the database. The specific issue you are facing might be related to how you are handling the data matching and updates in the database.
Here are a few considerations to troubleshoot this issue:
1. Data Matching: Verify that the incomingIsins and ISINData lists contain the expected data for matching. Ensure that the logic used to match records from PendingISINFile with existing data in the database is accurate.
2. Insert and Update Logic: Double-check the logic used for inserting new records and updating existing ones. Make sure that you are correctly differentiating between new records to be inserted and existing records to be updated.
3. Transaction Handling: Ensure that the database transaction is being managed properly. Check if the transaction is committed after all the operations are completed successfully and rolled back in case of any errors.
4. Exception Handling: Review how exceptions are handled in your code. Confirm that all possible exceptions are captured, logged, and appropriately reported to understand the root cause of the issue.
By carefully reviewing these aspects of your code and data handling, you may be able to pinpoint the cause of the error where the database operation is not affecting the expected number of rows. If you need further assistance or have additional details to share, feel free to let me know!