private void button3_Click_1(object sender, EventArgs e) { string name = textBox1.Text; string description = textBox2.Text; string[] sagesIdSt = textBox8.Text.Split(','); int[] sagesId = Array.ConvertAll(sagesIdSt, s => int.Parse(s)); // TODO: check arguments using (var cnt = new SageBookDbContext()) { var sages = cnt.Sages.Where(s => sagesId.Contains(s.id)); Book book = new Book() { name = name, description = description, }; foreach (var sage in sages) { book.Sages.Add(sage); } cnt.Books.Add(book); cnt.SaveChanges(); } loadBookRecords(); }
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(); }
private void button9_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 book = cnt.Sages.First(s => s.id == id); cnt.Sages.Remove(book); cnt.SaveChanges(); } } loadSageRecords(); }
private void button8_Click(object sender, EventArgs e) { if (dataGridView2.CurrentRow.Index != -1) { using (var cnt = new SageBookDbContext()) { int id = int.Parse(dataGridView2.CurrentRow.Cells["id"].Value.ToString()); var book = cnt.Books.First(b => b.id == id); //var entry = cnt.Entry(book); //if (entry.State == EntityState.Detached) // cnt.Books.Attach(book); cnt.Books.Remove(book); cnt.SaveChanges(); } } loadBookRecords(); }
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(); }
private void button1_Click(object sender, EventArgs e) { int id = int.Parse(textBox5.Text.ToString()); string name = textBox1.Text; string description = textBox2.Text; string[] sagesIdSt = textBox8.Text.Split(','); int[] sagesId = Array.ConvertAll(sagesIdSt, s => int.Parse(s)); using (var cnt = new SageBookDbContext()) { var dbBook = cnt.Books.First(b => b.id == id); dbBook.name = name; dbBook.description = description; var sages = cnt.Sages.Where(s => sagesId.Contains(s.id)).ToList(); //dbBook.Sages = dbBook.Sages; //dbBook.Sages = sages; cnt.SaveChanges(); } loadBookRecords(); }
private void loadSageRecords() { using (var cnt = new SageBookDbContext()) { var sages = cnt.Sages.ToList <Sage>(); DataTable table = new DataTable("SageTable"); // Declare variables for DataColumn and DataRow objects. DataColumn column; DataRow row; // Create new DataColumn, set DataType, // ColumnName and add to DataTable. column = new DataColumn(); column.DataType = Type.GetType("System.Int32"); column.ColumnName = "id"; column.ReadOnly = true; column.Unique = true; // Add the Column to the DataColumnCollection. table.Columns.Add(column); column = new DataColumn(); column.DataType = Type.GetType("System.String"); column.ColumnName = "name"; column.ReadOnly = true; column.Unique = false; // Add the Column to the DataColumnCollection. table.Columns.Add(column); column = new DataColumn(); column.DataType = Type.GetType("System.Int32"); column.ColumnName = "age"; column.ReadOnly = true; column.Unique = false; // Add the Column to the DataColumnCollection. table.Columns.Add(column); column = new DataColumn(); column.DataType = System.Type.GetType("System.Byte[]"); column.ColumnName = "photo"; column.ReadOnly = true; column.Unique = false; // Add the Column to the DataColumnCollection. table.Columns.Add(column); column = new DataColumn(); column.DataType = Type.GetType("System.String"); column.ColumnName = "city"; column.ReadOnly = true; column.Unique = false; // Add the Column to the DataColumnCollection. table.Columns.Add(column); column = new DataColumn(); column.DataType = Type.GetType("System.String"); column.ColumnName = "books"; column.ReadOnly = true; column.Unique = false; // Add the Column to the DataColumnCollection. table.Columns.Add(column); foreach (var sage in sages) { row = table.NewRow(); row["id"] = sage.id; row["name"] = sage.name; row["age"] = sage.age; row["photo"] = sage.photo; row["city"] = sage.city; row["books"] = booksIdToString(sage.Books); table.Rows.Add(row); } dataGridView1.DataSource = table; //dataGridView1.DataSource = sages; } }
private void loadBookRecords() { using (var cnt = new SageBookDbContext()) { var books = cnt.Books.ToList <Book>(); List <object> booksFormated = new List <object>(); DataTable table = new DataTable("BookTable"); // Declare variables for DataColumn and DataRow objects. DataColumn column; DataRow row; // Create new DataColumn, set DataType, // ColumnName and add to DataTable. column = new DataColumn(); column.DataType = Type.GetType("System.Int32"); column.ColumnName = "id"; column.ReadOnly = true; column.Unique = true; // Add the Column to the DataColumnCollection. table.Columns.Add(column); column = new DataColumn(); column.DataType = Type.GetType("System.String"); column.ColumnName = "Name"; column.ReadOnly = true; column.Unique = false; // Add the Column to the DataColumnCollection. table.Columns.Add(column); column = new DataColumn(); column.DataType = Type.GetType("System.String"); column.ColumnName = "Description"; column.ReadOnly = true; column.Unique = false; // Add the Column to the DataColumnCollection. table.Columns.Add(column); column = new DataColumn(); column.DataType = Type.GetType("System.String"); column.ColumnName = "SageId"; column.ReadOnly = true; column.Unique = false; // Add the Column to the DataColumnCollection. table.Columns.Add(column); foreach (var book in books) { row = table.NewRow(); row["id"] = book.id; row["Name"] = book.name; row["Description"] = book.description; row["SageId"] = sagesIdToString(book.Sages); table.Rows.Add(row); } dataGridView2.DataSource = table; } }