Monday, November 30, 2009

Check Duplicate Row while working with CSV file

Hi All,

If you are working with CSV file parsing, there is always a good practice to check duplicate rows while parsing data. If prevents the system crashing due to duplicate record entry in data and we could make proper exception handling with Friendly message to our users.

So, here i have make one usable function with help of C# as code language.

435 private int CheckDuplicateRows(string _SaveLocation)

436 {

437 if (!string.IsNullOrEmpty(_SaveLocation))

438 {

439 string[] FileContent = File.ReadAllLines(_SaveLocation);

440 ArrayList al = new ArrayList();

441 int lineNo = 1;

442 //ignore the header row

443 for (int i = 1; i <= FileContent.Length - 1; i++)

444 {

445 lineNo += 1;

446 if (al.Contains(FileContent[i]))

447 {

448 return lineNo;

449 }

450 else

451 {

452 al.Add(FileContent[i]);

453 }

454 }

455 return 1;

456 }

457 else return 1;

458 }


Thats all, and your code is safe to manage the exception of DuplicateRow, it will return 1 if there is no duplicate row else will return the duplicate row number.

Happy Coding :)

No comments:

Post a Comment