Exemplo n.º 1
0
        private void button1_Click(object sender, EventArgs e)
        {
            User   us  = new User();
            Adress ad  = new Adress();
            string s   = "";
            Regex  re  = new Regex(@"^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$");
            Regex  re1 = new Regex("^(?=.*?[A-Za-z])(?=.*?[0-9])(?=.*?[#?!@$%^_&*-]).{8,}$");

            if (txtVnaam.Text != "" && txtVnaam.Text.ToCharArray().All(c => char.IsLetter(c)))
            {
                us.Voornaam = txtVnaam.Text;
            }
            else
            {
                s += "Voornaam ? ";
            }
            if (txtAnaam.Text != "" && txtAnaam.Text.ToCharArray().All(c => char.IsLetter(c)))
            {
                us.Achternaam = txtAnaam.Text;
            }
            else
            {
                s += "Achternaam ? ";
            }
            if (dtpGD.Value != null && dtpGD.Value < DateTime.Now)
            {
                us.Geboortdatum = dtpGD.Value;
            }
            else
            {
                s += "Geboortdatum ? ";
            }
            if (txtTel.Text != "" && txtTel.Text.ToCharArray().All(c => char.IsDigit(c)))
            {
                us.Telefoon = txtTel.Text;
            }
            else
            {
                s += "Telefoon ? ";
            }
            if (re.IsMatch(txtEmail.Text))
            {
                us.Email = txtEmail.Text;
            }
            else
            {
                s += "Email ? ";
            }
            if (newAd)
            {
                if (txtStraat.Text != "" && txtStraat.Text.ToCharArray().All(c => char.IsLetter(c)))
                {
                    ad.Straat = txtStraat.Text;
                }
                else
                {
                    s += "Adress: Straat ? ";
                }
                if (txtHuisNr.Text != "" && txtHuisNr.Text.ToCharArray().All(c => char.IsLetterOrDigit(c)))
                {
                    ad.Huisnummer = Convert.ToInt32(txtHuisNr.Text);
                }
                else
                {
                    s += "Adress: Huisnummer ? ";
                }
                if (txttGem.Text != "" && txttGem.Text.ToCharArray().All(c => char.IsLetter(c)))
                {
                    ad.Gemeente = txttGem.Text;
                }
                else
                {
                    s += "Adress: Gemeente ? ";
                }
                if (txtPC.Text != "" && txtPC.Text.ToCharArray().All(c => char.IsDigit(c)))
                {
                    ad.Postcode = txtPC.Text;
                }
                else
                {
                    s += "Adress: Postcode ? ";
                }
                if (txtLand.Text != "" && txtLand.Text.ToCharArray().All(c => char.IsLetterOrDigit(c)))
                {
                    ad.Land = txtLand.Text;
                }
                else
                {
                    s += "Adress: Land ? ";
                }
            }
            User u = null;

            using (var ctx = new ProjectContext())
            {
                u = ctx.Users.FirstOrDefault(U => U.Username == txtUsername.Text);
            }
            if (txtUsername.Text != "" && txtUsername.Text.ToCharArray().All(c => char.IsLetter(c)) && u == null)
            {
                us.Username = txtUsername.Text;
            }
            else
            {
                s += "Usernaam ? ";
            }
            if (re1.IsMatch(txtWachtwoord1.Text) && txtWachtwoord1.Text == txtWachtwoord2.Text)
            {
                us.Wachtwoord = hc.PassHash(txtWachtwoord1.Text);
            }
            else
            {
                s += "Wachtwoord ? ";
            }
            us.Role = (RoleUser)cmbRoles.SelectedItem;
            if (s == "")
            {
                using (var ctx = new ProjectContext())
                {
                    if (newAd && (ctx.Adressen.FirstOrDefault(a => a.Straat + " " + a.Huisnummer + " " + a.Gemeente + " " + a.Postcode + " " + a.Land == ad.Straat + " " + ad.Huisnummer + " " + ad.Gemeente + " " + ad.Postcode + " " + ad.Land) == null))
                    {
                        us.adress = ad;
                    }
                    else if (newAd && ctx.Adressen.FirstOrDefault(a => a.Straat + " " + a.Huisnummer + " " + a.Gemeente + " " + a.Postcode + " " + a.Land == ad.Straat + " " + ad.Huisnummer + " " + ad.Gemeente + " " + ad.Postcode + " " + ad.Land) != null)
                    {
                        us.adress = ctx.Adressen.FirstOrDefault(a => a.Straat + " " + a.Huisnummer + " " + a.Gemeente + " " + a.Postcode + " " + a.Land == ad.Straat + " " + ad.Huisnummer + " " + ad.Gemeente + " " + ad.Postcode + " " + ad.Land);
                    }
                    else if (!newAd)
                    {
                        us.adress = ctx.Adressen.FirstOrDefault(a => a.AdressId == ((Adress)cmbAdress.SelectedItem).AdressId);
                    }
                    ctx.Users.Add(us);
                    ctx.SaveChanges();
                    Databeheer.Users = ctx.Users.Include("Adress").ToList();
                }
                Databeheer.loaddgvusers();
                txtVnaam.Text           = "";
                txtAnaam.Text           = "";
                dtpGD.Value             = DateTime.Now;
                txtTel.Text             = "";
                txtEmail.Text           = "";
                txtStraat.Text          = "";
                txtHuisNr.Text          = "";
                txttGem.Text            = "";
                txtPC.Text              = "";
                txtLand.Text            = "";
                txtUsername.Text        = "";
                txtWachtwoord1.Text     = "";
                txtWachtwoord2.Text     = "";
                cmbRoles.SelectedIndex  = 0;
                cmbAdress.SelectedIndex = 0;
                pnlAdress.Visible       = false;
                cmbAdress.Enabled       = true;
                newAd = false;
            }
            else
            {
                MessageBox.Show(s);
                s = "";
                this.DialogResult = DialogResult.OK;
            }
        }
