コード例 #1
0
        private void button5_Click(object sender, EventArgs e)
        {
            if (dataGridView1.CurrentRow.Index != -1)
            {
                using (var cnt = new SageBookDbContext())
                {
                    int    id    = int.Parse(dataGridView1.CurrentRow.Cells["id"].Value.ToString());
                    var    sage  = cnt.Sages.Where(s => s.id == id);
                    string name  = textBox3.Text;
                    int    age   = Int32.Parse(textBox4.Text);
                    byte[] photo = Sage.ImageToByteArray(pictureBox1.Image);
                    string city  = textBox7.Text;

                    updateSageInfo(id, name, age, photo, city);

                    //var booksIds = cnt.Books.Where(b => b.Sages.);
                    //Sage sage = new Sage() { name = name, age = age, city = city, photo = Sage.ImageToByteArray(photo) };
                    var dbSage = cnt.Sages.First(b => b.id == id);
                    dbSage.name  = name;
                    dbSage.age   = age.ToString();
                    dbSage.photo = photo;
                    dbSage.city  = city;
                    //var sages = cnt.Sages.Where(s => sagesId.Contains(s.id)).ToList();
                    //dbBook.Sages = dbBook.Sages;
                    //dbBook.Sages = sages;

                    cnt.SaveChanges();
                }
            }

            loadSageRecords();
        }
コード例 #2
0
 private void updateSageInfo(int id, string name, int age, byte[] photo, string city)
 {
     textBox6.Text     = id.ToString();
     textBox3.Text     = name;
     textBox4.Text     = age.ToString();
     pictureBox1.Image = Sage.ByteArrayToImage(photo);
     textBox7.Text     = city.ToString();
 }
コード例 #3
0
        public Form4()
        {
            sage = new Sage();
            InitializeComponent();

            pictureBox1.Size        = new Size(150, 160);
            pictureBox1.SizeMode    = PictureBoxSizeMode.StretchImage;
            pictureBox1.BorderStyle = BorderStyle.Fixed3D;
        }
コード例 #4
0
        private void button2_Click(object sender, EventArgs e)
        {
            int  index     = dataGridView1.SelectedRows[0].Index;
            int  id        = 0;
            bool converted = Int32.TryParse(dataGridView1[0, index].Value.ToString(), out id);

            if (converted == false)
            {
                return;
            }

            Sage sage = db.Sages.Find(id);

            Form4 f4 = new Form4();

            f4.button2.Text = "Update";

            f4.textBox1.Text        = sage.Name;
            f4.numericUpDown1.Value = sage.Age;
            f4.textBox3.Text        = sage.City;

            if (sage.Image != null)
            {
                MemoryStream mStream = new MemoryStream(sage.Image);
                f4.pictureBox1.Image = Image.FromStream(mStream);
            }

            do
            {
                DialogResult result = f4.ShowDialog(this);

                if (result == DialogResult.Cancel)
                {
                    return;
                }
            }while (f4.label6.Text == "Enter all fields from *");


            sage.Name = f4.textBox1.Text;
            sage.Age  = (int)f4.numericUpDown1.Value;
            sage.City = f4.textBox3.Text;

            if (f4.pictureBox1.Image != null)
            {
                ImageConverter converter = new ImageConverter();
                sage.Image = (byte[])converter.ConvertTo(f4.pictureBox1.Image, typeof(byte[]));
            }
            else
            {
                sage.Image = null;
            }

            db.SaveChanges();
            dataGridView1.Refresh();
            MessageBox.Show("Sage was updated");
        }
コード例 #5
0
        private void button6_Click(object sender, EventArgs e)
        {
            Form4 f4 = new Form4();

            f4.button2.Text = "Ok";

            // f2.ShowDialog(this);
            do
            {
                DialogResult result = f4.ShowDialog(this);

                if (result == DialogResult.Cancel)
                {
                    return;
                }
            }while (f4.label6.Text == "Enter all fields from *");


            //if (f4.button2.Text == "Ok")
            //{
            s = new Sage();

            s.Name = f4.textBox1.Text;
            s.Age  = (int)f4.numericUpDown1.Value;
            s.City = f4.textBox3.Text;
            if (f4.pictureBox1.Image != null)
            {
                ImageConverter converter = new ImageConverter();
                s.Image = (byte[])converter.ConvertTo(f4.pictureBox1.Image, typeof(byte[]));
            }


            db.Sages.Add(s);
            db.SaveChanges();

            int id = 0;

            Sage sg = s;

            id = sg.Id;
            Sage s2 = db.Sages.Find(id);

            s1.Add(s2);


            listBox1.Items.Add(s);

            listBox1.ValueMember   = "Id";
            listBox1.DisplayMember = "Name";


            MessageBox.Show("New sage added");

            //}
        }
