Example #1
0
 private void Form1_Load(object sender, EventArgs e)
 {
     using (Trade_Of_UkraineEntities db = new Trade_Of_UkraineEntities())
     {
         var scales = db.Scale;
         if (scales.Count() == 0)
         {
             var scale = new Scale
             {
                 Name        = "All",
                 Description = "All teriotory of Ukraine"
             };
             db.Scale.Add(scale);
             MessageBox.Show("1 row(s) added");
             db.SaveChanges();
             var scale2 = new Scale
             {
                 Name        = "Kyiv",
                 Description = "All teriotory of Ukraine"
             };
             db.Scale.Add(scale2);
             MessageBox.Show("1 row(s) added");
             db.SaveChanges();
         }
     }
 }
Example #2
0
 public Form1()
 {
     InitializeComponent();
     using (Trade_Of_UkraineEntities db = new Trade_Of_UkraineEntities())
     {
         var scales = db.Scale;
         foreach (var scale in scales)
         {
             comboBox1.Items.Add(scale.Name);
         }
     }
     checkBox1.Checked = true;
     checkBox2.Checked = true;
     CheckCheckBox();
 }
Example #3
0
        private void button1_Click(object sender, EventArgs e)
        {
            if (radioGet.Checked)
            {
                if (checkBox1.Checked && !checkBox2.Checked)
                {
                    using (Trade_Of_UkraineEntities db = new Trade_Of_UkraineEntities())
                    {
                        Regex r = new Regex("[0-9]$");
                        Match m = r.Match(IDBox.Text);
                        if (m.Success)
                        {
                            int id      = int.Parse(IDBox.Text);
                            var company = db.Company.Where(u => u.ID == id);
                            dataGridView1.Visible    = true;
                            dataGridView1.DataSource = company.ToList();
                        }
                        else
                        {
                            MessageBox.Show("Wrong input!");
                        }
                    }
                }
                if (checkBox1.Checked && checkBox2.Checked)
                {
                    using (Trade_Of_UkraineEntities db = new Trade_Of_UkraineEntities())
                    {
                        Regex r  = new Regex("^[0-9]$");
                        Match m1 = r.Match(IDBox.Text);
                        if (m1.Success && comboBox1.Text != "")
                        {
                            int id       = int.Parse(IDBox.Text);
                            var scaletxt = comboBox1.Text;
                            var scale    = db.Scale.FirstOrDefault(u => u.Name == scaletxt);
                            var company  = db.Company.Where(u => u.ID == id && u.TradingScaleId == scale.ID);
                            dataGridView1.Visible    = true;
                            dataGridView1.DataSource = company.ToList();
                        }
                        else
                        {
                            MessageBox.Show("Wrong input!");
                        }
                    }
                }
                if (!checkBox1.Checked && checkBox2.Checked)
                {
                    using (Trade_Of_UkraineEntities db = new Trade_Of_UkraineEntities())
                    {
                        if (comboBox1.Text != "")
                        {
                            var scaletxt = comboBox1.Text;
                            var scale    = db.Scale.FirstOrDefault(u => u.Name == scaletxt);
                            var company  = db.Company.Where(u => u.TradingScaleId == scale.ID);
                            dataGridView1.Visible    = true;
                            dataGridView1.DataSource = company.ToList();
                        }
                        else
                        {
                            MessageBox.Show("Wrong input!");
                        }
                    }
                }
                if (!checkBox1.Checked && !checkBox2.Checked)
                {
                    using (Trade_Of_UkraineEntities db = new Trade_Of_UkraineEntities())
                    {
                        var company = db.Company.Join(db.Scale,
                                                      c => c.TradingScaleId,
                                                      s => s.ID,
                                                      (c, s) => new
                        {
                            Id               = c.ID,
                            Name             = c.Name,
                            Owner            = c.Owner,
                            Shops            = c.NumberOfShops,
                            HeadCity         = c.HeadCity,
                            TradingScaleName = s.Name,
                            TradingScale     = s.Description
                        });
                        dataGridView1.Visible    = true;
                        dataGridView1.DataSource = company.ToList();
                    }
                }
            }
            if (radioDelete.Checked)
            {
                using (Trade_Of_UkraineEntities db = new Trade_Of_UkraineEntities())
                {
                    Regex r = new Regex("[0-9]$");
                    Match m = r.Match(IDBox.Text);
                    if (m.Success)
                    {
                        int id    = int.Parse(IDBox.Text);
                        var cShow = db.Company.Where(c => c.ID == id);
                        dataGridView1.DataSource = cShow.ToList();
                        var company = db.Company.FirstOrDefault(c => c.ID == id);
                        db.Company.Remove(company);
                        db.SaveChanges();
                        MessageBox.Show("1 row(s) deleted");
                    }
                    else
                    {
                        MessageBox.Show("Wrong input!");
                    }
                }
            }
            if (radioAdd.Checked)
            {
                using (Trade_Of_UkraineEntities db = new Trade_Of_UkraineEntities())
                {
                    var scaletxt = comboBox1.Text;
                    var scale    = db.Scale.FirstOrDefault(u => u.Name == scaletxt);

                    Regex r = new Regex("[0-9]$");

                    if (scale != null && r.Match(scale.ID.ToString()).Success&& r.Match(ShopsBox.Text).Success &&
                        !String.IsNullOrWhiteSpace(NameBox.Text) &&
                        !String.IsNullOrWhiteSpace(OwnersBox.Text))
                    {
                        Company company = new Company
                        {
                            Name           = NameBox.Text,
                            TradingScaleId = scale.ID,
                            HeadCity       = CityBox.Text,
                            Owner          = OwnersBox.Text,
                            NumberOfShops  = int.Parse(ShopsBox.Text)
                        };
                        //dataGridView1.DataSource;
                        db.Company.Add(company);
                        MessageBox.Show("1 row(s) added");
                        db.SaveChanges();
                        var cShow = db.Company
                                    .Where(u => u.Name == NameBox.Text && u.TradingScaleId == scale.ID &&
                                           u.HeadCity == CityBox.Text && u.Owner == OwnersBox.Text);
                        dataGridView1.DataSource = cShow.ToList();
                    }
                    else
                    {
                        MessageBox.Show("Wrong input!");
                    }
                }
            }
            if (radioPut.Checked)
            {
                using (Trade_Of_UkraineEntities db = new Trade_Of_UkraineEntities())
                {
                    var scaletxt = comboBox1.Text;
                    var scale    = db.Scale.FirstOrDefault(u => u.Name == scaletxt);

                    Regex r = new Regex("[0-9]$");
                    if (scale != null && r.Match(IDBox.Text).Success &&
                        r.Match(scale.ID.ToString()).Success &&
                        r.Match(ShopsBox.Text).Success &&
                        !String.IsNullOrWhiteSpace(NameBox.Text) &&
                        !String.IsNullOrWhiteSpace(OwnersBox.Text))
                    {
                        int id   = int.Parse(IDBox.Text);
                        var comp = db.Company.FirstOrDefault(c => c.ID == id);


                        comp.Name           = NameBox.Text;
                        comp.TradingScaleId = scale.ID;
                        comp.HeadCity       = CityBox.Text;
                        comp.Owner          = OwnersBox.Text;
                        comp.NumberOfShops  = int.Parse(ShopsBox.Text);

                        db.SaveChanges();
                        var cShow = db.Company.Where(c => c.ID == id);
                        MessageBox.Show("1 row(s) updated");
                        dataGridView1.DataSource = cShow.ToList();
                    }
                    else
                    {
                        MessageBox.Show("Wrong input!");
                    }
                }
            }
        }
 private void button1_Click(object sender, EventArgs e)
 {
     if (radioButton1.Checked)
     {
         using (Trade_Of_UkraineEntities db = new Trade_Of_UkraineEntities())
         {
             var scales = db.Scale;
             dataGridView1.Visible    = true;
             dataGridView1.DataSource = scales.ToList();
         }
     }
     if (radioButton2.Checked)
     {
         using (Trade_Of_UkraineEntities db = new Trade_Of_UkraineEntities())
         {
             if (!String.IsNullOrWhiteSpace(textBox2.Text) && !String.IsNullOrWhiteSpace(textBox3.Text))
             {
                 var scale = new Scale
                 {
                     Name        = textBox2.Text,
                     Description = textBox3.Text
                 };
                 db.Scale.Add(scale);
                 MessageBox.Show("1 row(s) added");
                 db.SaveChanges();
                 var sShow = db.Scale.Where(u => u.Name == textBox2.Text && u.Description == textBox3.Text);
                 dataGridView1.DataSource = sShow.ToList();
             }
             else
             {
                 MessageBox.Show("Wrong input!");
             }
         }
     }
     if (radioButton3.Checked)
     {
         using (Trade_Of_UkraineEntities db = new Trade_Of_UkraineEntities())
         {
             Regex r = new Regex("[0-9]$");
             Match m = r.Match(textBox1.Text);
             if (m.Success && !String.IsNullOrWhiteSpace(textBox2.Text) && !String.IsNullOrWhiteSpace(textBox3.Text))
             {
                 var scaletxt = int.Parse(textBox1.Text);
                 var scale    = db.Scale.FirstOrDefault(u => u.ID == scaletxt);
                 scale.Name        = textBox2.Text;
                 scale.Description = textBox3.Text;
                 db.SaveChanges();
                 MessageBox.Show("1 row(s) updated");
                 var sShow = db.Scale.Where(u => u.ID == scaletxt);
                 dataGridView1.DataSource = sShow.ToList();
             }
             else
             {
                 MessageBox.Show("Wrong input!");
             }
         }
     }
     if (radioButton4.Checked)
     {
         using (Trade_Of_UkraineEntities db = new Trade_Of_UkraineEntities())
         {
             Regex r = new Regex("[0-9]$");
             Match m = r.Match(textBox1.Text);
             if (m.Success)
             {
                 int id    = int.Parse(textBox1.Text);
                 var scale = db.Scale.FirstOrDefault(c => c.ID == id);
                 db.Scale.Remove(scale);
                 db.SaveChanges();
                 MessageBox.Show("1 row(s) deleted");
                 var sShow = db.Scale.Where(u => u.ID == id);
                 dataGridView1.DataSource = sShow.ToList();
             }
             else
             {
                 MessageBox.Show("Wrong input!");
             }
         }
     }
 }