Beispiel #1
0
        private void CreateCountryStatesButton_Click(object sender, System.EventArgs e)
        {
            Models.DatabaseContext databaseContext = null;

            try
            {
                databaseContext =
                    new Models.DatabaseContext();

                Models.Country country =
                    databaseContext.Countries
                    .FirstOrDefault();

                // Solution (1)
                Models.State state1 = new Models.State();

                state1.Name      = "State (1)";
                state1.CountryId = country.Id;

                databaseContext.States.Add(state1);
                // /Solution (1)

                // Solution (2)
                Models.State state2 = new Models.State();

                state2.Name    = "State (2)";
                state2.Country = country;

                databaseContext.States.Add(state2);
                // /Solution (2)

                // Solution (3)
                Models.State state3 = new Models.State();

                state3.Name = "State (3)";

                country.States.Add(state3);
                // /Solution (3)

                databaseContext.SaveChanges();

                System.Guid id1 = state1.CountryId;
                System.Guid id2 = state2.CountryId;
                System.Guid id3 = state3.CountryId;
            }
            catch (System.Exception ex)
            {
                System.Windows.Forms.MessageBox.Show(ex.Message);
            }
            finally
            {
                if (databaseContext != null)
                {
                    databaseContext.Dispose();
                    //databaseContext = null;
                }
            }
        }
Beispiel #2
0
        private void DeleteUserButton_Click(object sender, System.EventArgs e)
        {
            if (usersListBox.SelectedItems.Count == 0)
            {
                System.Windows.Forms.MessageBox.Show("You did not select any users for deleting!");

                return;
            }

            Models.DatabaseContext databaseContext = null;

            try
            {
                databaseContext = new Models.DatabaseContext();

                foreach (var selectedItem in usersListBox.SelectedItems)
                {
                    Models.User selectedUser = selectedItem as Models.User;

                    if (selectedUser != null)
                    {
                        Models.User foundedUser =
                            databaseContext.Users
                            .Where(current => current.Id == selectedUser.Id)
                            .FirstOrDefault();

                        if (foundedUser != null)
                        {
                            if (foundedUser.IsAdmin == false)
                            {
                                if (string.Compare(foundedUser.Username,
                                                   Infrastructure.Utility.AuthenticatedUser.Username, ignoreCase: true) != 0)
                                {
                                    databaseContext.Users.Remove(foundedUser);

                                    databaseContext.SaveChanges();
                                }
                            }
                        }
                    }
                }

                Search();
            }
            catch (System.Exception ex)
            {
                System.Windows.Forms.MessageBox.Show(ex.Message);
            }
            finally
            {
                if (databaseContext != null)
                {
                    databaseContext.Dispose();
                    databaseContext = null;
                }
            }
        }
Beispiel #3
0
        private void CreateButton_Click(object sender, System.EventArgs e)
        {
            if (string.IsNullOrWhiteSpace(titleTextBox.Text))
            {
                System.Windows.Forms.MessageBox.Show("Title is required!");
                return;
            }

            if (string.IsNullOrWhiteSpace(authorTextBox.Text))
            {
                System.Windows.Forms.MessageBox.Show("Author is required!");
                return;
            }

            if (string.IsNullOrWhiteSpace(publishYearTextBox.Text))
            {
                System.Windows.Forms.MessageBox.Show("Publish year is required!");
                return;
            }

            Models.DatabaseContext databaseContext = null;

            try
            {
                databaseContext =
                    new Models.DatabaseContext();

                Models.Resource resource = new Models.Resource
                {
                    Title       = titleTextBox.Text,
                    Author      = authorTextBox.Text,
                    Translator  = translatorTextBox.Text,
                    Description = descriptionTextBox.Text,
                    Type        = (Models.Enums.ResourceType)typeComboBox.SelectedValue,
                    PublishYear = System.Convert.ToInt32(publishYearTextBox.Text),
                };

                databaseContext.Resources.Add(resource);

                databaseContext.SaveChanges();

                System.Windows.Forms.MessageBox
                .Show("Resource created successfully...");
            }
            catch (System.Exception ex)
            {
                System.Windows.Forms.MessageBox.Show($"Error: { ex.Message }");
            }
            finally
            {
                if (databaseContext != null)
                {
                    databaseContext.Dispose();
                    //databaseContext = null;
                }
            }
        }
Beispiel #4
0
        private void SaveButton_Click(object sender, System.EventArgs e)
        {
            Models.DatabaseContext databaseContext = null;

            try
            {
                databaseContext = new Models.DatabaseContext();

                Models.User user = databaseContext.Users
                                   .Where(current => current.Id == SelectedUser.Id)
                                   .FirstOrDefault();

                if (user == null)
                {
                    System.Windows.Forms.MessageBox.Show("Please contact Support team",
                                                         caption: "Error",
                                                         buttons: System.Windows.Forms.MessageBoxButtons.OK,
                                                         icon: System.Windows.Forms.MessageBoxIcon.Error
                                                         );
                    System.Windows.Forms.Application.Exit();
                }

                user.IsAdmin  = isAdminCheckBox.Checked;
                user.IsActive = isActiveCheckBox.Checked;

                user.FullName    = fullNameTextBox.Text;
                user.Description = descriptionTextBox.Text;

                databaseContext.SaveChanges();

                System.Windows.Forms.MessageBox.Show("user Profile updated successfully!",
                                                     caption: "",
                                                     buttons: System.Windows.Forms.MessageBoxButtons.OK,
                                                     icon: System.Windows.Forms.MessageBoxIcon.Asterisk
                                                     );

                Infrastructure.Utility.AuthenticatedUser = databaseContext.Users
                                                           .Where(current => string.Compare(current.Username, Infrastructure.Utility.AuthenticatedUser.Username, true) == 0)
                                                           .FirstOrDefault();

                Infrastructure.Utility.MainForm.ResetForm();

                Close();
            }
            catch (System.Exception ex)
            {
                System.Windows.Forms.MessageBox.Show(ex.ToString());
            }
            finally
            {
                if (databaseContext != null)
                {
                    databaseContext.Dispose();
                    databaseContext = null;
                }
            }
        }
