-4

I created table N_Roles_Users in my database, I want to show it's value if username matches with existing login user.

I wrote this code below. But it is generating exception that check if object is Null.

// 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;
}

Can anyone guide me how to resolve this problem?

Zoya Sheikh
  • 871
  • 3
  • 11
  • 21
  • 1
    on which line you are getting the error – Microsoft DN Aug 21 '13 at 09:35
  • On which line is the exception generated? Please provide us with the stacktrace and exact exception type `System.InvalidOperationException` or maybe a `System.NullReferenceException` or what kind of exception? – SynerCoder Aug 21 '13 at 09:36
  • 1
    what is user_name ? it's any method or property ? – Ramesh Rajendran Aug 21 '13 at 09:36
  • on all lists. i removed list one by one to check that which list is creating this issue, but on each list this Exception generates. I executed queries to check it out that are values present in table or not. Values were present. I don't know what's wrong with it. – Zoya Sheikh Aug 21 '13 at 09:38
  • You make a new instance of `N_Roles_Users`, what is in the constructor? do you initialize everything? – Sayse Aug 21 '13 at 09:39
  • @RameshRajendran It is Column Name in N_Roles_Users – Zoya Sheikh Aug 21 '13 at 09:40
  • @ZoyaSheikh . I think that column name return null (empty) values , so you get this error. please look my answer , check that condition or check that column has any values there ? .. – Ramesh Rajendran Aug 21 '13 at 09:42
  • possible duplicate of [What is a NullReferenceException in .NET and how do I fix it?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-in-net-and-how-do-i-fix-it) – CodeCaster Aug 21 '13 at 09:42
  • @RameshRajendran yes you are right, Now how to solve this? – Zoya Sheikh Aug 21 '13 at 09:57
  • See my answer . Check that condition : http://stackoverflow.com/a/18353979/2218635 – Ramesh Rajendran Aug 21 '13 at 10:12

2 Answers2

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

set allroles.user_name = "some value" before

this line

temp = allroles.user_name.ToList();

Happy Coding :)

Abhishek Singh
  • 10,243
  • 22
  • 74
  • 108
0

Check this condition

// 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;
        }
Ramesh Rajendran
  • 37,412
  • 45
  • 153
  • 234