protected void btnSave_Click(object sender, EventArgs e)
    {
        try
        {
            int flag = 0;
            if (ddlTeam.SelectedIndex == 0)
            {
                ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "select team", "alert('Select Team')", true);
                return;
            }
            foreach (GridViewRow row in gvUsers.Rows)
            {
                if (row.RowType == DataControlRowType.DataRow)
                {
                    CheckBox chkRow = (row.Cells[0].FindControl("chkRow") as CheckBox);
                    if (chkRow.Checked)
                    {
                        string SysName = (row.Cells[6].FindControl("txtSystemName") as TextBox).Text;

                        if (SysName.ToLower().StartsWith("corp\\") == false)
                        {
                            ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "Sys Name", "alert('Enter valid System/Network UserName')", true);
                            return;
                        }

                        string empId        = (row.Cells[1].FindControl("lblGvEmpId") as Label).Text;
                        string firstname    = (row.Cells[2].FindControl("lblGvFirstName") as Label).Text;
                        string lastname     = (row.Cells[3].FindControl("lblGvLastName") as Label).Text;
                        string username     = firstname + " " + lastname;
                        string managerId    = (row.Cells[5].FindControl("lblGvManagerId") as Label).Text;
                        string managerEmail = (row.Cells[5].FindControl("lblManagerEmail") as Label).Text;
                        string email        = (row.Cells[4].FindControl("lblGvEmailId") as Label).Text;
                        string checkEmp     = (row.Cells[8].FindControl("lblGvCheckempId") as Label).Text;

                        if (string.IsNullOrEmpty(checkEmp))
                        {
                            bool checkUser = objAccount.CheckUserName(username);
                            if (checkUser == true)
                            {
                                ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "select team", "alert('Another user with same name exist in RTM')", true);
                                return;
                            }

                            bool result = objAccount.AddNewUser(ddlTeam.SelectedValue, empId, username, SysName, string.Empty, email, Session["UID"].ToString(), managerId, managerEmail, "", "", "", "", "");
                            if (result == true)
                            {
                                result = objAccount.AddAccessLevel(empId, Session["UID"].ToString());
                                if (result == true)
                                {
                                    StringBuilder sb = new StringBuilder();
                                    sb.AppendLine("Hi Team,");
                                    sb.AppendLine("Details of User added into RTM database is as follows");
                                    sb.AppendLine("Employee Id:" + empId);
                                    sb.AppendLine("Employee Name:" + username);
                                    sb.AppendLine("Employee email id:" + email);
                                    sb.AppendLine("System Name:" + SysName);
                                    sb.AppendLine("Team Name:" + ddlTeam.SelectedItem.Text);
                                    sb.AppendLine("Reporting Manager Email Id:" + managerEmail);
                                    sb.AppendLine("");
                                    sb.AppendLine("Please update the gender information as well.");
                                    sb.AppendLine("");
                                    sb.AppendLine("Thanks,");
                                    sb.AppendLine("RTM Support");

                                    MailMessage message1 = new MailMessage();
                                    SmtpClient  smtp     = new SmtpClient();

                                    message1.From = new MailAddress("*****@*****.**");
                                    message1.To.Add(new MailAddress("*****@*****.**"));
                                    message1.To.Add(new MailAddress("*****@*****.**"));
                                    message1.Subject    = "New User added to RTM database.";
                                    message1.Body       = sb.ToString();
                                    message1.IsBodyHtml = true;

                                    smtp.Port           = 25;
                                    smtp.Host           = "10.0.5.104";
                                    smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
                                    smtp.EnableSsl      = false;

                                    smtp.Send(message1);

                                    ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "Success", "alert('User added Successfully')", true);
                                    return;
                                }
                                else
                                {
                                    ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "access", "alert('User added But failed to provid access please contact RTM Support')", true);
                                    return;
                                }
                            }
                            else
                            {
                                ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "failed", "alert('User not added please try again or contact RTM Support')", true);
                                return;
                            }
                        }
                        else
                        {
                            bool result = objAccount.UpdateUser(empId, SysName, managerId, managerEmail, ddlTeam.SelectedValue, Session["UID"].ToString(), "", "", "", "", "", "");

                            if (result == true)
                            {
                                StringBuilder sb = new StringBuilder();
                                sb.AppendLine("Hi Team,");
                                sb.AppendLine("Details of User updated into RTM database is as follows");
                                sb.AppendLine("Employee Id:" + empId);
                                sb.AppendLine("Employee Name:" + username);
                                sb.AppendLine("Employee email id:" + email);
                                sb.AppendLine("System Name:" + SysName);
                                sb.AppendLine("Team Name:" + ddlTeam.SelectedItem.Text);
                                sb.AppendLine("Reporting Manager Email Id:" + managerEmail);
                                sb.AppendLine("");
                                sb.AppendLine("Please update the gender information as well.");
                                sb.AppendLine("");
                                sb.AppendLine("Thanks,");
                                sb.AppendLine("RTM Support");

                                MailMessage message1 = new MailMessage();
                                SmtpClient  smtp     = new SmtpClient();

                                message1.From = new MailAddress("*****@*****.**");
                                message1.To.Add(new MailAddress("*****@*****.**"));
                                message1.To.Add(new MailAddress("*****@*****.**"));
                                message1.Subject    = "User details updated to RTM database.";
                                message1.Body       = sb.ToString();
                                message1.IsBodyHtml = true;

                                smtp.Port           = 25;
                                smtp.Host           = "10.0.5.104";
                                smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
                                smtp.EnableSsl      = false;

                                smtp.Send(message1);

                                ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "Success", "alert('User added Successfully')", true);
                                return;
                            }
                            else
                            {
                                ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "failed", "alert('User not added please try again or contact RTM Support')", true);
                                return;
                            }
                        }
                    }
                }
            }

            ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "Error", "alert('Please select user')", true);
        }
        catch (Exception ex)
        {
            ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "Error", "alert('" + ex.Message + "')", true);
        }
    }
