#region Functions
private void NewMemberFunction(object obj)
{
SelectedMember = new Member();
if (Members.Count < 1)
{
SelectedMember.LibraryCardNumber = 1;
}
else
{
SelectedMember.LibraryCardNumber = Members.Max(a => a.LibraryCardNumber) + 1;
}
Members.Add(SelectedMember);
}
private void SaveMemberFunction(object obj)
{
try
{
if (SelectedMember == null)
{
throw new Exception("Press new button to add new member");
}
if (SelectedMember.FirstName == null || SelectedMember.Roll == null)
{
throw new Exception("There are some error on the form.\nPlease completely fill the form.");
}
if (SelectedMember.FirstName.Length < 1)
{
throw new Exception("First Name is Required");
}
if (SelectedMember.Roll.Length < 1)
{
throw new Exception("Roll Number is required");
}
if (SelectedMember != null)
{
TextInfo textInfo = new CultureInfo("en-US", false).TextInfo;
if (SelectedMember.FirstName != null)
{
SelectedMember.FirstName = textInfo.ToTitleCase(SelectedMember.FirstName);
}
if (SelectedMember.MiddleName != null)
{
SelectedMember.MiddleName = textInfo.ToTitleCase(SelectedMember.MiddleName);
}
if (SelectedMember.LastName != null)
{
SelectedMember.LastName = textInfo.ToTitleCase(SelectedMember.LastName);
}
if (SelectedMember.MemberId < 1)
{
context.Members.Add(SelectedMember);
}
ValidationContext vcontext = new ValidationContext(SelectedMember);
List results = new List();
bool isValid = Validator.TryValidateObject(SelectedMember, vcontext, results);
if (context.SaveChanges() > 0)
{
MessageBox.Show("Saved");
Members = new ObservableCollection(context.Members.Take(20).OrderBy(a=>a.FirstName));
}
else
{
}
}
}
catch (DbEntityValidationException e)
{
StringBuilder sb = new StringBuilder();
foreach (var eve in e.EntityValidationErrors)
{
//sb.AppendLine(string.Format("Entity of type \"{0}\" in state \"{1}\" has the following validation errors:",
//eve.Entry.Entity.GetType().Name,
//eve.Entry.State));
sb.AppendLine(string.Format("Entity of type \"{0}\" in state \"{1}\" has the following validation errors:",
eve.Entry.Entity.GetType().BaseType.Name,
eve.Entry.State));
foreach (var ve in eve.ValidationErrors)
{
sb.AppendLine(string.Format("- Property: \"{0}\", Error: \"{1}\"",
ve.PropertyName,
ve.ErrorMessage));
}
}
MessageBox.Show(sb.ToString());
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void DeleteMembeFunction(object obj)
{
if (SelectedMember != null)
{
context.Members.Remove(SelectedMember);
if (context.SaveChanges() > 0)
{
Members.Remove(SelectedMember);
}
}
}