Beispiel #1
0
        //private void btnSave_Click(object sender, EventArgs e)
        //{
        //    c.FirstName = txtFName.Text;
        //    c.LastName = txtLName.Text;
        //    c.Address = txtAddress.Text;
        //    c.City = txtCity.Text;
        //    using (var contex = new EFWINFORMDBEntities())
        //    {
        //        if (c.CustomerID == 0)
        //        {
        //            contex.Customer.Add(c);
        //        }
        //        else
        //        {
        //            contex.Entry(c).State = EntityState.Modified;
        //        }
        //        contex.SaveChanges();
        //    }

        //    Clear();
        //    PopulateGrid();
        //    MessageBox.Show("Record Saved Succefully");
        //}

        private void btnSave_Click(object sender, EventArgs e)
        {
            c.FirstName = txtFName.Text;
            c.LastName  = txtLName.Text;
            c.Address   = txtAddress.Text;
            c.City      = txtCity.Text;
            c.Image     = File.ReadAllBytes(dlgOpenImage.FileName);
            using (var contex = new EFWINFORMDBEntities())
            {
                try
                {
                    contex.Customer.AddOrUpdate(c);
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex);
                }

                contex.SaveChanges();
            }

            Clear();
            PopulateGrid();
            MessageBox.Show("Record Saved Sucefully");
        }
Beispiel #2
0
 public void PopulateGrid()
 {
     dataGridView1.AutoGenerateColumns = false;
     using (var contex = new EFWINFORMDBEntities())
     {
         dataGridView1.DataSource = contex.Customer.ToList <Customer>();
     }
 }
Beispiel #3
0
        private void dataGridView1_DoubleClick(object sender, EventArgs e)
        {
            if (dataGridView1.CurrentRow.Index != -1)
            {
                c.CustomerID = Convert.ToInt32(dataGridView1.CurrentRow.Cells["CustomerID"].Value);
                using (var contex = new EFWINFORMDBEntities())
                {
                    var query = contex.Customer.Where(x => x.CustomerID == c.CustomerID).FirstOrDefault();
                    txtFName.Text   = query.FirstName;
                    txtLName.Text   = query.LastName;
                    txtCity.Text    = query.City;
                    txtAddress.Text = query.Address;
                }

                btnSave.Text      = "Update";
                btnDelete.Enabled = true;
            }
        }
Beispiel #4
0
        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Are you Sure", "Delete Operation", MessageBoxButtons.YesNo) == DialogResult.Yes)
            {
                using (var contex = new EFWINFORMDBEntities())
                {
                    c.CustomerID = Convert.ToInt32(dataGridView1.CurrentRow.Cells["CustomerID"].Value);
                    var query = contex.Customer.Where(x => x.CustomerID == c.CustomerID).FirstOrDefault();
                    if (query != null)
                    {
                        contex.Customer.Remove(query);
                    }

                    contex.SaveChanges();
                    PopulateGrid();
                    Clear();
                    MessageBox.Show("Record Saved Sucefully");
                }
            }
        }