Catch bad file, invalid csv, etc.

This commit is contained in:
Tim Young 2017-08-01 14:45:38 -05:00
parent 405fe94953
commit f749914338

View File

@ -414,57 +414,64 @@ namespace EduNetworkBuilder
//Now we have a csv file. Try to parse it //Now we have a csv file. Try to parse it
bool HadError = false; bool HadError = false;
using (TextFieldParser parser = new TextFieldParser(mydialog.FileName)) try
{ {
parser.TextFieldType = FieldType.Delimited; using (TextFieldParser parser = new TextFieldParser(mydialog.FileName))
parser.SetDelimiters(",");
while (!parser.EndOfData)
{ {
//Process row parser.TextFieldType = FieldType.Delimited;
int index = 0; parser.SetDelimiters(",");
string[] fields = parser.ReadFields(); while (!parser.EndOfData)
StudentHolder TStudent = new StudentHolder();
foreach (string field in fields)
{ {
//TODO: Process field //Process row
if (index == 0) int index = 0;
string[] fields = parser.ReadFields();
StudentHolder TStudent = new StudentHolder();
foreach (string field in fields)
{ {
TStudent.Name = field; //TODO: Process field
if (!NB.ValidateUsername(field)) if (index == 0)
{ {
HadError = true; TStudent.Name = field;
InvalidUsers.Add(TStudent.Name); if (!NB.ValidateUsername(field))
} {
HadError = true;
InvalidUsers.Add(TStudent.Name);
}
}
if (index == 1)
{
TStudent.FullName = field;
if (!NB.ValidateFullName(field))
{
HadError = true;
InvalidFullName.Add(TStudent.Name);
} }
} if (index == 1)
if (index == 2)
{
TStudent.Password = field;
if (!NB.ValidatePassword(field))
{ {
HadError = true; TStudent.FullName = field;
InvalidPasswords.Add(TStudent.Name); if (!NB.ValidateFullName(field))
{
HadError = true;
InvalidFullName.Add(TStudent.Name);
}
} }
if (index == 2)
{
TStudent.Password = field;
if (!NB.ValidatePassword(field))
{
HadError = true;
InvalidPasswords.Add(TStudent.Name);
}
}
index++; //Track which field we are in.
} }
index++; //Track which field we are in. tList.Add(TStudent);
} }
tList.Add(TStudent); }
if (HadError)
{
ShowInvalid("Username", InvalidUsers);
ShowInvalid("Full Name", InvalidFullName);
ShowInvalid("Password", InvalidPasswords);
} }
} }
if(HadError) catch (Exception except)
{ {
ShowInvalid("Username", InvalidUsers); MessageBox.Show(except.ToString());
ShowInvalid("Full Name", InvalidFullName);
ShowInvalid("Password", InvalidPasswords);
} }
} }