Beispiel #5
0
        private void btnDelete_Click(object sender, System.EventArgs e)
        {
            int b = int.Parse(dataGridView1.CurrentRow.Cells[0].Value.ToString());

            db = dt.Tasks.FirstOrDefault(i => i.Id == b);
            dt.Tasks.Remove(db);
            dt.SaveChanges();
            dataGridView1.DataSource = dt.Tasks.ToList();
        }
Beispiel #6
0
        private void SaveButton_Click(object sender, System.EventArgs e)
        {
            Models.DatabaseContext databaseContext = null;

            try
            {
                databaseContext =
                    new Models.DatabaseContext();

                var currentUser =
                    databaseContext.Users
                    .Where(current => current.Id == Infrastructure.Utility.AuthenticatedUser.Id)
                    .FirstOrDefault();

                // **************************************************
                if (currentUser == null)
                {
                    System.Windows.Forms.Application.Exit();
                }

                if (currentUser.IsActive == false)
                {
                    System.Windows.Forms.Application.Exit();
                }
                // **************************************************

                currentUser.FullName    = fullNameTextBox.Text;
                currentUser.Description = descriptionTextBox.Text;

                databaseContext.SaveChanges();

                // **************************************************
                Infrastructure.Utility.AuthenticatedUser = currentUser;

                Infrastructure.Utility.MainForm.ResetForm();
                // **************************************************

                System.Windows.Forms.MessageBox
                .Show("Your profile updated successfully...");

                // استفاده کنیم Close فرم به طور اتوماتیک بسته شود، می‌توانیم از دستور MessageBox اگر بخواهیم بعد از UpdateProfileForm در داخل
                //Close();
            }
            catch (System.Exception ex)
            {
                System.Windows.Forms.MessageBox.Show($"Error: { ex.Message }");
            }
            finally
            {
                if (databaseContext != null)
                {
                    databaseContext.Dispose();
                    databaseContext = null;
                }
            }
        }
Beispiel #7
0
        private void RegisterButton_Click(object sender, System.EventArgs e)
        {
            Models.DatabaseContext databaseContext = null;

            try
            {
                databaseContext =
                    new Models.DatabaseContext();

                Models.User user =
                    databaseContext.Users
                    .Where(current => string.Compare(current.Username, usernameTextBox.Text, true) == 0)
                    .FirstOrDefault();

                if (user != null)
                {
                    System.Windows.Forms.MessageBox.Show
                        ("This username is already exist! please choose another one ...");

                    ResetForm();
                    usernameTextBox.Focus();

                    return;
                }

                user = new Models.User
                {
                    FullName = fullNameTextBox.Text,
                    Password = passwordTextBox.Text,
                    Username = usernameTextBox.Text,

                    IsActive = true
                };

                databaseContext.Users.Add(user);

                databaseContext.SaveChanges();

                System.Windows.Forms.MessageBox.Show("Registeration Done!");

                ResetForm();
            }
            catch (System.Exception ex)
            {
                System.Windows.Forms.MessageBox.Show("Error: " + ex.Message);
            }
            finally
            {
                if (databaseContext != null)
                {
                    databaseContext.Dispose();
                    databaseContext = null;
                }
            }
        }
        private void btn_UpdateSave_Click(object sender, EventArgs e)
        {
            Models.DatabaseContext oDatabaseContext = null;

            try
            {
                oDatabaseContext =
                    new Models.DatabaseContext();

                Models.User oUser =
                    oDatabaseContext.Users
                    .Where(current => current.Id == Infrastructure.Utility.AuthenticatedUser.Id)
                    .FirstOrDefault();

                if (oUser == null)
                {
                    // می توانیم پیغام به کاربر نشان دیم که همچین نام کاربری دیگر وجود ندارد.
                    System.Windows.Forms.Application.Exit();
                }

                oUser.FullName    = fullNameTextBox.Text;
                oUser.Description = fullNameTextBox.Text;

                oDatabaseContext.SaveChanges();

                //Infrastructure.Utility.AuthenticatedUser = oUser;
                Infrastructure.Utility.AuthenticatedUser = oUser;

                //System.Windows.Forms.MessageBox
                //    .Show("Your profile was updated successfully...");

                //-------------------------------------------------------------------------
                MessageBox
                .Show(this,
                      text: "پروفایل کاربری شما با موفقیا بروز رسانی شده است!",
                      caption: "اطلاع رسانی",
                      buttons: MessageBoxButtons.OK,
                      icon: MessageBoxIcon.Error,
                      defaultButton: MessageBoxDefaultButton.Button2,
                      options: MessageBoxOptions.RightAlign | MessageBoxOptions.RtlReading);
                //-------------------------------------------------------------------------
            }
            catch (System.Exception ex)
            {
                System.Windows.Forms.MessageBox.Show("Error: " + ex.Message);
            }
            finally
            {
                if (oDatabaseContext != null)
                {
                    oDatabaseContext.Dispose();
                    oDatabaseContext = null;
                }
            }
        }
