private void button5_Click(object sender, EventArgs e) { if (dataGridView1.Rows.Count <= 0) { MessageBox.Show("Tidak ada data untuk di simpan", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } else { db_Form_Penjualan_Eceran fe = new db_Form_Penjualan_Eceran() { No_Form_Penjualan_Eceran = textBox6.Text, ID_Karyawan = textBox1.Text, Tanggal = dateTimePicker1.Text }; db.AddTodb_Form_Penjualan_Eceran(fe); db.SaveChanges(); for (int i = 0; i < dataGridView1.RowCount; i++) { string ih = ""; string ah = ""; int h = 0, l = 0; try { ih = dataGridView1.Rows[i].Cells[1].Value.ToString(); h = int.Parse(dataGridView1.Rows[i].Cells[3].Value.ToString()); l = int.Parse(dataGridView1.Rows[i].Cells[4].Value.ToString()); ah = dataGridView1.Rows[i].Cells[2].Value.ToString(); } catch (Exception) { } db_Detail_Form_Penjualan_Eceran dfe = new db_Detail_Form_Penjualan_Eceran() { No_Form_Penjualan_Eceran = textBox6.Text, ID_Barang = ih, Harga = h, Jumlah = l, Nama_Barang = ah }; db.AddTodb_Detail_Form_Penjualan_Eceran(dfe); db.SaveChanges(); try { var up = (from a in db.db_Barang where a.ID_Barang == ih select a).FirstOrDefault(); if (up.Stock != null) { up.Stock = up.Stock - l; db.SaveChanges(); if (up.Stock <= 500) { MessageBox.Show("Stock " + dataGridView1.Rows[i].Cells[2].Value.ToString() + " mulai menipis, harap segera buat Faktur Pemesanan! ", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information); } } } catch (Exception) { } } MessageBox.Show("Transaksi berhasil", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information); button4.Enabled = false; button5.Enabled = false; button6.Enabled = false; id = textBox6.Text; } }