public ResponseApi <dynamic> GetPostWithDetails() { ResponseApi <dynamic> response = new ResponseApi <dynamic>(); List <UserPost> userPostList = new List <UserPost>(); string vComTxt = @"SELECT PostCode,P.UserCode,P.ActionDate,P.ActionType,Post,UserFullname FROM UserPost P JOIN UserInfo U ON P.usercode = U.UserCode WHERE P.ActionType <> 'DELETE' ORDER BY P.ActionDate DESC" ; SqlConnection connection = _habibDbContext.GetConn(); connection.Open(); SqlDataReader dr; SqlCommand objDbCommand = new SqlCommand(vComTxt, connection); dr = objDbCommand.ExecuteReader(); while (dr.Read()) { UserPost objApplicationInfo = new UserPost(); objApplicationInfo.PostCode = dr["PostCode"].ToString(); objApplicationInfo.UserCode = dr["UserCode"].ToString(); objApplicationInfo.ActionDate = dr.GetDateTime(dr.GetOrdinal("ActionDate")); objApplicationInfo.ActionType = dr["ActionType"].ToString(); objApplicationInfo.Post = dr["Post"].ToString(); objApplicationInfo.UserFullName = dr["UserFullname"].ToString(); objApplicationInfo.CommentsList = GetCommentByPostCode(objApplicationInfo.PostCode, connection); userPostList.Add(objApplicationInfo); } dr.Close(); response.Status = "OK"; response.Message = "Found"; response.Result = userPostList; return(response); }
public ResponseApi <dynamic> CreateUser(UserInfo objUserInfo) { ResponseApi <dynamic> response = new ResponseApi <dynamic>(); int vResult = 0; int vResult1 = 0; int vResult2 = 0; int vResult3 = 0; bool vResult4 = false; //string connectionstring = GetDefaultConnectionString(); string vOut = string.Empty;; StringBuilder vComText = new StringBuilder(); string vComText1 = string.Empty; string existinguser = string.Empty; Hashtable htExistingRoles = new Hashtable(); Hashtable htNewUserRoleList = new Hashtable(); SqlConnection connection = _habibDbContext.GetConn(); connection.Open(); existinguser = GetUserId(objUserInfo.UserName); if (String.IsNullOrEmpty(existinguser)) { MembershipProvider MembershipProvider = new MembershipProvider(); string passwordSaltedText = MembershipProvider.GeneratePasswordSaltingText(); string vPass = objUserInfo.Password; string saltedPassword = MembershipProvider.SaltText(vPass, passwordSaltedText); string saltedPasswordAnswer = MembershipProvider.SaltText(objUserInfo.PasswordAnswer, passwordSaltedText); string sSqlUser = @"INSERT INTO [dbo].[User] (UserId,UserName, UserCode,ActionDate,ActionType) VALUES (@UserId,@UserName, @UserCode,@ActionDate,@ActionType)"; SqlCommand sqluser = new SqlCommand(sSqlUser, connection); Guid userId = Guid.NewGuid(); sqluser.Parameters.AddWithValue("UserId", userId); sqluser.Parameters.AddWithValue("UserName", objUserInfo.UserName); sqluser.Parameters.AddWithValue("UserCode", Guid.NewGuid()); sqluser.Parameters.AddWithValue("ActionDate", objUserInfo.ActionDate); sqluser.Parameters.AddWithValue("ActionType", "Insert"); string sSqlMembership = @"INSERT INTO Membership (UserId,IsLockedOut,IsFirstLogin, LastLoginDate,LastPasswordChangeDate,FailedPassAtmptCount, LastLockoutDate,FailedPassAnsAtmptCount,PasswordSalt,Email, PasswordQuestion,PasswordAnswer, UserCode,ActionDate,ActionType) VALUES (@UserId,@IsLockedOut,@IsFirstLogin, @LastLoginDate,@LastPasswordChangeDate,@FailedPassAtmptCount, @LastLockoutDate,@FailedPassAnsAtmptCount,@PasswordSalt,@Email, @PasswordQuestion,@PasswordAnswer, @UserCode,@ActionDate,@ActionType) "; SqlCommand sqlmembership = new SqlCommand(sSqlMembership, connection); sqlmembership.Parameters.AddWithValue("UserId", userId.ToString()); sqlmembership.Parameters.AddWithValue("IsLockedOut", 0); sqlmembership.Parameters.AddWithValue("IsFirstLogin", 1); sqlmembership.Parameters.AddWithValue("LastLoginDate", new DateTime(1800, 1, 1)); sqlmembership.Parameters.AddWithValue("LastPasswordChangeDate", new DateTime(1800, 1, 1)); sqlmembership.Parameters.AddWithValue("FailedPassAtmptCount", 0); sqlmembership.Parameters.AddWithValue("LastLockoutDate", new DateTime(1800, 1, 1)); sqlmembership.Parameters.AddWithValue("FailedPassAnsAtmptCount", 0); sqlmembership.Parameters.AddWithValue("PasswordSalt", MembershipProvider.EncodeToBase64String(passwordSaltedText)); sqlmembership.Parameters.AddWithValue("Email", objUserInfo.Email); sqlmembership.Parameters.AddWithValue("PasswordQuestion", "abc"); sqlmembership.Parameters.AddWithValue("PasswordAnswer", MembershipProvider.EncodeText(saltedPasswordAnswer)); sqlmembership.Parameters.AddWithValue("UserCode", Guid.NewGuid()); sqlmembership.Parameters.AddWithValue("ActionDate", DateTime.Now); sqlmembership.Parameters.AddWithValue("ActionType", "Insert"); string sSqlUserPassword = @"INSERT INTO UserPassword (UserId,Password, UserCode,ActionDate,ActionType) VALUES (@UserId,@Password, @UserCode,@ActionDate,@ActionType)"; SqlCommand sqlpassword = new SqlCommand(sSqlUserPassword, connection); sqlpassword.Parameters.AddWithValue("UserId", userId.ToString()); sqlpassword.Parameters.AddWithValue("Password", MembershipProvider.EncodeText(saltedPassword)); sqlpassword.Parameters.AddWithValue("UserCode", Guid.NewGuid()); sqlpassword.Parameters.AddWithValue("ActionDate", DateTime.Now); sqlpassword.Parameters.AddWithValue("ActionType", "Insert"); vComText.Append("INSERT INTO UserInfo (UserId,UserCode,ActionDate,ActionType,UserFullName)"); vComText.Append(" VALUES"); vComText.Append("(@UserId,@UserCode,@ActionDate,@ActionType,@UserFullName)"); SqlCommand sqlCommand = new SqlCommand(vComText.ToString(), connection); sqlCommand.Parameters.AddWithValue("@UserId", userId); sqlCommand.Parameters.AddWithValue("UserCode", userId.ToString()); sqlCommand.Parameters.AddWithValue("ActionDate", objUserInfo.ActionDate); sqlCommand.Parameters.AddWithValue("ActionType", "Insert"); sqlCommand.Parameters.AddWithValue("UserFullName", objUserInfo.UserFullName); using (SqlTransaction transaction = connection.BeginTransaction()) { sqluser.Transaction = transaction; sqlCommand.Transaction = transaction; sqlmembership.Transaction = transaction; sqlpassword.Transaction = transaction; try { vResult1 = sqluser.ExecuteNonQuery(); if (vResult1 > 0) { vResult = sqlCommand.ExecuteNonQuery(); if (vResult > 0) { vResult2 = sqlmembership.ExecuteNonQuery(); if (vResult2 > 0) { vResult3 = sqlpassword.ExecuteNonQuery(); if (vResult3 > 0) { vResult4 = AddUsersToRoles(objUserInfo, userId, connection, transaction); //vResult4 = sqlrole.ExecuteNonQuery(); if (vResult4 == true) { transaction.Commit(); response.Status = "OK"; response.Message = "Registration Successfully"; response.Result = null; } } } } } } catch (Exception ex) { transaction.Rollback(); response.Status = "FAILED"; response.Message = ex.Message; response.Result = null; } finally { connection.Dispose(); connection.Close(); } } } else { response.Status = "FAILED"; response.Message = "This user Id already exists!"; response.Result = null; } return(response); }