-1
sb.AppendLine("Employee Id,First Name,Last Name,Email,Username,Password,Role,Group Name,Country Code, Supervisor Id, Hire Date, Birth Date");

for (int i = 0; i < dt.Rows.Count; i++)
{
    String[] empid = dt.Rows[i]["EmpId"].ToString().Split(new Char[] { '-' });
    sb.AppendLine(Convert.ToInt32(empid[0]).ToString("000000") + "," + dt.Rows[i]["FirstName"] + "," + dt.Rows[i]["LastName"].ToString().Replace(",", " ") +
         ",," + dt.Rows[i]["Email"] + ",reward," + dt.Rows[i]["Role"] + ",CCCC," + ",," + ",," + dt.Rows[i]["EmployeeHireDate"] + "," + dt.Rows[i]["EmployeeBirthDate"]);
}

email field needs to be empty, username needs to be the email, country code needs to be empty, supervisor id needs to be empty,

Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794
Dramian
  • 3
  • 2
  • What have you tried so far? What's your question? This is not a coding service. Please read [how to ask](https://stackoverflow.com/help/how-to-ask) and include a [minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example). https://idownvotedbecau.se/noattempt/ https://idownvotedbecau.se/unclearquestion https://idownvotedbecau.se/nomcve/ – Jesse Jun 23 '22 at 21:21
  • needs to know how use use the quotes and comas for the empty fields. sorry new at c# – Dramian Jun 23 '22 at 21:23
  • reading or writing csv - use csvhelper library – pm100 Jun 23 '22 at 21:26
  • "", is enough for empty string or just a comma. "John", , "Doe" is a sample for 3 fields. – Cetin Basoz Jun 23 '22 at 21:27
  • for a csv file they have to be separated by a comma so i guess "," would work for all of them that need to be empty? – Dramian Jun 23 '22 at 21:29
  • Does this answer your question? [Writing data into CSV file in C#](https://stackoverflow.com/questions/18757097/writing-data-into-csv-file-in-c-sharp) Use a proper library, don't do this yourself – Charlieface Jun 23 '22 at 22:20

1 Answers1

0

Replace this segement:

+ ",CCCC," + ",," + ",," +

with this:

+ ",CCCC,,," + 

But you'll really do MUCH better with a dedicated csv library, of which there are several good options on NuGet.

Additionally, if you're using StringBuilder because you heard it's faster, remember that faster is relative. StringBuilder is faster than string concatenation for this kind of work, but if you're ultimately going to writing to a network stream, file, or web response you'll do even better using a StreamWriter

Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794
  • okay. I get that part but the part ___ToString().Replace(",", " ") + ",," + dt____ after the plus sign ",," does it need 2 commas or just one to represent the one space – Dramian Jun 23 '22 at 21:34
  • @Dramian You want two commas there: one to end the `LastName` field, which hasn't been closed yet, and one to end the `Email` field that comes after, because you want it to be blank. – Joel Coehoorn Jun 23 '22 at 21:37