-4

我在我的数据库中创建了表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;
}

谁能指导我如何解决这个问题?

4

2 回答 2

0
temp = allroles.user_name.ToList(); is the line of exception i guess.

allroles.user_name = "some value"之前设置

这条线

temp = allroles.user_name.ToList();

快乐编码:)

于 2013-08-21T09:39:19.597 回答
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;
        }
于 2013-08-21T09:40:22.907 回答