0

I am developing an Address Book using C#.

i have a list which is displayed in an DataGridView but I want to save the list and not the datagridview into a text file.

When the form loads i have the code:

string[] parts = line.Split(','); // the word line throws an error saying that it does not exist in the current context
Person p = new Person(parts[0], parts[1], parts[2], parts[3], parts[4], parts[5], parts[6]);
AddressBook.Persons.Add(p);

I then have this code in the datalayer to save the list:

string filePath = @"c:\test.txt";
p.ToString(); // it does not recognise p

Can anyone help?

Otiel
  • 18,404
  • 16
  • 78
  • 126
  • Search for keyword `Serialization`. XmlSerializer, DataContractSerializer, JavaScriptSerializer etc. – I4V May 26 '13 at 16:27
  • Errors speak of themselves. But we're going to need more code to see where you have a problem. – Otiel May 26 '13 at 17:09
  • "It does not recognise p" -- Do you have a p variable in that scope? We need methods to see what it is you are specifically trying to do. – TyCobb May 26 '13 at 17:25
  • Thanks for the feedback. I have this in my Person class and was hoping it would pick it up but im not 100% sure how. public Person(string p, string p_2, string p_3, string p_4, string p_5, string p_6, string p_7) { this.p = FirstName; this.p_2 = LastName; this.p_3 = Address; this.p_4 = Town; this.p_5 = County; this.p_6 = PostCode; this.p_7 = PhoneNumber; } – user2422643 May 26 '13 at 17:38

1 Answers1

0

A simple and easy to use method for writing into a file is File.WriteAllText( filePath, p.ToString()). You have also to override the method ToString in the class Person, so you can return the information you want to save to file, otherwise it will, as default, return the class' name as string. If you need a speedest way you should use a StreamWriter.

Nico.S
  • 753
  • 8
  • 12