示例#1
0
 private void BtnDelete_Click(object sender, EventArgs e)
 {
     if (MessageBox.Show("Are you sure you want to delete this record?", "Message", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
     {
         using (HubEntities db = new HubEntities())
         {
             var entry = db.Entry(currentClient);
             if (entry.State == EntityState.Detached)
             {
                 db.Clients.Attach(currentClient);
             }
             db.Clients.Remove(currentClient);
             db.SaveChanges();
             PopulateDataGridView();
             Clear();
             MessageBox.Show("Client deleted successfully!");
         }
     }
 }
示例#2
0
        private void BtnAdd_Click(object sender, EventArgs e)
        {
            if (txtName.Text == String.Empty)
            {
                MessageBox.Show("Name is required!", "Validation", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }

            if (txtEmail.Text == String.Empty)
            {
                MessageBox.Show("Email is required!", "Validation", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }

            currentClient.Name      = txtName.Text.Trim();
            currentClient.Email     = txtEmail.Text.Trim();
            currentClient.CreatedAt = dtpCreatedAt.Value;

            using (HubEntities db = new HubEntities())
            {
                if (db.Clients.AsEnumerable().Contains(currentClient))
                {
                    Client existentClient = db.Clients.Where(x => x.Name == currentClient.Name && x.Email == currentClient.Email).FirstOrDefault();
                    MessageBox.Show("Client already registered on date " + existentClient.CreatedAt.ToString("dd/MM/yyyy") + " !", "Validation", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }

                if (currentClient.Id == 0)
                {
                    db.Clients.Add(currentClient);
                }
                else
                {
                    db.Entry(currentClient).State = EntityState.Modified;
                }
                db.SaveChanges();
            }

            PopulateDataGridView();
            Clear();
            MessageBox.Show("Client saved successfully!");
        }