我在我的数据库中创建了表N_Roles_Users
,如果用户名与现有登录用户匹配,我想显示它的值。
我在下面写了这段代码。但它产生异常,检查对象是否为空。
// currentUser="UserA";
public List<string> GetUserRoles( string currentUser)
{
N_Roles_Users allroles = new N_Roles_Users(); //N_Roles_Users is database table name.
List<string> roleslist = new List<string>();
List<char> temp = new List<char>();
temp = allroles.user_name.ToList();
List<char> tempa = new List<char>();
tempa = allroles.role_name.ToList();
for (int i = 0; i < temp.Count; i++) // Loop through List with for
{
if (currentUser == temp[i].ToString())
{
roleslist.Add(tempa[i].ToString());
MessageBox.Show(tempa[i].ToString());
}
}
return roleslist;
}
有人可以指导我如何解决这个问题吗?
答案 0 :(得分:0)
temp = allroles.user_name.ToList(); is the line of exception i guess.
在
之前设置allroles.user_name = "some value"
这一行
temp = allroles.user_name.ToList();
快乐编码:)
答案 1 :(得分:0)
检查此情况
// currentUser =“UserA”;
public List<string> GetUserRoles( string currentUser)
{
N_Roles_Users allroles = new N_Roles_Users();
List<string> roleslist = new List<string>();
List<char> temp = new List<char>();
**if(allroles.user_name.ToList()!=null && allroles.user_name.ToList().Count!=0)
{
temp = allroles.user_name.ToList();
}**
List<char> tempa = new List<char>();
tempa = allroles.role_name.ToList();
for (int i = 0; i < temp.Count; i++) // Loop through List with for
{
if (currentUser == temp[i].ToString())
{
roleslist.Add(tempa[i].ToString());
MessageBox.Show(tempa[i].ToString());
}
}
return roleslist;
}