Example #1
0
        private void btnDelete_Click(object sender, EventArgs e)
        {
            tblLogFault fault = new tblLogFault();

            if (dgvViewLog.CurrentRow.Index != -1)
            {
                model.LogID = Convert.ToInt32(dgvViewLog.CurrentRow.Cells["LogID"].Value);
                using (MacrocommEntities db = new MacrocommEntities())
                {
                    model = db.tblViewLogs.Where(x => x.LogID == model.LogID).FirstOrDefault();

                    if (MessageBox.Show("Are you sure to delete this record ?", "EF CRUD Operation", MessageBoxButtons.YesNo) == DialogResult.Yes)
                    {
                        var entry = db.Entry(model);
                        if (entry.State == EntityState.Detached)
                        {
                            db.tblViewLogs.Attach(model);
                        }
                        db.tblViewLogs.Remove(model);
                        db.SaveChanges();
                        MessageBox.Show("Deleted Successfully");
                    }
                }
            }
        }
Example #2
0
        private void btnSubmit_Click(object sender, EventArgs e)
        {
            MacrocommEntities context = new MacrocommEntities();
            tblLogFault       objLog  = new tblLogFault();

            if (cmbFaultType.Text == "" || cmbCustomerType.Text == "" || txtComment.Text == "")
            {
                MessageBox.Show("Please enter all requered field.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            else
            {
                try
                {
                    // Adding logs to the FaultLogs table.
                    objLog.Log_Type      = cmbFaultType.Text;
                    objLog.Consumer_Type = cmbCustomerType.Text;
                    objLog.Comment       = txtComment.Text;
                    context.tblLogFaults.Add(objLog);
                    context.SaveChanges();
                    MessageBox.Show("Log request Successfully logged.", "Successful", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    Clear();
                }
                catch
                {
                    MessageBox.Show("Something went wrong please try again.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    Clear();
                }
            }

            using (MacrocommEntities db = new MacrocommEntities())
            {
                tblViewLog view = new tblViewLog();
                var        logg = (from p in db.tblLogFaults
                                   join r in db.tblViewLogs
                                   on p.Ref_No equals r.LogID
                                   where p.Log_Type == "Water,Electricity,Refuse,Sawege"
                                   select new
                {
                    ID = p.Ref_No,
                    cmbFaultType = p.Log_Type,
                    cmbCustomerType = p.Consumer_Type,
                    txtComment = p.Comment,
                    lID = r.LogID,
                    fault = r.Fault_Type,
                    comsumer = r.Consumer_Status,
                    comments = r.Comment
                }).ToList();
                context.tblViewLogs.Add(view);
                context.SaveChanges();
            }
        }