Example #2
0
    protected void btnAdd_Click(object sender, EventArgs e)
    {
        if (Convert.ToInt32(hfTeamId.Value) == 0)
        {
            lblError.Text      = "Select Team";
            lblError.ForeColor = Color.Red;
            return;
        }
        if (string.IsNullOrWhiteSpace(txtEmpId.Text))
        {
            lblError.Text      = "Enter valid Employee Id";
            lblError.ForeColor = Color.Red;
            return;
        }

        if (txtEmpId.Text.Length > 15)
        {
            lblError.Text      = "Enter valid Employee Id, Length exceeds 15 digits";
            lblError.ForeColor = Color.Red;
            return;
        }

        if (string.IsNullOrWhiteSpace(txtname.Text))
        {
            lblError.Text      = "Enter Name";
            lblError.ForeColor = Color.Red;
            return;
        }
        if (string.IsNullOrWhiteSpace(txtSystemName.Text))
        {
            lblError.Text      = "Enter valid System User Name";
            lblError.ForeColor = Color.Red;
            return;
        }
        bool validEmail = ValidateEmail(txtEmail.Text);

        if (validEmail == false)
        {
            lblError.Text      = "Enter valid email";
            lblError.ForeColor = Color.Red;
            return;
        }
        else
        {
            lblError.Text = string.Empty;
        }

        if (ValidateEmail(txtMgrEmailId.Text) == false)
        {
            lblError.Text      = "Enter valid email";
            lblError.ForeColor = Color.Red;
            return;
        }
        else
        {
            lblError.Text = string.Empty;
        }

        if (string.IsNullOrWhiteSpace(txtDOJ.Text))
        {
            lblError.Text      = "Enter valid Date Of Joining";
            lblError.ForeColor = Color.Red;
            return;
        }

        if (string.IsNullOrWhiteSpace(txtMgrId.Text))
        {
            lblError.Text      = "Enter valid Manager Employee Id";
            lblError.ForeColor = Color.Red;
            return;
        }


        if (txtMgrId.Text.Length > 15)
        {
            lblError.Text      = "Enter valid Manager Employee Id, Length exceeds 6 digits";
            lblError.ForeColor = Color.Red;
            return;
        }

        if (ddlGender.SelectedIndex == 0)
        {
            lblError.Text      = "Please select Gender";
            lblError.ForeColor = Color.Red;
            return;
        }

        if (ddlHourly.SelectedIndex == 0)
        {
            lblError.Text      = "Please select Hourly type";
            lblError.ForeColor = Color.Red;
            return;
        }


        if (CheckDate(txtDOJ.Text) == false)
        {
            lblError.Text      = "Sorry, Date Of Joining is invalid";
            lblError.ForeColor = Color.Red;
            return;
        }

        if (ddlType.SelectedIndex == 0)
        {
            lblError.Text      = "Please select Employee type";
            lblError.ForeColor = Color.Red;
            return;
        }

        bool result = true;

        string Hourly = "0";

        if (ddlHourly.SelectedValue == "No")
        {
            Hourly = "0";
        }

        else if (ddlHourly.SelectedValue == "Yes")
        {
            Hourly = "1";
        }

        if (btnAdd.Text == "Add Record")
        {
            bool checkEmpId = objAccount.CheckEmpId(txtEmpId.Text.Trim());
            if (checkEmpId == true)
            {
                lblError.Text      = "Employee Id already exist";
                lblError.ForeColor = Color.Red;
                return;
            }
            bool checkUser = objAccount.CheckUserName(txtname.Text.Trim());
            if (checkUser == true)
            {
                lblError.Text      = "Employee name already exist";
                lblError.ForeColor = Color.Red;
                return;
            }
            result = objAccount.AddNewUser(hfTeamId.Value, txtEmpId.Text.Trim(), txtname.Text.Trim(), txtSystemName.Text.Trim(), ddlGender.SelectedItem.Text, txtEmail.Text.Trim(), Session["UID"].ToString(), txtMgrId.Text.Trim(), txtMgrEmailId.Text.Trim(), Hourly, txtDOJ.Text.Trim(), ddlType.SelectedValue, txtEmpNo.Text.Trim(), txtPayrollId.Text.Trim());
            Response.Write("<script>alert('User added Successfully')</script>");
        }
        else
        {
            btnAdd.Text       = "Add Record";
            txtname.ReadOnly  = false;
            txtEmpId.ReadOnly = false;
            result            = objAccount.UpdateUser(txtEmpId.Text.Trim(), txtSystemName.Text.Trim(), txtMgrId.Text.Trim(), txtMgrEmailId.Text.Trim(), hfTeamId.Value, Session["UID"].ToString(), ddlGender.SelectedValue, Hourly, txtDOJ.Text, ddlType.SelectedValue, txtEmpNo.Text.Trim(), txtPayrollId.Text.Trim());
            Response.Write("<script>alert('User updated Successfully')</script>");
        }


        if (result == true)
        {
            result = objAccount.AddAccessLevel(txtEmpId.Text, Session["UID"].ToString());
            if (result == true)
            {
                lblError.ForeColor = Color.Blue;
                txtEmail.Text      = string.Empty;
                txtEmpId.Text      = string.Empty;
                txtname.Text       = string.Empty;
                txtSystemName.Text = string.Empty;
            }
            else
            {
                Response.Write("<script>alert('User added But failed to provide access please contact RTM Support')</script>");
                lblError.ForeColor = Color.Red;
            }
        }
        else
        {
            lblError.Text      = "User not added please try again or contact RTM Support";
            lblError.ForeColor = Color.Red;
        }

        resetFields();
        BindUsersGrid();
    }