the information I enter in my form is not being stored in my CSV file. Just a blank file. the file name updates with todays date.
here is my code.
private void btnSave_Click(object sender, EventArgs e)
{
string date = DateTime.Today.ToString("dd-MM-yyyy"); //get today's date
string filePath = "Policy_" + date + ".csv"; //create a name of the new csv file (including the date)
string delimiter = ","; //comma needed to create new csv file
StringBuilder sb = new StringBuilder();
foreach (DriverDetails driverDetails in driverDet) //go through the List called DriverDetails and examine each object in turn
{
sb.AppendLine(driverDetails.Title + delimiter + driverDetails.FName + delimiter + driverDetails.SName + delimiter + driverDetails.Dob + delimiter + driverDetails.Phone + delimiter + driverDetails.Email + delimiter + driverDetails.Employment + delimiter + driverDetails.Marital + delimiter + driverDetails.HouseNo + delimiter + driverDetails.Street + delimiter + driverDetails.Postcode + delimiter);//uild up a String containing all the data in the List
}
File.WriteAllText(filePath, sb.ToString());
//File.AppendAllText(filePath, sb.ToString()); //add the new string (made up of SEVERAL lines, each representing data from ONE order) to the end of the csv file
MessageBox.Show("Driver details saved to file");
}
I use the following code to fill the driverDet, and then can write the data to the csv file.
class DriverDetails
{
public string Title { get; set; }
public string FName { get; set; }
public string SName { get; set; }
}
List<DriverDetails> driverDet = new List<DriverDetails> {
new DriverDetails { Title = "T1", FName = "F1", SName = "S1"},
new DriverDetails { Title = "T2", FName = "F2", SName = "S2"},
new DriverDetails { Title = "T3", FName = "F3", SName = "S3"}};