Row Not Found Or Changed
-
I'm importing and updating a parts table from a CSV file into a table using Linq To Sql:
public int ImportPartsFile(string FileName)
{
using (SparesDataContext dc = getDataContext())
{
using (TextFieldParser tfp = new TextFieldParser(FileName))
{
int lineNo = 0;
int imported = 0;
int updated = 0;tfp.TextFieldType = Microsoft.VisualBasic.FileIO.FieldType.Delimited; tfp.SetDelimiters(","); string\[\] words; while (!tfp.EndOfData) { words = tfp.ReadFields(); lineNo++; string partNo = words\[0\]; string description = words\[1\]; string priceString = words\[2\]; if (partNo.Trim() == string.Empty || priceString == string.Empty) { continue; } priceString = priceString.Replace("$", ""); int price = Convert.ToInt32(Convert.ToDecimal(priceString)); var part = (from p in dc.tblHPParts where p.PartNumber.Trim().ToLower() == partNo.Trim().ToLower() select p).FirstOrDefault(); if (part == null) { tblHPPart partRow = new tblHPPart { PartNumber = partNo, Description = description, Price = price }; dc.tblHPParts.InsertOnSubmit(partRow); dc.SubmitChanges(); imported++; } else { part.Price = price; dc.SubmitChanges(); updated++; } } return imported; } }
}
At the point where I do
else
{
part.Price = price;
dc.SubmitChanges();
updated++;
}if get
Row not found or changed
There are 3676 lines in the CSV file, and it dies on line 40
022N02177 ,XEROX PICKUP ROLLER ,$13
Anyone know what's wrong?
If it's not broken, fix it until it is
-
I'm importing and updating a parts table from a CSV file into a table using Linq To Sql:
public int ImportPartsFile(string FileName)
{
using (SparesDataContext dc = getDataContext())
{
using (TextFieldParser tfp = new TextFieldParser(FileName))
{
int lineNo = 0;
int imported = 0;
int updated = 0;tfp.TextFieldType = Microsoft.VisualBasic.FileIO.FieldType.Delimited; tfp.SetDelimiters(","); string\[\] words; while (!tfp.EndOfData) { words = tfp.ReadFields(); lineNo++; string partNo = words\[0\]; string description = words\[1\]; string priceString = words\[2\]; if (partNo.Trim() == string.Empty || priceString == string.Empty) { continue; } priceString = priceString.Replace("$", ""); int price = Convert.ToInt32(Convert.ToDecimal(priceString)); var part = (from p in dc.tblHPParts where p.PartNumber.Trim().ToLower() == partNo.Trim().ToLower() select p).FirstOrDefault(); if (part == null) { tblHPPart partRow = new tblHPPart { PartNumber = partNo, Description = description, Price = price }; dc.tblHPParts.InsertOnSubmit(partRow); dc.SubmitChanges(); imported++; } else { part.Price = price; dc.SubmitChanges(); updated++; } } return imported; } }
}
At the point where I do
else
{
part.Price = price;
dc.SubmitChanges();
updated++;
}if get
Row not found or changed
There are 3676 lines in the CSV file, and it dies on line 40
022N02177 ,XEROX PICKUP ROLLER ,$13
Anyone know what's wrong?
If it's not broken, fix it until it is
As observed in the data of line 40 given above in
022N02177
0
iszero
instead of letter'O'
. But if that is the case and the part number is not found then the execution should go in to theif (part == null)
block. But I want to share what I have noticed and please see whether the issue is because of that.