Example #1
0
    protected void ibtnSave_Click(object sender, ImageClickEventArgs e)
    {
        string   empName = (txtFirstName.Text.Replace("'", "").Replace("\"", "").Trim() + " " + txtLastName.Text.Replace("'", "").Replace("\"", "").Trim());
        string   email   = ((txtEmail.Text != "") ? (txtEmail.Text.Replace("'", "").Replace("\"", "")) + lblEmail.Text : "");
        DateTime stDate  = (txtBegin.Text != "") ? Convert.ToDateTime(txtBegin.Text) : DateTime.Now;
        DateTime endDate = (txtEnd.Text != "") ? Convert.ToDateTime(txtEnd.Text) : DateTime.Now;

        txtPhone.Text = RemovePhoneNumberFormat(txtPhone.Text.Replace("'", "").Trim());
        txtFax.Text   = RemovePhoneNumberFormat(txtFax.Text.Replace("'", "").Trim());

        if (endDate >= stDate)
        {
            if (ViewState["Mode"].ToString() == "Add")
            {
                columnValue = "'" + empName + "'," +
                              "'" + ddlHireLocation.SelectedItem.Value.ToString() + "'," +
                              "'" + txtEmpNo.Text.Replace("'", "").Replace("\"", "") + "'," +
                              ((ddlStatus.SelectedItem.Value.ToString() != "") ? ("'" + ddlStatus.SelectedItem.Value.ToString() + "'") : "NULL") + "," +
                              "'" + dtpHireDate.SelectedDate + "'," +
                              ((ddlDepartment.SelectedItem.Value.ToString() != "") ? ddlDepartment.SelectedItem.Value.ToString() : "NULL") + "," +
                              "'" + ((ddlPosition.SelectedItem.Value.ToString() != "") ? ddlPosition.SelectedItem.Value.ToString() : "NULL") + "'," +
                              ((ddlShift.SelectedItem.Value.ToString() != "") ? ("'" + ddlShift.SelectedItem.Value.ToString() + "'") : "NULL") + "," +
                              ((ddlSupervisior.SelectedItem.Value.ToString() != "") ? ("'" + ddlSupervisior.SelectedItem.Value.ToString() + "'") : "NULL") + "," +
                              "'" + txtFirstName.Text.Replace("'", "").Replace("\"", "") + "'," +
                              "'" + txtMiddleName.Text.Replace("'", "").Replace("\"", "").Trim() + "'," +
                              "'" + txtLastName.Text.Replace("'", "").Replace("\"", "") + "'," +
                              ((ddlSalutation.SelectedItem.Value.ToString() != "") ? ("'" + ddlSalutation.SelectedItem.Value.ToString().Trim() + "'") : "NULL") + "," +
                              "'" + email + "'," +
                              "'" + txtPhone.Text + "'," +
                              "'" + txtFax.Text + "'," +
                              "'" + ddlPayCode.SelectedItem.Value.ToString() + "'," +
                              "'" + txtPayrollEmpNo.Text.Replace("'", "").Replace("\"", "") + "'," +
                              "'" + ddlPayrollLoc.SelectedItem.Value.ToString() + "'," +
                              ((txtHoliday.Text != "") ? txtHoliday.Text : "NULL") + "," +
                              ((txtSick.Text != "") ? txtSick.Text : "NULL") + "," +
                              ((txtVacation.Text != "") ? txtVacation.Text : "NULL") + "," +
                              "'" + txtBegin.Text + "'," +
                              "'" + txtEnd.Text + "'," +
                              "'" + txtAbsenceBal.Text + "'," +
                              "" + ((txtBalance.Text != "") ? txtBalance.Text : "NULL") + "," +
                              "'" + Session["UserName"].ToString() + "'," +
                              "'" + DateTime.Now.ToShortDateString() + "'," +
                              ((ddlPosition.SelectedItem.Value.ToString().Trim().ToLower() == "supervisior") ? "'Y'" : "NULL"); // +"'";

                string whereClause = "EmployeeName='" + empName + "' and DeleteDt is null";

                bool checkUser   = employee.CheckUser(whereClause);
                bool checkUserID = employee.CheckUserID(txtEmpNo.Text.Trim());

                Label lbl = Page.FindControl("lblMessage") as Label;
                if (checkUser)
                {
                    employee.DisplayMessage(MessageType.Failure, "Employee Name already exists", lbl);
                    UpdatePanel pnl = Page.FindControl("upnlMessage") as UpdatePanel;
                    pnl.Update();
                    status = "Failure";
                }
                else if (checkUserID)
                {
                    employee.DisplayMessage(MessageType.Failure, "Employee No. already exists", lbl);
                    UpdatePanel pnl = Page.FindControl("upnlMessage") as UpdatePanel;
                    pnl.Update();
                    status = "Failure";
                }
                else
                {
                    string _empID = employee.InsertEmployeeData(columnValue);
                    ViewState["EmpID"] = _empID;
                    status             = "NewEmployeeSuccess";

                    string userSettingcolumnValue = "'" + ddlHireLocation.SelectedItem.Text.ToString().Split('-')[1].ToString().Trim() + "'," +
                                                    "'" + ddlHireLocation.SelectedItem.Value.ToString() + "'," +
                                                    "'" + _empID + "'," +
                                                    "'" + Session["UserName"].ToString() + "'," +
                                                    "'" + DateTime.Now.ToShortDateString() + "'";
                    UserID = employee.InsertUserData(userSettingcolumnValue);

                    ViewState["UserID"] = UserID.ToString();
                    EmployeeName        = empName;
                    ViewState["Mode"]   = "Save";
                }
            }
            else
            {
                string empID       = ViewState["EmpID"].ToString();
                string updateValue = "EmployeeName='" + empName.Trim() + "'," +
                                     "Location='" + ddlHireLocation.SelectedItem.Value.ToString() + "'," +
                                     "EmployeeNo='" + txtEmpNo.Text.Replace("'", "").Replace("\"", "") + "'," +
                                     "EmploymentStatus=" + ((ddlStatus.SelectedItem.Value.ToString() != "") ? ("'" + ddlStatus.SelectedItem.Value.ToString() + "'"): "NULL") + "," +
                                     "HireDt='" + dtpHireDate.SelectedDate + "'," +
                                     "DepartmentNo=" + ((ddlDepartment.SelectedItem.Value.ToString() != "") ? ddlDepartment.SelectedItem.Value.ToString() : "NULL") + "," +
                                     "DefaultJobCd=" + ((ddlPosition.SelectedItem.Value.ToString() != "") ? ("'" + ddlPosition.SelectedItem.Value.ToString() + "'") : "NULL") + "," +
                                     "Shift='" + ddlShift.SelectedItem.Value.ToString() + "'," +
                                     "SupervisorEmpID=" + ((ddlSupervisior.SelectedItem.Value.ToString() != "") ? ddlSupervisior.SelectedItem.Value.ToString() : "NULL") + "," +
                                     "SupervisorInd='" + ((ddlPosition.SelectedItem.Value.ToString().Trim().ToLower() == "supervisior") ? "Y" : "") + "'," +
                                     "FirstName='" + txtFirstName.Text.Replace("'", "").Replace("\"", "") + "'," +
                                     "MiddleInitial='" + txtMiddleName.Text.Replace("'", "").Replace("\"", "") + "'," +
                                     "LastName='" + txtLastName.Text.Replace("'", "").Replace("\"", "") + "'," +
                                     "Salutation=" + ((ddlSalutation.SelectedItem.Value.ToString() != "")?("'" + ddlSalutation.SelectedItem.Value.ToString() + "'"):"NULL") + "," +
                                     "EmailAddress='" + email + "'," +
                                     "PhoneNo='" + txtPhone.Text + "'," +
                                     "FaxNo='" + txtFax.Text + "'," +
                                     "PayCd='" + ddlPayCode.SelectedItem.Value.ToString() + "'," +
                                     "PayRollEmployeeNo='" + txtPayrollEmpNo.Text.Replace("'", "").Replace("\"", "") + "'," +
                                     "PayRollLocation=" + ((ddlPayrollLoc.SelectedItem.Value.ToString() != "")?("'" + ddlPayrollLoc.SelectedItem.Value.ToString() + "'"):"NULL") + "," +
                                     "HolidayHours=" + ((txtHoliday.Text != "") ? txtHoliday.Text : "NULL") + "," +
                                     "SickHours=" + ((txtSick.Text != "") ? txtSick.Text : "NULL") + "," +
                                     "VacationHours=" + ((txtVacation.Text != "") ? txtVacation.Text : "NULL") + "," +
                                     "LeaveBeginDt='" + txtBegin.Text + "'," +
                                     "LeaveEndDt='" + txtEnd.Text + "'," +
                                     "LeaveBalanceDt='" + txtAbsenceBal.Text + "'," +
                                     "BenefitBalance=" + ((txtBalance.Text != "") ? txtBalance.Text : "NULL") + "," +
                                     "ChangeID='" + Session["UserName"].ToString() + "'," +
                                     "ChangeDt='" + DateTime.Now.ToShortDateString() + "'";

                employee.UpdateEmployeeData(updateValue, empID);

                int userID = Convert.ToInt32((employee.GetUserIDByEmployeeID(empID)));
                ViewState["UserID"] = userID.ToString();

                string userSettingColumnValue = " Location='" + ddlHireLocation.SelectedItem.Text.ToString() + "'," +
                                                "IMLoc='" + ddlHireLocation.SelectedItem.Value.ToString() + "'";
                employee.UpdateUserData(userSettingColumnValue, userID.ToString());
                status = "Success";

                EmpLocation       = (ddlHireLocation.SelectedItem.Value.ToString()) + "-" + (ddlHireLocation.SelectedItem.Text.ToString());
                EmployeeName      = empName;
                ViewState["Mode"] = "Save";
            }
        }
        else
        {
            Label lbl = Page.FindControl("lblMessage") as Label;

            employee.DisplayMessage(MessageType.Failure, "End Date must be greater than Start Date", lbl);
            UpdatePanel pnl = Page.FindControl("upnlMessage") as UpdatePanel;
            pnl.Update();
            txtEnd.Text = "";
            // ClearControl();
            status = "Failure";
        }
    }