Click here to Skip to main content
15,886,422 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
i have three tables

table 1:
users

userid, username, password


tabel 2:
roles

userid, groupid


table3:
group
groupid,name

i want to show a username from table1 and name from table 3 in grid view.
i wrote a query as.

C#
SqlDataAdapter da = new SqlDataAdapter("select users.username,groups.name from users,groups inner join users on roles.userid=users.userid inner join groups on roles.groupid=users.groupid", MAconn);


but i am getting an error

System.NullReferenceException: Object reference not set to an instance of an object. at Default2.bind()
Posted
Updated 9-Sep-11 17:31pm
v2
Comments
[no name] 10-Sep-11 0:31am    
what do u mean by defaul2.bind()
could u add your complete code to knowing excatly your problem

SQL
select U.username as username,G.name as Name from users U,roles R,group G where U.userid=G.userid and R.groupid=G.group


and in bound filed of gridview take
 
Share this answer
 
Comments
[no name] 10-Sep-11 6:27am    
you missed a join for roles table here.
That's because Default2 is null
 
Share this answer
 
Comments
codegeekalpha 8-Sep-11 10:21am    
mean???
Simon Bang Terkildsen 8-Sep-11 10:32am    
It means that Default2 is not set to an instance of an object, just like the exceptions say. I don't know how else to say it if you don't understand what it means when Default2 == null. If you don't understand it you'll have to hold your breath for one of the experts, who frequent this Q&A, to come to your aid.
codegeekalpha 8-Sep-11 10:36am    
how to get rid of this problem??
codegeekalpha 8-Sep-11 10:38am    
i did filling data in grid many time. but did n't see such error before..
codegeekalpha 8-Sep-11 11:22am    
can u pls help me
Try this SQL

SQL
select users.username,groups.name,roles.userid
from users,groups,roles
inner join users on roles.userid=users.userid
inner join groups on roles.groupid=users.groupid
 
Share this answer
 
Comments
[no name] 10-Sep-11 6:26am    
is this ok?
Hi,

Here is your answer.

SQL
SELECT U.Username,G.[Name] AS [GroupName]
FROM Users U
    INNER JOIN Roles R ON U.Userid = R.UserId
    INNER JOIN [Group] G ON R.GroupId = G.GroupId


for table3 i gave name Group which you didn't mention in your question.
And the reason for this error is your query will return an error and if you are not using using proper try catch block and assign data source to your server control Default2 which was bind to null object.

and also try your sql query to sql server's enterprise manager.
 
Share this answer
 
v2
Is that query correctly framed

SQL
SqlDataAdapter da = new SqlDataAdapter("select users.username,groups.name from users,groups inner join users on roles.userid=users.userid inner join groups on roles.groupid=group.groupid", MAconn);

you have placed roles instead of group i guess
 
Share this answer
 
v3
Comments
[no name] 10-Sep-11 6:26am    
is this Ok?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900