private void dgvUsers_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            int selectedRowIndex    = this.dgvUsers.SelectedCells[0].RowIndex;
            int selectedColumnIndex = this.dgvUsers.SelectedCells[0].ColumnIndex;

            if (selectedColumnIndex == 0)//update
            {
                //MessageBox.Show("update click  "+this.dgvUsers.Rows[selectedRowIndex].Cells[4].Value);access row value
                cWorker w1 = new cWorker();
                w1.wId               = Convert.ToInt32(this.dgvUsers.Rows[selectedRowIndex].Cells[2].Value);
                w1.wFullName         = this.dgvUsers.Rows[selectedRowIndex].Cells[3].Value.ToString();
                w1.wCNIC             = this.dgvUsers.Rows[selectedRowIndex].Cells[4].Value.ToString();
                w1.wAddress          = this.dgvUsers.Rows[selectedRowIndex].Cells[5].Value.ToString();
                w1.wMobileNo         = this.dgvUsers.Rows[selectedRowIndex].Cells[6].Value.ToString();
                w1.wEmergencyContact = this.dgvUsers.Rows[selectedRowIndex].Cells[7].Value.ToString();
                w1.wdateOfJoin       = Convert.ToDateTime(this.dgvUsers.Rows[selectedRowIndex].Cells[8].Value);
                w1.wIsActive         = Convert.ToBoolean(this.dgvUsers.Rows[selectedRowIndex].Cells[9].Value);
                new cWorkerBLL().UpdateWorker(w1);
                dgvUsers.DataSource = "";
                dgvUsers.DataSource = new cWorkerBLL().getWorkers();
                this.dgvUsers.Columns[2].ReadOnly = true;
                MessageBox.Show("Data Updated...!!!", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            else if (selectedColumnIndex == 1)//delete
            {
                new cWorkerBLL().RemoveWorker(Convert.ToInt32(this.dgvUsers.Rows[selectedRowIndex].Cells[2].Value));
                dgvUsers.DataSource = "";
                dgvUsers.DataSource = new cWorkerBLL().getWorkers();
                this.dgvUsers.Columns[2].ReadOnly = true;
                MessageBox.Show("Data Deleted...!!!", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }
        public int InsertWorker(cWorker worker)
        {
            SqlConnection con = new SqlConnection(Helper.conStr);

            if (con.State == System.Data.ConnectionState.Open)
            {
                con.Close();
            }
            con.Open();
            string query = "INSERT into Workers (wFullname,wCNIC,wAddress,wMobileNo,wEmergencyContact,wdateOfJoin,uIsActive)" +
                           "values('" + worker.wFullName + "','" + worker.wCNIC + "','" + worker.wAddress + "','" + worker.wMobileNo + "','" + worker.wEmergencyContact + "','" + worker.wdateOfJoin + "','" + worker.wIsActive + "')";
            SqlCommand cmd    = new SqlCommand(query, con);
            var        result = cmd.ExecuteNonQuery();

            con.Close();
            cmd.Dispose();
            con.Dispose();
            return(result);
        }
        public int UpdateWorker(cWorker worker)
        {
            SqlConnection con = new SqlConnection(Helper.conStr);

            if (con.State == System.Data.ConnectionState.Open)
            {
                con.Close();
            }
            con.Open();
            string query = "UPDATE Workers " +
                           "SET wFullname = '" + worker.wFullName + "',wCNIC='" + worker.wCNIC + "',wAddress='" + worker.wAddress + "',wMobileNo='" + worker.wMobileNo + "',wEmergencyContact='" + worker.wEmergencyContact + "',wdateOfJoin='" + worker.wdateOfJoin + "',uIsActive='" + worker.wIsActive + "'" +
                           " WHERE wId ='" + worker.wId + "'";
            SqlCommand cmd    = new SqlCommand(query, con);
            var        result = cmd.ExecuteNonQuery();

            con.Close();
            cmd.Dispose();
            con.Dispose();
            return(result);
        }
        public List <cWorker> getWorkers(string workername)
        {
            List <cWorker> workers = new List <cWorker>();

            SqlConnection con = new SqlConnection(Helper.conStr);

            if (con.State == System.Data.ConnectionState.Open)
            {
                con.Close();
            }
            con.Open();

            string        query  = "select * from Workers where wFullName LIKE '%" + workername + "%'";
            SqlCommand    cmd    = new SqlCommand(query, con);
            SqlDataReader reader = cmd.ExecuteReader();

            while (reader.Read())
            {
                cWorker u1 = new cWorker();
                u1.wId               = Convert.ToInt32(reader["wId"]);
                u1.wFullName         = reader["wFullName"].ToString();
                u1.wCNIC             = reader["wCNIC"].ToString();
                u1.wAddress          = reader["wAddress"].ToString();
                u1.wMobileNo         = reader["wMobileNo"].ToString();
                u1.wEmergencyContact = reader["wEmergencyContact"].ToString();
                u1.wdateOfJoin       = Convert.ToDateTime(reader["wdateOfJoin"]);
                u1.wIsActive         = Convert.ToBoolean(reader["uIsActive"]);
                workers.Add(u1);
            }
            reader.Close();
            con.Close();

            con.Dispose();
            cmd.Dispose();
            return(workers);
        }
        private void button1_Click(object sender, EventArgs e)
        {
            cWorker worker = new cWorker();

            worker.wFullName = txtfullname.Text.Trim();
            worker.wCNIC     = txtCNIC.Text.Trim();
            worker.wAddress  = txtAddress.Text.Trim();
            worker.wMobileNo = txtMobileNo.Text.Trim();


            worker.wEmergencyContact = cbEmergency.Text.Trim();
            worker.wdateOfJoin       = dtpJoining.Value;
            worker.wIsActive         = cbIsActive.Checked;

            if (worker.wFullName != "")
            {
                if (worker.wCNIC != "")
                {
                    if (worker.wAddress != "")
                    {
                        if (worker.wMobileNo != "")
                        {
                            if (worker.wEmergencyContact != "")
                            {
                                if (new cWorkerBLL().InsertWorker(worker) > 0)
                                {
                                    //valid record or user
                                    /**/
                                    MessageBox.Show("Data stored...!!!", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
                                    txtfullname.Text = "";
                                    txtCNIC.Text     = "";
                                    txtAddress.Text  = "";
                                    txtMobileNo.Text = "";
                                    cbEmergency.Text = "";
                                    cbEmergency.Items.Clear();
                                    dtpJoining.ResetText();
                                    cbIsActive.Checked = false;
                                }
                                else
                                {
                                    MessageBox.Show("Data cannot be stored...!!!", "Alert", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                                }
                            }
                            else
                            {
                                MessageBox.Show("Emergencey Contact No. Must be entered...!!!", "Alert", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                            }
                        }
                        else
                        {
                            MessageBox.Show("Mobile No should be selected...!!!", "Alert", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        }
                    }
                    else
                    {
                        MessageBox.Show("Address should be filled...!!!", "Alert", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    }
                }
                else
                {
                    MessageBox.Show("CNIC should be filled...!!!", "Alert", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
            }
            else
            {
                MessageBox.Show("Full Name should be filled...!!!", "Alert", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }
 public int UpdateWorker(cWorker worker)
 {
     return(new cWorkerDAL().UpdateWorker(worker));
 }
 public int InsertWorker(cWorker worker)
 {
     return(new cWorkerDAL().InsertWorker(worker));
 }