public CUser GetAUser(string ConnectStringHere, int UserIDToGetHere)
{
SqlConnection sqlConn = null;
SqlCommand sqlComm = null;
SqlDataReader sqlDR = null;
CUser MyUser = null;
StringBuilder MySQL = new StringBuilder();
try
{
sqlConn = new SqlConnection(ConnectStringHere);
MySQL.Append(" SELECT FirstName, LastName,");
MySQL.Append(" UserName, Role,");
MySQL.Append(" PhoneNumber, LastLoginDate,");
MySQL.Append(" BadLoginCount");
MySQL.Append(" FROM dbo.tbl_User");
MySQL.Append(" WHERE IsActive = @ActiveFlag");
MySQL.Append(" AND UserID = @UserID");
sqlComm = new SqlCommand();
sqlComm.CommandType = CommandType.Text;
sqlComm.CommandText = MySQL.ToString();
sqlComm.Parameters.Add("@ActiveFlag", SqlDbType.Bit).Value = true;
sqlComm.Parameters.Add("@UserID", SqlDbType.Int).Value = UserIDToGetHere;
sqlComm.Connection = sqlConn;
sqlConn.Open();
sqlDR = sqlComm.ExecuteReader();
if (sqlDR.Read())
{
MyUser = new CUser();
MyUser.UserId = UserIDToGetHere;
MyUser.FirstName = sqlDR.GetString(0).Trim();
MyUser.LastName = sqlDR.GetString(1).Trim();
MyUser.UserName = sqlDR.GetString(2).Trim();
MyUser.Role = sqlDR.GetString(3).Trim();
MyUser.PhoneNumber = sqlDR.GetString(4).Trim();
MyUser.LastLoginDate = sqlDR.GetDateTime(5);
MyUser.BadLoginCount = sqlDR.GetInt(6);
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message + Environment.NewLine + ex.StackTrace);
}
finally
{
if (sqlDR != null)
{
if (!sqlDR.IsClosed)
{
sqlDR.Close();
}
}
if (sqlConn != null)
{
if (sqlConn.State == ConnectionState.Open)
{
sqlConn.Close();
}
sqlConn.Dispose();
}
return MyUser;
}
}