コード例 #6
0
        private void button4_Click(object sender, EventArgs e)
        {
            int  index     = dataGridView1.SelectedRows[0].Index;
            int  id        = 0;
            bool converted = Int32.TryParse(dataGridView1[0, index].Value.ToString(), out id);

            if (converted == false)
            {
                return;
            }


            Sage sage = db.Sages.Find(id);

            //db.Books.Load();

            List <Book> book = sage.Books.ToList();

            listBox1.Items.Clear();

            foreach (Book bk in book)
            {
                listBox1.Items.Add(bk.Name);
                //  listBox1.DisplayMember = "Name";
            }

            if (book.Count == 0)
            {
                listBox1.Items.Add("No books");
            }

            pictureBox1.Image = null;


            if (sage.Image != null)
            {
                label2.Text = "";

                MemoryStream mStream = new MemoryStream(sage.Image);
                pictureBox1.Image = Image.FromStream(mStream);
            }
            else
            {
                label2.Text = "";
            }
        }
コード例 #7
0
        private void button1_Click(object sender, EventArgs e)
        {
            Form4 f4 = new Form4();

            f4.button2.Text = "Ok";

            // f2.ShowDialog(this);
            do
            {
                DialogResult result = f4.ShowDialog(this);

                if (result == DialogResult.Cancel)
                {
                    return;
                }
            }while (f4.label6.Text == "Enter all fields from *");

            if (f4.button2.Text == "Ok")
            {
                s = new Sage();

                s.Name = f4.textBox1.Text;
                s.Age  = (int)f4.numericUpDown1.Value;
                s.City = f4.textBox3.Text;

                if (f4.pictureBox1.Image != null)
                {
                    ImageConverter converter = new ImageConverter();
                    s.Image = (byte[])converter.ConvertTo(f4.pictureBox1.Image, typeof(byte[]));

                    /* MemoryStream ms = new MemoryStream();
                     * Image img = Image.FromFile(f4.pictureBox1.Image.ToString());
                     * img.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
                     *
                     * s.Image = ms.ToArray();*/

                    // s.Image = File.ReadAllBytes(f4.pictureBox1.Image.ToString());
                }

                db.Sages.Add(s);
                db.SaveChanges();

                MessageBox.Show("New sage was added");
            }
        }
コード例 #8
0
        private void button1_Click(object sender, EventArgs e)
        {
            Form2 f2 = new Form2();

            f2.button2.Text = "Ok";

            do
            {
                DialogResult result = f2.ShowDialog(this);

                if (result == DialogResult.Cancel)
                {
                    return;
                }
            }while (f2.label6.Text == "Enter all fields from *");



            b      = new Book();
            b.Name = f2.textBox4.Text;
            b.YearOfPublication = (int)f2.numericUpDown1.Value;
            b.Description       = f2.richTextBox1.Text;

            //  List<Sage> sage = new List<Sage>(f2.s1);


            foreach (Sage sga in f2.s1)
            {
                int id;
                id = sga.Id;
                Sage s2 = db.Sages.Find(id);
                b.Sages.Add(s2);
            }

            db.Books.Add(b);
            db.SaveChanges();

            // b.Sages.Clear();

            // f2.s1.Clear();
            MessageBox.Show("New book was added");
        }
コード例 #9
0
        private void button7_Click(object sender, EventArgs e)
        {
            int id = 0;


            if (listBox1.SelectedItem != null)
            {
                Sage sg = (Sage)listBox1.SelectedItem;
                id = sg.Id;
                Sage s2 = db.Sages.Find(id);
                s1.Remove(s2);

                if (button2.Text == "Update")
                {
                    sg22.Add(s2);
                }

                listBox1.Items.Remove(listBox1.SelectedItem);
            }
        }
