示例#1
0
        private void add_btn_Click(object sender, EventArgs e)
        {
            List <Genre> genres = db.Genres.ToList();

            if (mode.Equals("Add"))
            {
                if (name_txt.Text != "")
                {
                    db.Genres.Add(new Genre {
                        Name = name_txt.Text
                    });
                    db.SaveChanges(); // Add this data on a table
                    name_txt.Text = "";
                    (this.Owner as Main_Form).listBox1.Items.Clear();
                    (this.Owner as Main_Form).listBox1.Items.AddRange(genres.ToArray());
                    Close();
                }
                else
                {
                    MessageBox.Show("Field Name is Empty... Try to fill out it!", "Name - EMPTY", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
            }
            else if (mode.Equals("Edit"))
            {
            }
        }
示例#2
0
 private void addBtn_Click(object sender, RoutedEventArgs e)
 {
     try
     {
         Work_Area area = new Work_Area
         {
             Site_Name = nameTxt.Text,
             Email     = emailTxt.Text,
             Login     = loginTxt.Text,
             Password  = passTxt.Text,
             URL       = urlTxt.Text,
             Phone     = phoneTxt.Text,
             Comments  = commentTxt.Text
         };
         db.Works_Datas.Add(area);
         db.SaveChanges();
         MessageBox.Show($"Datas about sitename {nameTxt.Text} was added successfully!",
                         "Congratulate!", MessageBoxButton.OK, MessageBoxImage.Asterisk);
         // After correct adding clear all text fields:
         nameTxt.Text     = emailTxt.Text = loginTxt.Text =
             passTxt.Text = urlTxt.Text = phoneTxt.Text = commentTxt.Text = "";
     }
     catch (Exception ex)
     {
         MessageBox.Show(ex.Message, "System Exception..Failed..",
                         MessageBoxButton.OK, MessageBoxImage.Error);
     }
 }
 private void okBtn_Click(object sender, RoutedEventArgs e)
 {
     if (fnameTxt.Text == "" && lnameTxt.Text == "" && loginTxt.Text == "" &&
         passTxt.Text == "" && roleTxt.Text == "" && ageTxt.Text == "")
     {
         MessageBox.Show("All fields is EMPTY... Try to fill it!", "EMPTY",
                         MessageBoxButton.OK, MessageBoxImage.Warning);
     }
     else if (fnameTxt.Text == "" || lnameTxt.Text == "" || loginTxt.Text == "" ||
              passTxt.Text == "" || roleTxt.Text == "" || ageTxt.Text == "")
     {
         MessageBox.Show("Some of field is EMPTY... Try to fill it!", "Some one was EMPTY",
                         MessageBoxButton.OK, MessageBoxImage.Warning);
     }
     else
     {
         try
         {
             User user = new User
             {
                 First_Name    = fnameTxt.Text,
                 Last_Name     = lnameTxt.Text,
                 Login         = loginTxt.Text,
                 Password      = passTxt.Text,
                 Role          = roleTxt.Text,
                 Age           = ageTxt.Text,
                 Date_Register = DateTime.Now
             };
             db.Users.Add(user);
             db.SaveChanges();
             MessageBox.Show($"User - {fnameTxt.Text} was added successfully!", "Congratulate!",
                             MessageBoxButton.OK, MessageBoxImage.Asterisk);
             // After correct adding clear all text fields:
             fnameTxt.Text    = lnameTxt.Text = loginTxt.Text =
                 passTxt.Text = roleTxt.Text = ageTxt.Text = "";
         }
         catch (Exception ex)
         {
             MessageBox.Show(ex.Message, "System Exception..Failed..",
                             MessageBoxButton.OK, MessageBoxImage.Error);
         }
     }
 }
示例#4
0
        private void exec_btn_Click(object sender, RoutedEventArgs e)
        {
            if (mode.Equals("Add"))
            {
                // Если мы оставили все поля пустыми:
                if (name_txt.Text == "" && birth_txt.Text == "")
                {
                    MessageBox.Show("Вы оставили все поля пустыми", "Все поля пустые...", MessageBoxButton.OK, MessageBoxImage.Warning);
                }
                // Если мы оставили все поля пустыми:
                else if (name_txt.Text == "" || birth_txt.Text == "")
                {
                    MessageBox.Show("Какое то пеле оставили пустым", "Пустое к-ето поле...", MessageBoxButton.OK, MessageBoxImage.Warning);
                }
                else
                {
                    try
                    {
                        Author author = new Author
                        {
                            Id            = autoIncrement,
                            Name          = name_txt.Text,
                            Date_of_Birth = int.Parse(birth_txt.Text),
                        };

                        // Add to Books table of databese:
                        string msg = InsertToDatabase.InsertAuthor(author);
                        MessageBox.Show(msg, "Added");

                        (this.Owner as MainWindow).listBox1.Items.Add(author);
                        (this.Owner as MainWindow).listBox1.Items.Clear();

                        authors = ReadFromDatabase.ReadAllAuthors().ToArray();

                        // Ubdate listbox to curent datas from a table Books:
                        foreach (Author b in authors)
                        {
                            (this.Owner as MainWindow).listBox1.Items.Add(b);
                        }
                        ClearFields();
                        this.Close();
                    }
                    catch { MessageBox.Show("Вы ввели символи, или строку вместо целого числа", "Не правильный формат", MessageBoxButton.OK, MessageBoxImage.Warning); }
                }
            }
            else if (mode.Equals("Update"))
            {
                // Если мы оставили все поля пустыми:
                if (name_txt.Text == "" && birth_txt.Text == "")
                {
                    MessageBox.Show("Вы оставили все поля пустыми", "Все поля пустые...", MessageBoxButton.OK, MessageBoxImage.Warning);
                }
                // Если мы оставили все поля пустыми:
                else if (name_txt.Text == "" || birth_txt.Text == "")
                {
                    MessageBox.Show("Какое то пеле оставили пустым", "Пустое к-ето поле...", MessageBoxButton.OK, MessageBoxImage.Warning);
                }
                else
                {
                    try
                    {
                        My_Context db    = new My_Context();
                        int        index = (this.Owner as MainWindow).listBox1.Items.IndexOf(this.author);

                        // Get Id entity from the Table:
                        int id = author.Id;

                        // Get entity from DbSet to Id and edit it:
                        List <Author> all     = ReadFromDatabase.ReadAllAuthors();
                        Author        updated = all.Where(u => u.Id == id).FirstOrDefault();
                        //updated.Id = index + 1;
                        updated.Name          = name_txt.Text;
                        updated.Date_of_Birth = int.Parse(birth_txt.Text);

                        // Update to Database:
                        db.Entry(updated).State = System.Data.Entity.EntityState.Modified;
                        db.SaveChanges();
                        //MessageBox.Show(msg, "Updated");

                        (this.Owner as MainWindow).listBox1.Items.RemoveAt(index);
                        (this.Owner as MainWindow).listBox1.Items.Insert(index, updated);
                        (this.Owner as MainWindow).listBox1.Items.Clear();

                        authors = ReadFromDatabase.ReadAllAuthors().ToArray();
                        foreach (Author b in authors)
                        {
                            (this.Owner as MainWindow).listBox1.Items.Add(b);
                        }
                        this.Close();
                    }
                    catch (System.Exception ex)
                    {
                        MessageBox.Show(ex.Message, "System Exception..Failed..", MessageBoxButton.OK, MessageBoxImage.Error);
                    }
                }
            }
            else
            {
                MessageBox.Show("Incorrect sting parameter 'mode'..", "Something went wrong...", MessageBoxButton.OK, MessageBoxImage.Warning);
            }
        }