I'm working on a Address book that has 4 fields: Name, Email, Phone, Birthday. I have so far been able to get the program to add a new entry in the csv when you click add (it currently does no sort of verification). Right now I am trying to get the search function to work which I am having trouble with. If all I had to do was read the file in with streamreader I can do that, but how do I read in an entry split on commas so it can store each split read into a field. I need those split reads stored in fields to compare against the searched term. Thus far I have something that looks like this:
-
string[] fields;
-
string recordIn;
-
-
recordIn = reader.ReadLine();
-
-
-
while(recordIn != null)
-
{
-
string line = reader.ReadLine();
-
fields = line.Split(DELIM);
-
entryObject.Name = fields[0];
-
entryObject.Email = fields[1];
-
entryObject.Phone = fields[2];
-
entryObject.Birthday = fields[3];
-
-
-
-
-
if (entryObject.Name == searchTerm || entryObject.Email == searchTerm
-
|| entryObject.Phone == searchTerm || entryObject.Birthday == searchTerm)
-
{
-
-
string searchResults = string.Format("Name:\t{0}\nEmail:\t{1}\n"+
-
"Phone:\t{2}\nBirthday:\t{3}"
-
,entryObject.Name, entryObject.Email,
-
entryObject.Phone, entryObject.Birthday);
-
-
MessageBox.Show(searchResults, "Entry Found", MessageBoxButtons.OK);
-
entryFound = true;
-
break;
-
}
-
}
-
The problem if as far as I can tell is that the program reads a line, then it enters a while loop but it just sits and compares the first line it read in never failing a test because it just does the same thing over and over. Is that right? I feel like if I could read the entire csv then do something like:
-
for(int i = 0; i < recordIn.Length; i++)
-
string[] line = reader.readline();
-
Then it would look at the whole file and say, there are this many entries, we need to read the next line until we have read each line and compared the fields. Problem is I don't know exactly how I would do that or if it is the most practical way to do what I am trying to achieve.
Again I'm trying to read the entries of a csv and see if any of the 4 fields per entry match the searched term.
Any suggestions are greatly appreciated.