Click here to Skip to main content
15,888,351 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
I am setting up a website which will allow clubs to upload their membership lists as CSV files.

I have set up a four step process (four separate web pages):
1. Upload the file
2. Read column headings and map to database fields
3. Read the whole CSV file and check every field against the database requirements
4. If there are no major errors, load all the data into the database.

In step 3, I produce a detailed report (e.g. "xyz" is not a valid email address). To do this, I would really like to utilize the Data Annotations associated with each of the fields in the model, but I can't figure out how to access them.

Currently, I have set up field-level checks using information manually transcribed from the model, but this is very unsatisfactory since changing a StringLength setting in the model would mean remembering to update the validation routine separately.

I have googled every combination of search terms I can think of to try to find how to validate data without using the "SaveChanges()" function, but to no avail.

Surely I can't be the only person in the world to want to do something like this?
Posted

You should be able to get errors when settings the parameters:

C#
Employee emp = new Employee();

 try
 {
     emp.IdNumber = 9999999;
 }
 catch (ValidationException ex)
 {
     Console.WriteLine(ex.Message);
 }
 
Share this answer
 
Comments
John Mackenzie 1-Sep-15 15:39pm    
I tried your suggestion Alexander, but the error was not trapped at the time the model was changed. The validation is apparently only done when the SaveChanges() function is called.

This was the code I used:

MemberDetail mbr = new MemberDetail();
try
{
mbr.EmailAddress = "thisIsNotAnEmailAddressAndItIsTooLongAnywaySoItShouldCauseAProblemForTwoReasons";
}
catch (System.ComponentModel.DataAnnotations.ValidationException ex)
{
ViewBag.errorMessage = ex.Message;
//throw;
}

The MemberDetail object contains the following definition for EmailAddress:

[Display(Name = "Email Address")]
[DisplayFormat(ConvertEmptyStringToNull = true)]
[EmailAddress]
[StringLength(64, ErrorMessage = "The email address can be up to 64 characters long.")]
public string EmailAddress { get; set; }
If all updates are done in a transaction, then any undected error will cause the data to not be saved. However, if you want custom messages, then you have to validate first.

But remember that in multiusers database, someone else might make a change between your validation and the actual commit thus it might not always works.
 
Share this answer
 
v2
Comments
John Mackenzie 1-Sep-15 15:05pm    
Thank you for your suggestions Alexander and Philippe. I tried your suggestion Alexander, but the error was not trapped at the time the model was changed. The validation is apparently only done when the SaveChanges() function is called.

This was the code I used:

MemberDetail mbr = new MemberDetail();
try
{
mbr.EmailAddress = "thisIsNotAnEmailAddressAndItIsTooLongAnywaySoItShouldCauseAProblemForTwoReasons";
}
catch (System.ComponentModel.DataAnnotations.ValidationException ex)
{
ViewBag.errorMessage = ex.Message;
//throw;
}

The MemberDetail object contains the following definition for EmailAddress:

[Display(Name = "Email Address")]
[DisplayFormat(ConvertEmptyStringToNull = true)]
[EmailAddress]
[StringLength(64, ErrorMessage = "The email address can be up to 64 characters long.")]
public string EmailAddress { get; set; }
Philippe Mori 1-Sep-15 15:30pm    
Reply to the appropriate solution as otherwise the other user won't receive an email and won't know that you still have questions.
John Mackenzie 1-Sep-15 15:40pm    
Apologies for that Philippe.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900