public void Link(UserEntity userEntity, int RoleId, string mail)
        {
            using (SqlConnection connection = DALHelper.CreateSqlDbConnection())
            {
                // Start a local transaction.
                SqlTransaction sqlTran = connection.BeginTransaction();

                // Enlist a command in the current transaction.

                try
                {
                    SqlCommand command = connection.CreateCommand();
                    command.Transaction = sqlTran;
                    new UserMapper().Insert(userEntity, command);

                    command = connection.CreateCommand();
                    command.Transaction = sqlTran;
                    new RoleMapper().InsertUserForRole(userEntity, new RoleEntity() { Id = RoleId }, command);

                    command = connection.CreateCommand();
                    command.Transaction = sqlTran;
                    new EmployeeMapper().UpdateJobInformation(userEntity.EmployeeId, mail, "", null, command);

                    // Commit the transaction.
                    sqlTran.Commit();
                }
                catch (Exception)
                {
                    // Handle the exception if the transaction fails to commit.

                    try
                    {
                        // Attempt to roll back the transaction.
                        sqlTran.Rollback();
                    }
                    catch (Exception)
                    {
                        // Throws an InvalidOperationException if the connection
                        // is closed or the transaction has already been rolled
                        // back on the server.
                    }
                }
            }
        }
Example #2
0
        protected void ProceedButton_Click(object sender, EventArgs e)
        {
            PrincipalContext ctx = new PrincipalContext(ContextType.Domain);
            UserPrincipal user = UserPrincipal.FindByIdentity(ctx, IdentityType.SamAccountName, userTextBox.Text);
            if (user != null)
            {
                int appId = Convert.ToInt32(ConfigurationManager.AppSettings["ApplicationId"]);
                int empId = Convert.ToInt32(Request.QueryString["EmployeeId"]);

                UserEntity userEntity = new UserEntity();
                userEntity.ApplicationId = appId;
                userEntity.EmployeeId = empId;
                userEntity.Username = userTextBox.Text;
                userEntity.Status = StatusEnum.Active;
                userEntity.Password = "";

                new LinkEmployeeToUserBO().Link(userEntity, Convert.ToInt32(RoleDropDownList.SelectedValue), user.UserPrincipalName);
                #warning set this to a good mail message
                string[] to = { EmailAddressTextBox.Text };
                SendMail.Send(to, "Hi", ("HR Test from system. You have been granted acess to the HR system." + userEntity.EmployeeId));

                Response.Redirect("List.aspx");
            }
        }