コード例 #10
0
        private void button3_Click(object sender, EventArgs e)
        {
            if (dataGridView1.SelectedRows.Count < 1)
            {
                return;
            }

            int  index     = dataGridView1.SelectedRows[0].Index;
            int  id        = 0;
            bool converted = Int32.TryParse(dataGridView1[0, index].Value.ToString(), out id);

            if (converted == false)
            {
                return;
            }

            Sage sage = db.Sages.Find(id);

            List <Book> bb = sage.Books.ToList();

            foreach (Book b1 in bb)
            {
                if (b1.Sages.Count == 1)
                {
                    int id1 = 0;

                    id1 = b1.Id;
                    Book book = db.Books.Find(id1);
                    db.Books.Remove(book);
                    db.Entry(book).State = EntityState.Deleted;
                    db.SaveChanges();
                    MessageBox.Show("The book " + b1.Name + "is removed");
                }
            }

            db.Sages.Remove(sage);
            db.Entry(sage).State = EntityState.Deleted;
            db.SaveChanges();

            MessageBox.Show("Sage was removed");
        }
コード例 #11
0
        private void button4_Click(object sender, EventArgs e)
        {
            string name  = textBox3.Text;
            string age   = textBox4.Text;
            var    photo = pictureBox1.Image;
            string city  = textBox7.Text;

            // TODO: check arguments

            using (var cnt = new SageBookDbContext())
            {
                //var booksIds = cnt.Books.Where(b => b.Sages.);
                Sage sage = new Sage()
                {
                    name = name, age = age, city = city, photo = Sage.ImageToByteArray(photo)
                };

                cnt.Sages.Add(sage);
                cnt.SaveChanges();
            }
            loadSageRecords();
        }
コード例 #12
0
        private void button5_Click(object sender, EventArgs e)
        {
            int id = 0;

            Sage sg = (Sage)comboBox1.SelectedItem;

            id = sg.Id;
            Sage s2 = db.Sages.Find(id);

            s1.Add(s2);


            listBox1.Items.Add(comboBox1.SelectedItem);

            listBox1.ValueMember   = "Id";
            listBox1.DisplayMember = "Name";


            // listBox1.Items.Add(comboBox1.SelectedItem);

            //  b.Sages.Add(ss);
        }
コード例 #13
0
        private void button3_Click(object sender, EventArgs e)
        {
            int  index     = dataGridView1.SelectedRows[0].Index;
            int  id        = 0;
            bool converted = Int32.TryParse(dataGridView1[0, index].Value.ToString(), out id);

            if (converted == false)
            {
                return;
            }

            Book book = db.Books.Find(id);

            Form2 f2 = new Form2();

            f2.button2.Text = "Update";

            List <Sage> sage = book.Sages.ToList <Sage>();

            //f2.s1.Clear();
            //f2.sg22.Clear();

            foreach (Sage sg in sage)
            {
                int id2 = 0;

                id2 = sg.Id;
                Sage s2 = db.Sages.Find(id2);
                f2.s1.Add(s2);


                f2.listBox1.Items.Add(sg);
                f2.listBox1.ValueMember   = "Id";
                f2.listBox1.DisplayMember = "Name";

                //  book.Sages.Remove(s2);
                //  db.Entry(book).State = EntityState.Deleted;
            }

            f2.textBox4.Text        = book.Name;
            f2.numericUpDown1.Value = book.YearOfPublication;
            f2.richTextBox1.Text    = book.Description;

            do
            {
                DialogResult result = f2.ShowDialog(this);

                if (result == DialogResult.Cancel)
                {
                    return;
                }
            }while (f2.label6.Text == "Enter all fields from *");

            book.Name = f2.textBox4.Text;
            book.YearOfPublication = (int)f2.numericUpDown1.Value;
            book.Description       = f2.richTextBox1.Text;



            List <Sage> sage1 = f2.s1;

            book.Sages.Clear();

            foreach (Sage sa in sage1)
            {
                int id4 = 0;
                id4 = sa.Id;
                Sage s2 = db.Sages.Find(id4);
                book.Sages.Add(s2);
            }

            List <Sage> sage2 = f2.sg22;

            // book.Sages.Clear();

            foreach (Sage sa in sage2)
            {
                int id4 = 0;
                id4 = sa.Id;
                Sage s2 = db.Sages.Find(id4);
                book.Sages.Remove(s2);
                //db.Entry(book.Sages).State = EntityState.Deleted;
            }

            //book.Sages.Clear();
            db.Entry(book).State = EntityState.Modified;

            db.SaveChanges();
            dataGridView1.Refresh();

            f2.sg22.Clear();
            f2.s1.Clear();
            MessageBox.Show("Book was updated");
        }