Guest User

Untitled

a guest
Jan 17th, 2019
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.65 KB | None | 0 0
  1. protected void ReadData(string filePath, bool upload)
  2. {
  3. StringBuilder sb = new StringBuilder();
  4.  
  5. #region upload
  6. if (upload == true) // CSV file upload chosen
  7. {
  8.  
  9. using (CsvReader csv = new CsvReader(new StreamReader(filePath), true)) // Cache CSV file to memory
  10. {
  11. int fieldCount = csv.FieldCount; // Total number of fields per row
  12. string[] headers = csv.GetFieldHeaders(); // Correct CSV headers stored in array
  13.  
  14. SortedList<int, string> errorList = new SortedList<int, string>(); // This list will contain error values
  15. ORCData data = new ORCData();
  16.  
  17. bool errorFlag = false;
  18. int errorCount = 0;
  19.  
  20. // Check if headers are correct first before reading data
  21. if (headers[0] != "first name" || headers[1] != "last name" || headers[2] != "job title" || headers[3] != "email address" || headers[4] != "telephone number" || headers[5] != "company" || headers[6] != "research manager" || headers[7] != "user card number")
  22. {
  23. sb.Append("Headers are incorrect");
  24. }
  25.  
  26. else
  27. {
  28. while (csv.ReadNextRecord())
  29. try
  30. {
  31. //Check csv obj data for valid values
  32. for (int i = 0; i < fieldCount; i++)
  33. {
  34. if (i == 0 || i == 1) // FirstName and LastName
  35. {
  36. if (Regex.IsMatch(csv[i].ToString(), "^[a-z]+$", RegexOptions.IgnoreCase) == false) //REGEX letters only min of 5 char max of 20
  37. {
  38. errorList.Add(errorCount, csv[i]);
  39. errorCount += 1;
  40. errorFlag = true;
  41. string text = csv[i].ToString();
  42. }
  43. }
  44. }
  45.  
  46. if (errorFlag == true)
  47. {
  48. sb.Append("<b>" + "Number of Error: " + errorCount + "</b>");
  49. sb.Append("<ul>");
  50. foreach (KeyValuePair<int, string> key in errorList)
  51. {
  52. sb.Append("<li>" + key.Value + "</li>");
  53. }
  54. }
  55. else // All validation checks equaled to false. Create User
  56. {
  57.  
  58. string message = ORCLdap.CreateUserAccount(rootLDAPPath, svcUsername, svcPassword, csv[0], csv[1], csv[2], csv[3], csv[4], csv[5], csv[7]);
  59. // TODO: Add to object here
  60. sb.Append(message);
  61. //sb.Append("<b>New user data uploaded successfully</b>");
  62. }
  63.  
  64. }// end of try
  65.  
  66.  
  67.  
  68. catch (Exception ex)
  69. {
  70. sb.Append(ex.ToString());
  71. }
  72.  
  73. finally
  74. {
  75. lblMessage.Text = sb.ToString();
  76. sb.Remove(0, sb.Length);
  77. hdnRdoSelection.Value = "1";
  78. }
  79. }
  80. }
  81.  
  82. }
  83. #endregion
  84.  
  85. dynamic settings= new ExpandoObject();
Add Comment
Please, Sign In to add comment