Exemplo n.º 2
0
        private void btnupload_Click(object sender, EventArgs e)
        {
            Product        pr     = new Product();
            OpenFileDialog dialog = new OpenFileDialog();

            //dialog.Filter = "Text files | *.txt";
            dialog.Multiselect = false;
            if (dialog.ShowDialog() == DialogResult.OK)
            {
                string FileLocation = dialog.FileName;
                Microsoft.Office.Interop.Word.Application App  = new Microsoft.Office.Interop.Word.Application();
                Microsoft.Office.Interop.Word.Document    Doc  = App.Documents.Open(FileLocation);
                Microsoft.Office.Interop.Word.Table       tab1 = Doc.Content.Tables[1];
                Microsoft.Office.Interop.Word.Table       tab2 = Doc.Content.Tables[2];
                Microsoft.Office.Interop.Word.Table       tab3 = Doc.Content.Tables[3];
                Levrancier lev = new Levrancier();
                Adress     ad  = new Adress();
                lev.Naam         = tab1.Cell(2, 1).Range.Text.Substring(0, tab1.Cell(2, 1).Range.Text.Length - 2);
                lev.Omschrijving = tab1.Cell(2, 2).Range.Text.Substring(0, tab1.Cell(2, 2).Range.Text.Length - 2);
                lev.Telefoon1    = tab1.Cell(2, 3).Range.Text.Substring(0, tab1.Cell(2, 3).Range.Text.Length - 2);
                lev.Telefoon2    = tab1.Cell(2, 4).Range.Text.Substring(0, tab1.Cell(2, 4).Range.Text.Length - 2);
                lev.Email        = tab1.Cell(2, 5).Range.Text.Substring(0, tab1.Cell(2, 5).Range.Text.Length - 2);
                ad.Straat        = tab2.Cell(2, 1).Range.Text.Substring(0, tab2.Cell(2, 1).Range.Text.Length - 2);
                ad.Huisnummer    = Convert.ToInt32(tab2.Cell(2, 2).Range.Text.Substring(0, tab2.Cell(2, 2).Range.Text.Length - 2));
                ad.Gemeente      = tab2.Cell(2, 3).Range.Text.Substring(0, tab2.Cell(2, 3).Range.Text.Length - 2);
                ad.Postcode      = tab2.Cell(2, 4).Range.Text.Substring(0, tab2.Cell(2, 4).Range.Text.Length - 2);
                ad.Land          = tab2.Cell(2, 5).Range.Text.Substring(0, tab2.Cell(2, 5).Range.Text.Length - 2);

                for (int i = 2; i <= tab3.Rows.Count; i++)
                {
                    pr.ProductNaam   = tab3.Cell(i, 1).Range.Text.Substring(0, tab3.Cell(i, 1).Range.Text.Length - 2);
                    pr.UnitPrice     = Convert.ToDouble(tab3.Cell(i, 3).Range.Text.Substring(0, tab3.Cell(i, 3).Range.Text.Length - 2));
                    pr.UnitsOnStock  = Convert.ToInt32(tab3.Cell(i, 2).Range.Text.Substring(0, tab3.Cell(i, 2).Range.Text.Length - 2));
                    pr.ProductNummer = tab3.Cell(i, 4).Range.Text.Substring(0, tab3.Cell(i, 4).Range.Text.Length - 2);
                    pr.BarCode       = tab3.Cell(i, 5).Range.Text.Substring(0, tab3.Cell(i, 5).Range.Text.Length - 2);
                    pr.Gewicht       = Convert.ToDouble(tab3.Cell(i, 6).Range.Text.Substring(0, tab3.Cell(i, 6).Range.Text.Length - 2));
                    pr.Breedte       = Convert.ToDouble(tab3.Cell(i, 7).Range.Text.Substring(0, tab3.Cell(i, 7).Range.Text.Length - 2));
                    pr.Lengte        = Convert.ToDouble(tab3.Cell(i, 8).Range.Text.Substring(0, tab3.Cell(i, 8).Range.Text.Length - 2));
                    pr.Hoogte        = Convert.ToDouble(tab3.Cell(i, 9).Range.Text.Substring(0, tab3.Cell(i, 9).Range.Text.Length - 2));
                    pr.Omschrijving  = tab3.Cell(i, 10).Range.Text.Substring(0, tab3.Cell(i, 10).Range.Text.Length - 2);
                    using (var ctx = new ProjectContext())
                    {
                        Product P = ctx.Products.FirstOrDefault(p => p.ProductNummer == pr.ProductNummer);
                        if (P == null)
                        {
                            if (ctx.Adressen.FirstOrDefault(a => a.Straat + " " + a.Huisnummer + " " + a.Gemeente + " " + a.Postcode + " " + a.Land == ad.Straat + " " + ad.Huisnummer + " " + ad.Gemeente + " " + ad.Postcode + " " + ad.Land) == null)
                            {
                                lev.adress = ad;
                            }
                            else
                            {
                                lev.adress = ctx.Adressen.FirstOrDefault(a => a.Straat + " " + a.Huisnummer + " " + a.Gemeente + " " + a.Postcode + " " + a.Land == ad.Straat + " " + ad.Huisnummer + " " + ad.Gemeente + " " + ad.Postcode + " " + ad.Land);
                            }
                            ctx.SaveChanges();
                            if (ctx.Levranciers.FirstOrDefault(l => l.Naam == lev.Naam) == null)
                            {
                                pr.levrancier = lev;
                            }
                            else
                            {
                                pr.levrancier = ctx.Levranciers.FirstOrDefault(l => l.Naam == lev.Naam);
                            }
                            ctx.Products.Add(pr);
                            ctx.SaveChanges();
                        }
                        else
                        {
                            ctx.Products.FirstOrDefault(p => p.ProductNummer == pr.ProductNummer).UnitsOnStock = P.UnitsOnStock + pr.UnitsOnStock;
                        }
                        ctx.SaveChanges();
                        Producten = ctx.Products.Include("levrancier").ToList();
                    }
                    loaddgvprodut();
                }
            }
        }