// Searches for user (by name or national code) // Returns user' info private void Search_Click(object sender, RoutedEventArgs e) { resultTextBox.Content = ""; ReadWriteJson file = new ReadWriteJson(); try { string byType = typeDetectorBox.SelectedItem.ToString(); string searchMethod = searchArgTextBox.Text; User foundUser = new User(); if (byType.Equals("By Name")) { foundUser = file.searchUser(searchMethod); } else if (byType.Equals("By National Code")) { int nationalCode = int.Parse(searchMethod); foundUser = file.searchUser(nationalCode); } if (foundUser.Role.Equals("Manager") && IsAdmin == false) { MessageBox.Show("Access limited"); } else { UserControls.UserDetails userDetailUserControl = new UserControls.UserDetails(foundUser, admin.Name); userDetailStackPanel.Children.Clear(); userDetailStackPanel.Children.Add(userDetailUserControl); //if (searchMethod.Equals(admin.Name)) // userDetailUserControl.nameTextBox.IsEnabled = true; } } catch (FormatException excp) { ErrorLogger.LogError(excp); resultTextBox.Content = "Wrong format,please check national code!"; searchArgTextBox.BorderBrush = Brushes.Red; //throw new userControl/ ArgumentNullException(); } catch (FileNotFoundException) { resultTextBox.Content = "UserName not Found!!"; searchArgTextBox.BorderBrush = Brushes.Red; //throw new userControl/ArgumentNullException(); } catch (ArgumentNullException excp) { ErrorLogger.LogError(excp); resultTextBox.Content = "Data not Found!"; //throw new userControl/ArgumentNullException(); } catch (Exception ex) { ErrorLogger.LogError(ex); MessageBox.Show(ex.Message); } }
//sumbit a new user private void Submit_Click(object sender, RoutedEventArgs e) { Brush fill = (SolidColorBrush) new BrushConverter().ConvertFromString("#FF622F96"); nameTextBox.BorderBrush = fill; nationalCodeTextBox.BorderBrush = fill; typeDetectorBox.BorderBrush = fill; ReadWriteJson file = new ReadWriteJson(); try { string name = nameTextBox.Text; int nationalCode = Int32.Parse(nationalCodeTextBox.Text); string imageAddress = imageAdd; string role = typeDetectorBox.Text; string description = descriptionTextBox.Text; bool isAdmin; if (adminChecker.IsChecked == true) { isAdmin = true; } else { isAdmin = false; } if (typeDetectorBox.SelectedIndex == -1) { throw new NullReferenceException(); } else if (file.CheckUser(name) != false) { resultTextBox.Content = "User already existed!Choose another Name"; nameTextBox.BorderBrush = Brushes.Red; throw new Exception("Duplicate User"); } string imageExtension = Path.GetExtension(imageAdd); assistant.CopyFile(imageAdd, assistant.AppPath + @"Images\" + name + imageExtension); imageAddress = assistant.AppPath + @"Images\" + name + imageExtension; User newUser = new User(name, nationalCode, imageAddress, role, nationalCode.ToString(), isAdmin, description); file.WriteUser(newUser); resultTextBox.Content = "Registered!"; nameTextBox.Text = ""; nationalCodeTextBox.Text = ""; string PicName = assistant.AppPath + @"NoImage.PNG"; BitmapImage bitmap = new BitmapImage(); bitmap.BeginInit(); bitmap.UriSource = new Uri(PicName); bitmap.EndInit(); mainPic.Source = bitmap; typeDetectorBox.SelectedIndex = -1; adminChecker.IsChecked = false; descriptionTextBox.Text = ""; } catch (FormatException) { resultTextBox.Content = "Wrong format,please check national code!"; nationalCodeTextBox.BorderBrush = Brushes.Red; //throw new userControl/ArgumentFormatException(); } catch (NullReferenceException) { resultTextBox.Content = "Fill the blank boxes!"; nameTextBox.BorderBrush = Brushes.Red; nationalCodeTextBox.BorderBrush = Brushes.Red; typeDetectorBox.BorderBrush = Brushes.Red; //throw new userControl/ArgumentNullException(); } catch (Exception excp) { MessageBox.Show(excp.Message); ErrorLogger.LogError(excp); //pop error in center //UserControls.ErrorBox Error = new UserControls.ErrorBox(); } }