Exemplo n.º 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!");
         }
     }
 }
Exemplo n.º 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!");
        }
Exemplo n.º 3
0
        private void BtnUpload_Click(object sender, EventArgs e)
        {
            if (ofdUpload.ShowDialog().Equals(DialogResult.OK))
            {
                try
                {
                    using (var stream = File.Open(ofdUpload.FileName, FileMode.Open, FileAccess.Read))
                    {
                        IExcelDataReader reader = ExcelDataReader.ExcelReaderFactory.CreateReader(stream);

                        var conf = new ExcelDataSetConfiguration
                        {
                            ConfigureDataTable = _ => new ExcelDataTableConfiguration
                            {
                                UseHeaderRow = true
                            }
                        };

                        var dataTable = reader.AsDataSet(conf).Tables[0];

                        using (HubEntities db = new HubEntities())
                        {
                            List <Client> clientList = (from DataRow dr in dataTable.Rows
                                                        select new Client()
                            {
                                Name = dr["Name"].ToString(),
                                Email = dr["Email"].ToString(),
                                CreatedAt = DateTime.Parse(dr["Date"].ToString())
                            })
                                                       .Where(x => !db.Clients.AsEnumerable().Contains(x) &&
                                                              x.Name.Trim() != "" &&
                                                              x.Email.Trim() != "")
                                                       .Distinct()
                                                       .ToList();

                            if (clientList.Count == 0)
                            {
                                MessageBox.Show("File has no new valid clients!", "Validation", MessageBoxButtons.OK, MessageBoxIcon.Information);
                                return;
                            }

                            db.Clients.AddRange(clientList);
                            db.SaveChanges();
                        }
                    }
                }
                catch (Exception ex)
                {
                    if (ex is HeaderException || ex is ArgumentException)
                    {
                        MessageBox.Show("File must be an Excel file with Headers: Name, Email and Date", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        return;
                    }

                    if (ex is FormatException)
                    {
                        MessageBox.Show("'Date' must be on format dd/MM/yyyy", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        return;
                    }

                    MessageBox.Show("Import file failed! Error Message: " + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }

                PopulateDataGridView();
                Clear();
                MessageBox.Show("Clients uploaded successfully!");
            }
        }