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
1 changed files with 44 additions and 37 deletions

View File

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