Beispiel #9
0
        private void EditContact(object sender, EventArgs e)
        {
            Models.DatabaseContext oDatabaseContext = null;
            try
            {
                oDatabaseContext = new Models.DatabaseContext();

                //Find Row of Selected Cell and Find Guid Of This Row (Guid Column is Number 4 Column. That is Hidden)
                //By These info We Can Find Record in Database And Call its Info into Our Fields
                //Then After Edit Info Refresh Grid And Empty Fields
                //***********************************************************************************
                int           row       = dataGridView.CurrentCell.RowIndex;
                string        cellIndex = dataGridView.Rows[row].Cells[4].Value.ToString();
                Guid          guid      = new Guid(cellIndex);
                Models.Person p1        = oDatabaseContext.People.Where(current => current.Id == guid).FirstOrDefault();
                p1.Name   = NameTextbox.Text;
                p1.Family = FamilyTextbox.Text;
                p1.Email  = EmailTextbox.Text;
                p1.Phone  = PhoneTextbox.Text;
                if (p1.Name == string.Empty)
                {
                    MessageBox.Show("Name Can not Be Empty");
                }
                else
                {
                    oDatabaseContext.SaveChanges();

                    //Clear Grid For Call New Info
                    dataGridView.Rows.Clear();
                    dataGridView.Refresh();
                    NameTextbox.Text   = string.Empty;
                    FamilyTextbox.Text = string.Empty;
                    PhoneTextbox.Text  = string.Empty;
                    EmailTextbox.Text  = string.Empty;
                    var contacts = oDatabaseContext.People.Where(current => current.Id != null).OrderBy(c => c.Name).ToList();
                    for (int i = 0; i < contacts.Count(); i++)
                    {
                        //Add Row In Grid
                        this.dataGridView.Rows.Add(contacts[i].Name, contacts[i].Family, contacts[i].Phone, contacts[i].Email, contacts[i].Id);
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                if (oDatabaseContext != null)
                {
                    oDatabaseContext.Dispose();
                    oDatabaseContext = null;
                }
            }
        }
        private void SaveButton_Click(object sender, System.EventArgs e)
        {
            Models.DatabaseContext databaseContext = null;

            try
            {
                databaseContext =
                    new Models.DatabaseContext();

                Models.User foundedUser =
                    databaseContext.Users
                    .Where(current => current.Id == SelectedUser.Id)
                    //.Where(current => current.Id == SelectedUserId)
                    .FirstOrDefault();

                if (foundedUser == null)
                {
                    System.Windows.Forms.MessageBox
                    .Show("There is no such a user anymore!");

                    Close();
                }

                foundedUser.IsAdmin  = isAdminCheckBox.Checked;
                foundedUser.IsActive = isActiveCheckBox.Checked;

                foundedUser.FullName    = fullNameTextBox.Text;
                foundedUser.Description = descriptionTextBox.Text;

                databaseContext.SaveChanges();

                if (Infrastructure.Utility.AuthenticatedUser.Id == SelectedUser.Id)
                {
                    Infrastructure.Utility.AuthenticatedUser = foundedUser;

                    Infrastructure.Utility.MainForm.ResetForm();
                }

                Close();
            }
            catch (System.Exception ex)
            {
                System.Windows.Forms.MessageBox.Show($"Error: { ex.Message }");

                Close();
            }
            finally
            {
                if (databaseContext != null)
                {
                    databaseContext.Dispose();
                    databaseContext = null;
                }
            }
        }
Beispiel #11
0
        private void ChangePasswordButton_Click(object sender, System.EventArgs e)
        {
            string errorMessages = string.Empty;

            if (string.IsNullOrWhiteSpace(oldPasswordTextBox.Text))
            {
                errorMessages =
                    "Old password is required!";
            }

            Models.DatabaseContext databaseContext = null;

            try
            {
                databaseContext = new Models.DatabaseContext();

                Models.User currentUser = databaseContext.Users
                                          .Where(current => current.Id == Infrastructure.Utility.AuthenticatedUser.Id)
                                          .FirstOrDefault();
                if (currentUser == null)
                {
                    System.Windows.Forms.Application.Exit();
                }

                if (string.Compare(currentUser.Password, oldPasswordTextBox.Text, ignoreCase: false) != 0)
                {
                    System.Windows.Forms.MessageBox.Show("The old password is not correct!");

                    oldPasswordTextBox.Focus();

                    return;
                }

                currentUser.Password = newPasswordTextBox.Text;

                databaseContext.SaveChanges();

                System.Windows.Forms.MessageBox.Show("Your password changed successfully.");

                Close();
            }
            catch (System.Exception ex)
            {
                System.Windows.Forms.MessageBox.Show(ex.Message);
            }
            finally
            {
                if (databaseContext != null)
                {
                    databaseContext.Dispose();
                    databaseContext = null;
                }
            }
        }
Beispiel #12
0
        private void btnAddNewCountry_Click(object sender, System.EventArgs e)
        {
            //ایجاد یک شی از دیتا بیس کانتکست
            Models.DatabaseContext oDatabaseContext = null;

            try
            {
                //New DatabaseContext
                oDatabaseContext =
                    new Models.DatabaseContext();

                // راه حل اول
                //ایجاد یک شی از کلاس کانتری
                Models.Country oCountry =
                    new Models.Country();

                //ایجاد خود کار یک نام کشور
                //oCountry.Name = "New Country";

                //ایجاد نام کشور به واسطه تکست باکس
                oCountry.Name = txtCountryName.Text;

                //افزودن به کالکشن کانتریز در دیتابیس کانتکست
                oDatabaseContext.Countries.Add(oCountry);
                // پایان راه حل اول

                // راه حل دوم
                //Models.Country oCountry =
                //	new Models.Country();

                //oCountry.Name = "New Country";

                //oDatabaseContext.Entry(oCountry).State =
                //	System.Data.EntityState.Added;
                // پایان راه حل دوم

                //افزودن اطلاعات در دیتا بیس
                oDatabaseContext.SaveChanges();
            }
            //درصورت بروز خطا آن را نمایش میدهیم
            catch (System.Exception ex)
            {
                System.Windows.Forms.MessageBox.Show(ex.Message);
            }
            //در نهایت اگر شی دیتا بیس کانتکست نال نیود آن را نال میکنیم
            finally
            {
                if (oDatabaseContext != null)
                {
                    oDatabaseContext.Dispose();
                    oDatabaseContext = null;
                }
            }
        }
Beispiel #13
0
        private void AddContact(object sender, EventArgs e)
        {
            Models.DatabaseContext oDatabaseContext = null;
            try
            {
                oDatabaseContext = new Models.DatabaseContext();

                //Get Info Of Fields And Add These Into Database And Empty Fields
                //Then Clear And Refresh Grid For Call New Info
                //***********************************************************************************
                Models.Person p1 = new Models.Person();
                p1.Id     = Guid.NewGuid();
                p1.Name   = NameTextbox.Text;
                p1.Family = FamilyTextbox.Text;
                p1.Email  = EmailTextbox.Text;
                p1.Phone  = PhoneTextbox.Text;
                if (p1.Name == string.Empty)
                {
                    MessageBox.Show("Name Can not Be Empty");
                }
                else
                {
                    oDatabaseContext.People.Add(p1);
                    oDatabaseContext.SaveChanges();

                    //Clear Grid For Call New Info
                    dataGridView.Rows.Clear();
                    dataGridView.Refresh();
                    NameTextbox.Text   = string.Empty;
                    FamilyTextbox.Text = string.Empty;
                    PhoneTextbox.Text  = string.Empty;
                    EmailTextbox.Text  = string.Empty;
                    var contacts = oDatabaseContext.People.Where(current => current.Id != null).OrderBy(c => c.Name).ToList();
                    for (int i = 0; i < contacts.Count(); i++)
                    {
                        //Add Row In Grid
                        this.dataGridView.Rows.Add(contacts[i].Name, contacts[i].Family, contacts[i].Phone, contacts[i].Email, contacts[i].Id);
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                if (oDatabaseContext != null)
                {
                    oDatabaseContext.Dispose();
                    oDatabaseContext = null;
                }
            }
        }
Beispiel #14
0
        private void saveButton_Click(object sender, EventArgs e)
        {
            Models.DatabaseContext databaseContext = null;

            try
            {
                databaseContext =
                    new Models.DatabaseContext();

                Models.User currentUser =
                    databaseContext.Users
                    .Where(current => current.Id == Infrastructure.Utility.AuthenticatedUser.Id)
                    .FirstOrDefault();

                if (currentUser == null)
                {
                    System.Windows.Forms.Application.Exit();
                }

                currentUser.FullName    = fullnametextBox.Text;
                currentUser.Description = descriptionTextBox.Text;

                databaseContext.SaveChanges();

                Infrastructure.Utility.AuthenticatedUser = currentUser;

                //MainForm mainForm = this.MdiParent as MdiForm;

                //if (mainForm != null)
                //{
                //    mainForm.InitializeMainForm();
                //}

                Infrastructure.Utility.MainForm.InitializeMainForm();

                System.Windows.Forms.MessageBox
                .Show(" your profile was update successfully...");
            }
            catch (System.Exception ex)
            {
                System.Windows.Forms.MessageBox.Show("Error:" + ex.Message);
            }

            finally
            {
                if (databaseContext != null)
                {
                    databaseContext.Dispose();
                    databaseContext = null;
                }
            }
        }
        internal static void Main()
        {
            System.Windows.Forms.Application.EnableVisualStyles();
            System.Windows.Forms.Application.SetCompatibleTextRenderingDefault(false);

            #region Database Context
            Models.DatabaseContext databaseContext = null;

            try
            {
                databaseContext =
                    new Models.DatabaseContext();

                bool hasAnyUser =
                    databaseContext.Users
                    .Any();

                if (hasAnyUser == false)
                {
                    Models.User adminUser = new Models.User
                    {
                        IsAdmin  = true,
                        IsActive = true,

                        Username = "******",
                        Password = "******",
                        FullName = "Arad Aryan",
                    };

                    databaseContext.Users.Add(adminUser);

                    databaseContext.SaveChanges();
                }
            }
            catch (System.Exception ex)
            {
                System.Windows.Forms.MessageBox.Show(ex.Message);

                return;
            }
            finally
            {
                if (databaseContext != null)
                {
                    databaseContext.Dispose();
                    databaseContext = null;
                }
            }
            #endregion Database Context

            System.Windows.Forms.Application.Run(Infrastructure.Utility.SplashScreenForm);
        }
 public void Save()
 {
     try
     {
         DatabaseContext.SaveChanges();
     }
     //catch (System.Exception ex)
     catch
     {
         //Hmx.LogHandler.Report(GetType(), null, ex);
         throw;
     }
 }
        private void AddButton_Click(object sender, System.EventArgs e)
        {
            if (DataValidation() == false)
            {
                return;
            }

            Models.DatabaseContext databaseContext = null;

            try
            {
                databaseContext = new Models.DatabaseContext();

                Models.Contact contact = new Models.Contact
                {
                    FirstName    = firstNameTextEdit.Text.ToString(),
                    LastName     = lastNameTextEdit.Text.ToString(),
                    PhoneNumber  = phoneNumberTextEdit.Text.ToString(),
                    MobileNumber = mobileTextEdit.Text.ToString(),
                    Address      = addressRichText.Text.ToString(),
                };

                databaseContext.Contacts.Add(contact);

                databaseContext.SaveChanges();

                DevExpress.XtraEditors.XtraMessageBox.Show($"!{ firstNameTextEdit.Text } { lastNameTextEdit.Text } با موفقیت اضافه شد",
                                                           caption: "",
                                                           buttons: System.Windows.Forms.MessageBoxButtons.OK,
                                                           icon: System.Windows.Forms.MessageBoxIcon.Asterisk
                                                           );

                Reset();
                firstNameTextEdit.Focus();
            }

            catch (System.Exception ex)
            {
                DevExpress.XtraEditors.XtraMessageBox.Show(ex.Message.ToString());
            }

            finally
            {
                if (databaseContext != null)
                {
                    databaseContext.Dispose();
                    databaseContext = null;
                }
            }
        }
Beispiel #18
0
        protected void Page_Load(object sender, EventArgs e)
        {
            Session.Abandon();
            string key = Response.Cookies["OpenPasteLogin"].Value;
            Response.Cookies["OpenPasteLogin"].Expires = DateTime.MinValue;
            Response.Redirect("Pastes.aspx");

            using (var ctx = new Models.DatabaseContext())
            {
                Models.LoggedByCookie lbc = (from logging in ctx.LoggedUsers where logging.session_id == key select logging).First();
                ctx.LoggedUsers.Remove(lbc);
                ctx.SaveChanges();
            }
        }
Beispiel #19
0
        private void SaveButton_Click(object sender, System.EventArgs e)
        {
            Models.DatabaseContext databaseContext = null;

            try
            {
                databaseContext =
                    new Models.DatabaseContext();

                Models.User user =
                    databaseContext.Users
                    .Where(current => string.Compare(current.Username, Username, true) == 0)
                    .FirstOrDefault();

                if (user == null)
                {
                    Dtx.Windows.Forms.MessageBox.ShowError("اطلاعات این کاربر حذف شده است!");
                    Close();
                }

                user.FullName    = fullNameTextBox.Text;
                user.Description = descriptionTextBox.Text;

                user.IsActive        = isActiveCheckBox.Checked;
                user.IsAdministrator = isAdministratorCheckBox.Checked;

                databaseContext.SaveChanges();

                Dtx.Windows.Forms.MessageBox.ShowInformation("اطلاعات شما با موفقیت ذخیره گردید.");

                // **************************************************
                MyUserListForm.Search();
                Program.AuthenticatedUser = user;
                Program.MainForm.Initialize();
                // **************************************************
            }
            catch (System.Exception ex)
            {
                Dtx.Windows.Forms.MessageBox.ShowError(ex.Message);
            }
            finally
            {
                if (databaseContext != null)
                {
                    databaseContext.Dispose();
                    databaseContext = null;
                }
            }
        }
Beispiel #20
0
        private void deleteUsersButton_Click(object sender, System.EventArgs e)
        {
            if (usersListBox.SelectedItems.Count == 0)
            {
                System.Windows.Forms.MessageBox.Show("You did not select any users for deleting!");

                return;
            }

            Models.DatabaseContext databaseContext = null;

            try
            {
                databaseContext =
                    new Models.DatabaseContext();

                foreach (var selectedItem in usersListBox.SelectedItems)
                {
                    Models.User selectedUser = selectedItem as Models.User;

                    if (selectedUser != null)
                    {
                        if (selectedUser.IsAdmin == false)
                        {
                            databaseContext.Entry(selectedUser).State = System.Data.Entity.EntityState.Deleted;

                            // Note: Does Not Work!
                            //databaseContext.Users.Remove(selectedUser);

                            databaseContext.SaveChanges();
                        }
                    }
                }

                Search();
            }
            catch (System.Exception ex)
            {
                System.Windows.Forms.MessageBox.Show(ex.Message);
            }
            finally
            {
                if (databaseContext != null)
                {
                    databaseContext.Dispose();
                    databaseContext = null;
                }
            }
        }
        private void DisplayResourceForm_Load(object sender, System.EventArgs e)
        {
            typeComboBox.DataSource =
                System.Enum.GetValues(typeof(Models.Enums.ResourceType));

            Models.DatabaseContext databaseContext = null;

            try
            {
                databaseContext =
                    new Models.DatabaseContext();

                Models.Resource resource =
                    databaseContext.Resources
                    .Where(current => current.Id == Id)
                    .FirstOrDefault();

                if (resource == null)
                {
                    System.Windows.Forms.MessageBox.Show("There is not resource any more!");
                    Close();
                }

                resource.VisitCount++;

                databaseContext.SaveChanges();

                titleTextBox.Text       = resource.Title;
                authorTextBox.Text      = resource.Author;
                translatorTextBox.Text  = resource.Translator;
                descriptionTextBox.Text = resource.Description;
                publishYearTextBox.Text = resource.PublishYear.ToString();

                //typeComboBox.SelectedText = resource.Type.ToString();
                typeComboBox.SelectedItem = (Models.Enums.ResourceType)resource.Type;
            }
            catch (System.Exception ex)
            {
                System.Windows.Forms.MessageBox.Show($"Error: { ex.Message }");
            }
            finally
            {
                if (databaseContext != null)
                {
                    databaseContext.Dispose();
                    //databaseContext = null;
                }
            }
        }
Beispiel #22
0
        private void RegistrationButton_Click(object sender, System.EventArgs e)
        {
            Models.DatabaseContext oDatabaseContext = null;


            try
            {
                oDatabaseContext =
                    new Models.DatabaseContext();

                Models.Task oTask =
                    new Models.Task();


                oTask.TaskTitle = TitleTextBox.Text;

                oTask.TaskDescription = DescriptionTextBox.Text;
                oTask.ImportanceLevel = ImportanceComboBox.Text;
                oTask.Date            = dateTimePicker1.Text;



                oDatabaseContext.Tasks.Add(oTask);

                oDatabaseContext.SaveChanges();


                System.Windows.Forms.MessageBox.Show("کار جدید مورد نظر شما اضافه گردید.");

                TitleTextBox.Text       = string.Empty;
                DescriptionTextBox.Text = string.Empty;
            }
            catch (System.Exception ex)
            {
                System.Windows.Forms.MessageBox.Show("Error: " + ex.Message);
            }
            finally
            {
                if (oDatabaseContext != null)
                {
                    oDatabaseContext.Dispose();
                    oDatabaseContext = null;
                    Main oMian = new Main();
                    oMian.Show();
                }
            }
        }
        private void EditButton_Click(object sender, System.EventArgs e)
        {
            if (DataValidation() == false)
            {
                return;
            }

            Models.DatabaseContext databaseContext = null;

            try
            {
                databaseContext = new Models.DatabaseContext();

                Models.Contact contact = databaseContext.Contacts
                                         .Where(current => current.Id == SelectedContact.Id)
                                         .FirstOrDefault();

                contact.Address      = addressRichText.Text.ToString();
                contact.LastName     = lastNameTextEdit.Text.ToString();
                contact.MobileNumber = mobileTextEdit.Text.ToString();
                contact.FirstName    = firstNameTextEdit.Text.ToString();
                contact.PhoneNumber  = phoneNumberTextEdit.Text.ToString();

                databaseContext.SaveChanges();

                DevExpress.XtraEditors.XtraMessageBox.Show($"!{ firstNameTextEdit.Text } { lastNameTextEdit.Text } با موفقیت ویرایش شد",
                                                           caption: "",
                                                           buttons: System.Windows.Forms.MessageBoxButtons.OK,
                                                           icon: System.Windows.Forms.MessageBoxIcon.Asterisk
                                                           );

                this.Close();
            }
            catch (System.Exception ex)
            {
                DevExpress.XtraEditors.XtraMessageBox.Show(ex.Message.ToString());
            }
            finally
            {
                if (databaseContext != null)
                {
                    databaseContext.Dispose();
                    databaseContext = null;
                }
            }
        }
Beispiel #24
0
        private void registerBook_Click(object sender, EventArgs e)
        {
            Models.DatabaseContext databaseContext = null;
            try
            {
                databaseContext = new Models.DatabaseContext();
                Models.Book book =
                    databaseContext.Books
                    .Where(current => string.Compare(current.ISBN, isbnTextBox.Text, true) == 0)
                    .FirstOrDefault();

                if (book != null)
                {
                    System.Windows.Forms.MessageBox.Show
                        ("This book is already exist! Please choose another one...");
                    isbnTextBox.Focus();

                    return;
                }
                book                 = new Models.Book();
                book.ISBN            = isbnTextBox.Text;
                book.AuthorName      = authorNameTextBox.Text;
                book.Title           = titleTextBox.Text;
                book.PublicationDate = DateTime.Now;

                Image temp = new Bitmap(strFilePath);
                System.IO.MemoryStream strm = new System.IO.MemoryStream();
                temp.Save(strm, System.Drawing.Imaging.ImageFormat.Jpeg);
                imageByteArray = strm.ToArray();

                book.ImageByteArray = imageByteArray;


                databaseContext.Books.Add(book);
                databaseContext.SaveChanges();


                System.Windows.Forms.MessageBox.Show("Registration Done!");
                Close();
            }
            catch (Exception)

            {
                throw;
            }
        }
Beispiel #25
0
        private void SaveButton_Click(object sender, System.EventArgs e)
        {
            Models.DatabaseContext databaseContext = null;

            try
            {
                databaseContext =
                    new Models.DatabaseContext();

                Models.User authenticatedUser = Program.AuthenticatedUser;

                Models.User user =
                    databaseContext.Users
                    .Where(current => current.Id == authenticatedUser.Id)
                    .FirstOrDefault();

                if (user == null)
                {
                    System.Windows.Forms.Application.Exit();
                }

                user.FullName    = fullNameTextBox.Text;
                user.Description = descriptionTextBox.Text;

                databaseContext.SaveChanges();

                Dtx.Windows.Forms.MessageBox.ShowInformation("اطلاعات شما با موفقیت ذخیره گردید.");

                // **************************************************
                Program.AuthenticatedUser = user;
                Program.MainForm.Initialize();
                // **************************************************
            }
            catch (System.Exception ex)
            {
                Dtx.Windows.Forms.MessageBox.ShowError(ex.Message);
            }
            finally
            {
                if (databaseContext != null)
                {
                    databaseContext.Dispose();
                    databaseContext = null;
                }
            }
        }
Beispiel #26
0
        private void saveButton_Click(object sender, EventArgs e)
        {
            Models.DatabaseContext databaseContext = null;

            try
            {
                databaseContext =
                    new Models.DatabaseContext();

                Models.Book foundedBook =
                    databaseContext.Books
                    .Where(current => current.Id == SelectedBook.Id)

                    .FirstOrDefault();

                if (foundedBook == null)
                {
                    System.Windows.Forms.MessageBox.Show("There is no such a user anymore!");

                    Close();
                }

                foundedBook.IsActive = activeCheckBox.Checked;

                foundedBook.BookName = bookNameTextbox.Text;

                databaseContext.SaveChanges();

                Close();
            }
            catch (System.Exception ex)
            {
                System.Windows.Forms.MessageBox.Show(ex.Message);

                Close();
            }
            finally
            {
                if (databaseContext != null)
                {
                    databaseContext.Dispose();
                    databaseContext = null;
                }
            }
        }
Beispiel #27
0
        private void CreateANewCountryButton_Click(object sender, System.EventArgs e)
        {
            Models.DatabaseContext databaseContext = null;

            try
            {
                databaseContext =
                    new Models.DatabaseContext();

                Models.Country country =
                    databaseContext.Countries
                    .Where(current => current.Name.ToLower() == "Iran".ToLower())
                    .FirstOrDefault();

                if (country == null)
                {
                    country = new Models.Country
                    {
                        Name = "Iran",
                    };

                    databaseContext.Countries.Add(country);

                    databaseContext.SaveChanges();

                    System.Windows.Forms.MessageBox.Show("Iran country created successfully!");
                }
                else
                {
                    System.Windows.Forms.MessageBox.Show("Iran country already exists!");
                }
            }
            catch (System.Exception ex)
            {
                System.Windows.Forms.MessageBox.Show(ex.Message);
            }
            finally
            {
                if (databaseContext != null)
                {
                    databaseContext.Dispose();
                    //databaseContext = null;
                }
            }
        }
        private void SaveAndClosebutton_Click(object sender, System.EventArgs e)
        {
            Models.DatabaseContext databaseContext = null;

            try
            {
                databaseContext =
                    new Models.DatabaseContext();
                Models.User foundedUser =
                    databaseContext.Users
                    .Where(current => current.Id == SelectedUser.Id)
                    .FirstOrDefault();

                if (foundedUser == null)
                {
                    System.Windows.Forms.MessageBox.Show("There is no such a user anymore!");

                    Close();
                }

                foundedUser.IsAdmin  = isAdminCheckBox.Checked;
                foundedUser.IsActive = isActiveCheckBox.Checked;

                foundedUser.FullName    = fullNameTextBox.Text;
                foundedUser.Description = descriptionTextBox.Text;

                databaseContext.SaveChanges();

                Close();
            }
            catch (System.Exception ex)
            {
                System.Windows.Forms.MessageBox.Show(ex.Message);

                Close();
            }
            finally
            {
                if (databaseContext != null)
                {
                    databaseContext.Dispose();
                    databaseContext = null;
                }
            }
        }
Beispiel #29
0
        private void EditButton_Click(object sender, EventArgs e)
        {
            var foundedEntity =
                databasecontext.Tasks
                .Where(current => current.Id == postedTaskViewModel.Id)
                .FirstOrDefault()
            ;

            var taskStatus =
                databasecontext.TaskStatuses
                .ToList()
            ;

            foundedEntity.Name        = nameTextBox.Text;
            foundedEntity.Description = descriptionTextBox.Text;

            if (ToDoRadioButton.Checked == true)
            {
                foundedEntity.TaskStatus = taskStatus.Where(current => current.Titile.ToLower() == "ToDo".ToLower()).FirstOrDefault();
            }

            if (DoneRadioButton.Checked == true)
            {
                foundedEntity.TaskStatus = taskStatus.Where(current => current.Titile.ToLower() == "Done".ToLower()).FirstOrDefault();
            }

            foundedEntity.EndtDate =
                DateTime.TryParse(endDateMaskedTextBox.Text.ToString(), out DateTime dateTime) == true ? dateTime : foundedEntity.EndtDate;

            if (deactiveCheckBox.Checked == true)
            {
                foundedEntity.IsActive = false;
            }
            else
            {
                foundedEntity.IsActive = true;
            }


            databasecontext.SaveChanges();

            MessageBox.Show(text: "Edit Completed", caption: "", buttons: MessageBoxButtons.OK, icon: MessageBoxIcon.Information);

            this.Close();
        }
Beispiel #30
0
        private void saveButton_Click(object sender, System.EventArgs e)
        {
            Models.DatabaseContext databaseContext = null;

            try
            {
                databaseContext =
                    new Models.DatabaseContext();

                Models.User currentUser =
                    databaseContext.Users
                    .Where(current => current.Id == Infrastructure.Utility.AuthenticatedUser.Id)
                    .FirstOrDefault();

                if (currentUser == null)
                {
                    System.Windows.Forms.Application.Exit();
                }

                currentUser.FullName    = fullNameTextBox.Text;
                currentUser.Description = descriptionTextBox.Text;

                databaseContext.SaveChanges();

                Infrastructure.Utility.AuthenticatedUser = currentUser;

                ((MainForm)this.MdiParent).UpdateWelcomeToolStripStatusLabel();

                System.Windows.Forms.MessageBox
                .Show("Your profile was updated successfully...");
            }
            catch (System.Exception ex)
            {
                System.Windows.Forms.MessageBox.Show("Error: " + ex.Message);
            }
            finally
            {
                if (databaseContext != null)
                {
                    databaseContext.Dispose();
                    databaseContext = null;
                }
            }
        }
        private void saveButton_Click(object sender, System.EventArgs e)
        {
            Models.DatabaseContext oDatabaseContext = null;

            try
            {
                oDatabaseContext =
                    new Models.DatabaseContext();

                Models.User oUser =
                    oDatabaseContext.Users
                    .Where(current => current.Id == Infrastructure.Utility.AuthenticatedUser.Id)
                    .FirstOrDefault();

                if (oUser == null)
                {
                    System.Windows.Forms.Application.Exit();
                }

                oUser.FullName    = fullNameTextBox.Text;
                oUser.Description = descriptionTextBox.Text;

                oDatabaseContext.SaveChanges();

                Infrastructure.Utility.AuthenticatedUser = oUser;

                System.Windows.Forms.MessageBox
                .Show("Your profile was updated successfully...");
            }
            catch (System.Exception ex)
            {
                System.Windows.Forms.MessageBox.Show("Error: " + ex.Message);
            }
            finally
            {
                if (oDatabaseContext != null)
                {
                    oDatabaseContext.Dispose();
                    oDatabaseContext = null;
                }
            }
        }
Beispiel #32
0
        public ActionResult NyKunde(FormCollection innListe)
        {
            if (ModelState.IsValid)
            {
                /*
                return "Fornavn: " + innListe["Fornavn"] + "\nEtternavn: " + innListe["Etternavn"] + "\nAdresse: " + innListe["Adresse"] +
                     "\nTelefonnr: " + innListe["Telefonnummer"] + "\nEpost: " + innListe["Epost"] + "\nPassord: " + innListe["Passord"] +
                     "\nPoststed: " + innListe["Poststed"] + "\nPostnr: " + innListe["Postnummer"];
                */
               // Console.WriteLine("In the mucK");
                try
                {
                    using (var db = new Models.DatabaseContext())
                    {
                        Session["Bruker"] = null;
                        Session["LoggetInn"] = false;
                        var nyKunde = new Models.dbKunde();
                        nyKunde.Fornavn = innListe["Fornavn"];
                        nyKunde.Etternavn = innListe["Etternavn"];
                        nyKunde.Adresse = innListe["Adresse"];
                        nyKunde.Telefonnummer = int.Parse(innListe["Telefonnummer"]);
                        nyKunde.Epost = innListe["Epost"];
                        if (innListe["Passord"].Equals(innListe["PassordKopi"]))
                        {
                            nyKunde.Passord = (Logikk.hashPword(innListe["Passord"]));
                        }

                          //Kan ikke bruke dette array i LINQ nedenfor
                          string innPostNr = innListe["Postnummer"];
                          var funnetPoststed = db.Poststeder
                              .FirstOrDefault(p => p.Postnummer == innPostNr);
                          if (funnetPoststed == null) //fant ikke poststed, må legge inn et nytt
                          {
                              var nyttPoststed = new Models.dbPoststed();
                              nyttPoststed.Postnummer = innListe["Postnummer"];
                              nyttPoststed.Poststed = innListe["Poststed"];
                              db.Poststeder.Add(nyttPoststed);
                              //det nye poststedet legges i den nye brukeren
                              nyKunde.Poststed = nyttPoststed;
                          }
                          else
                          {
                              //fant poststedet, legger det inn på bruker
                              nyKunde.Poststed = funnetPoststed;
                          }
                        var exist = db.Kunder
                            .FirstOrDefault(k => k.Epost == nyKunde.Epost);
                        if (exist == null)
                        {
                            db.Kunder.Add(nyKunde);

                            Session["Bruker"] = nyKunde;
                            db.SaveChanges();
                        }
                        else if(funnetPoststed== null)
                        {
                            db.SaveChanges();
                        }

                        return RedirectToAction("RegistreringsResultat");

                    }
                }
                catch (Exception feil)
                {
                return View();
                }
            }
            else return View();
        }