Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
Node is saved as draft in My Content >> Draft
  • Implementing Transactions in .NET

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 68
    Comment on it

    For performing transactions first you need to understand the use and things associated with it.

    Transaction is a way to make operations done in such a manner so that only relevant sequence or logical things should be done on the client and server side, we will call it commit in transaction terms and in any case if failure or exception occurs then the change that happened just before failure gets reverted. We also call it Rollback in transaction terms.

    Now we will see how actually transaction gets implemented in our code.

     

    I have made a WindowsCommunicationFoundation service for user forgot password:
     

     public EmployeeResponse ForgotPassword(string Email)
            {
                int rowsAffected = 0;
                SqlTransaction transaction = null;
                string randomPassword = PasswordGenerator.GenerateRandomString();
                empResponse = new EmployeeResponse();
                using (SqlConnection sqlConnection = new SqlConnection(ConnectionString))
                {
                    if (sqlConnection.State == ConnectionState.Closed)
                    {
                        sqlConnection.Open();
                    }
                   
                    transaction = sqlConnection.BeginTransaction("ForgotPasswordTransaction");
                    try
                    {
                        SqlCommand cmd = new SqlCommand("uspForgotPassword", sqlConnection,transaction);
                        cmd.CommandType = CommandType.StoredProcedure;
                        cmd.Parameters.Add("@Email", SqlDbType.VarChar, 100).Value = Email;
                        cmd.Parameters.Add("@Password", SqlDbType.VarChar, 100).Value = randomPassword;
                        cmd.Parameters.Add("@PasswordSalt", SqlDbType.VarChar, 200).Value = Guid.NewGuid().ToString();
                        rowsAffected = cmd.ExecuteNonQuery();
                        if (rowsAffected > 0)
                        {
                            bool isMailSent = Mail.SendMail(Email, randomPassword);
    
                            if (isMailSent)
                            {
                                transaction.Commit();
                                empResponse.Status = true;
                                empResponse.Message = "New password successfully sent in your mail.";
                            }
                            else {
                                transaction.Rollback();
                                empResponse.Status = false;
                                empResponse.Message = "Problem in sending password in your mail.";
                            }
                        }
                        else {
                            transaction.Rollback();
                            empResponse.Status = false;
                            empResponse.Message = "Please check your email address and then try again.";
                        }
    
                    }
                    catch (SqlException ex)
                    {
                        Logger.LogException(ex);
                        transaction.Rollback();
                        empResponse.Status = false;
                        empResponse.Message = ex.Message;
                    }
                }
                return empResponse;
            }

    In the above example i have made a transaction in which if in any case mail sent gets failed then rollback occurs and if everything goes fine then commit will be done by the code.

    To implement transaction first create the object of the transaction class

    Then associate this object with the SQL operation you are performing

     

    .net

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: