Hello all,
I have a SQL Table "Roles" in DB. I want to find out if user have one or more Roles (Admin/User) and then do something with a value if user have 2 Roles at time.
Problem: I cant get all Values from Database, I getting just a first one.
can I do it with foreach loop? I dont have it here but i'm looking for solution.
like:
cmd = new SqlCommand();
cmd.Connection = connection;
cmd.CommandType = System.Data.CommandType.Text;
cmd.CommandText = @"SELECT Role "
+ " FROM UsersRole "
+ " WHERE UserName = @UserName ";
cmd.Parameters.Add(new SqlParameter("@UserName", username));
reader = cmd.ExecuteReader();
if (reader.HasRows)
{
while (reader.Read())
{
if (reader["Role"] != DBNull.Value)
{
Role = Convert.ToString(reader["Role"]);
if(Role == UserRole.Admin.ToString())
{
IsAdmin = true;
}
if (Role == UserRole.User.ToString())
{
IsUser = true;
}
}
else
{
Role = "";
}
}
}
reader.Close();
IS it possible to access Table in DB with C# code or I can do it different?
Thanks and take care, Ragims