In my ASP.NET classic WebForms application, I have a class that returns a list of Role
objects that I use for mapping the roles of the users in my database.
I wrote this static class:
public static class RoleHelper
{
public static List<RoleValue> getRoles()
{
List<RoleValue> myroles = null;
string connectionString = ConfigurationManager.ConnectionStrings["SQLConnStr"].ConnectionString;
if (!string.IsNullOrWhiteSpace(connectionString))
{
using (SqlConnection dbconn = new SqlConnection(connectionString))
{
dbconn.Open();
SqlDataAdapter cmd = new SqlDataAdapter(" SELECT groupID,Name FROM Gruppi", dbconn);
cmd.SelectCommand.CommandType = CommandType.Text;
DataSet ds = new DataSet();
cmd.Fill(ds);
if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
{
myroles = new List<RoleValue>();
foreach (DataRow row in ds.Tables[0].Rows)
{
RoleValue myrole = new RoleValue();
myrole.roleID = (int)row["groupID"]; ;
myrole.roleName = (string)row["Name"]; ;
myroles.Add(myrole);
}
}
dbconn.Close();
}
}
return myroles;
}
}
At first I wrote:
List(RoleValue) myroles = null;
Is this wrong?
In the calling function, I check if (rolesList.Count > 0)
but I should check if(!rolesList is null)
but null isn't allowed for lists?