Asp.NET return empy List C#
In my asp.net classic WebForms Application I have a class that returns a
List of Roles object that I use for mapping the roles of the users in my
db.
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 it 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 ? do you
advice me. thanks
No comments:
Post a Comment