My select new statement get values from two different tables but it does not data bind in the grid view please help.

var search = (from u in db.Users
`              join s in db.Sites on u.SiteID equals s.SiteID`
              where u.FirstName.Contains(tbSearch.Text.Trim())
              select new 
              {
                  u.FirstName,
                  u.LastName,
                  u.Username,
                  u.Password,
                  s.SiteName,
                  u.Active
              });

if (search != null)
{

    rgUsers.DataSource = search;
    rgUsers.DataBind();

Result : Displays not values in the gridiew.

Note : When I change my select new statement to "select u" it returns all the values except the s.sitename which is from another table.

No it just doesnt display the values in the grid view.

Probably means the join is failing. Check that your User table contains SiteIDs that are contained in your Sites able where the firstname is equal to what you're searching for.

Also, when you execute this look at what LINQ to SQL is generated using SQL Profiler. You can execute this against the database and see if you're getting any results.

If you aren't, then there is probably a problem with your data. For comparison, running the following query is equivalent;

select
    u.FirstName,
    u.LastName,
    u.Username,
    u.Password,
    s.SiteName,
    u.Active
from [Users] u
inner join [Sites] s on u.SiteID = s.SiteID
where u.FirstName = 'Name to search for';

If you run this, you should get some results, if you don't, then it is definitely a data issue :)

Its not a problem with the join, The data gets passed to search.
The problem is that the values arent getting assigned to the columns.

I solved it, i copy pasted some of the code from another page, i just chaged a few things and it was solved, thanks for your help.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.