private bool IsValidUserData(Users user, AuthTable authUser, out string message) { bool isValidUser = true; string errorMessage = string.Empty; if (string.IsNullOrEmpty(user.Username.Trim())) { isValidUser = false; errorMessage += "Please enter Username." + "<br />"; } else if (user.Username.Length > 50) { isValidUser = false; errorMessage += "Username can be upto 50 characters." + "<br />"; } if (string.IsNullOrEmpty(user.UserFullName.Trim())) { isValidUser = false; errorMessage += "Please enter valid Full Name." + "<br />"; } else if (user.UserFullName.Length > 100) { isValidUser = false; errorMessage += "Full name can be upto 100 characters." + "<br />"; } DateTime date; bool isValidDate = DateTime.TryParse(user.UserDOB.ToString(), out date); if (!isValidDate) { isValidUser = false; errorMessage += "Please enter valid Date of Birth." + "<br />"; } else if (date.CompareTo(DateTime.Now) >= 0) { isValidUser = false; errorMessage += "Please enter valid Date of Birth." + "<br />"; } string emailRegex = @"^(?("")("".+?""@)|(([0-9a-zA-Z]((\.(?!\.))|[-!#\$%&'\*\+/=\?\^`\{\}\|~\w])*)(?<=[0-9a-zA-Z])@))(?(\[)(\[(\d{1,3}\.){3}\d{1,3}\])|(([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,6}))$"; if(!Regex.IsMatch(user.UserEmailId,emailRegex)) { isValidUser = false; errorMessage += "Please enter valid email-id ([email protected])" + "<br />"; } if (string.IsNullOrEmpty(authUser.Password)) { isValidUser = false; errorMessage += "Please enter valid Password." + "<br />"; } message = errorMessage; return isValidUser; }
public Users InsertUsers(Users user, AuthTable authUser, string connString) { string errorMessage = string.Empty; if (IsValidUserData(user, authUser, out errorMessage)) { SqlParameter pUsername = new SqlParameter("@Username", SqlDbType.NVarChar, 50); SqlParameter pUserFullName = new SqlParameter("@UserFullName", SqlDbType.NVarChar, 100); SqlParameter pUserEmailId = new SqlParameter("@UserEmailId", SqlDbType.NVarChar, 100); SqlParameter pUserGender = new SqlParameter("@UserGender", SqlDbType.Bit); SqlParameter pUserDOB = new SqlParameter("@UserDOB", SqlDbType.SmallDateTime); SqlParameter pUserAreaOfInterest = new SqlParameter("@UserAreaOfInterest", SqlDbType.NVarChar, 100); SqlParameter pUsernameAuth = new SqlParameter("@Username", SqlDbType.NVarChar, 50); SqlParameter pPassword = new SqlParameter("@Password", SqlDbType.NVarChar, 50); SqlParameter pRole = new SqlParameter("@Role", SqlDbType.Int); pUsername.Value = user.Username; pUserFullName.Value = user.UserFullName; pUserEmailId.Value = user.UserEmailId; pUserGender.Value = user.UserGender; pUserDOB.Value = user.UserDOB; pUserAreaOfInterest.Value = user.UserAreaOfInterest; pUsernameAuth.Value = user.Username; pPassword.Value = authUser.Password; pRole.Value = authUser.Role; SqlCommand cmdSQL = new SqlCommand(); cmdSQL.CommandType = CommandType.StoredProcedure; cmdSQL.CommandText = "usp_InsertAuthTable"; cmdSQL.Parameters.Add(pUsernameAuth); cmdSQL.Parameters.Add(pPassword); cmdSQL.Parameters.Add(pRole); bool isUsernamePresent = IsUsernamePresent(authUser, connString); DataLayer dataLayerObj = new DataLayer(); if (!isUsernamePresent) { cmdSQL = dataLayerObj.Insert(cmdSQL, connString); } else { errorMessage = "Username already Present." + "<br/>" + errorMessage; } cmdSQL = new SqlCommand(); cmdSQL.CommandType = CommandType.StoredProcedure; cmdSQL.CommandText = "usp_InsertUser"; cmdSQL.Parameters.Add(pUsername); cmdSQL.Parameters.Add(pUserFullName); cmdSQL.Parameters.Add(pUserEmailId); cmdSQL.Parameters.Add(pUserGender); cmdSQL.Parameters.Add(pUserDOB); cmdSQL.Parameters.Add(pUserAreaOfInterest); if (!isUsernamePresent) { cmdSQL = dataLayerObj.Insert(cmdSQL, connString); } else { throw new Exception(errorMessage); ; } } else { throw new Exception(errorMessage); } return user; }