views:

985

answers:

3

OK, I am amittedly new to LINQ and have spent the last week reading everything I could on it. I am just playing around, trying to follow some examples I have found (a PDF from Scott Gu on the topic, in fact) and I am at a complete loss. Can someone please tell me why, when I bind a GridView to the query below, using the code below, I get no data?? I can see the results while debugging, so I know they are coming back from the DB, they are just not apparently binding correctly. I read something saying you could not bind directly to the result,and that you have to use a BindingSource as an intermediate step?

Someone, please tell me what I am missing here.

protected void Page_Load(object sender, EventArgs e)
{
    SwapDBDataContext db = new SwapDBDataContext();

    var users = from u in db.aspnet_Users
                select new
                {
                   Name =  u.UserName,
                   ID = u.UserId
                };

    GridView1.DataSource = users;
    GridView1.DataBind();

}

I am just using an empty GridView. I had assumed that the binding would take care of setting up the columns to match the resulting columns from the query - was that a stupid beginners mistake?

    <asp:GridView ID="GridView1" runat="server">
    </asp:GridView>
A: 

Try doing

GridView1.DataSource = users.ToList();

or

GridView1.DataSource = users.ToArray();

Is possibly that the query isn't executing at all, because of deferred execution.

eKek0
+2  A: 

Cant understand why this shouldn't work. I whipped together a page using (almost) your code. It works perfectly for me.

protected void Page_Load(object sender, EventArgs e)
{
    BlodsockerkollenDataContext db = new BlodsockerkollenDataContext();
    var members = from m in db.Members
                  select new { Id = m.Id, Email = m.Email };

    GridView1.DataSource = members;
    GridView1.DataBind();
}

I don't agree with the suggested answers stating you should use ToList(). The GridView is capable of taking and traversing an IEnumerable, and IQueryable inherits IEnumerable.

And no, there should be no need to declare a BindingSource.

Maybe you have declared something in your GridView? Mine is just empty, pulled straight in from the toolbox:

<asp:GridView ID="GridView1" runat="server">
</asp:GridView>
Marpe
Thanks for taking the time to do this. In short, you're right. It should work. I don't know what happened. I had added the LINQ to SQL Classes item to an existing project and worked from there - there has to be something in that project that is preventing this code from running correctly. Don't know what, but I too created a new project from scratch and used the exact same code - and it worked fine! Now I have some work to do!
TheUXGuy
+1  A: 

You should not have to convert to a List or Array. Binding requires, at a minimum, an IEnumerable<T>, which is what your Users variable is. Anonymous types are simply pre-compile place holders for compiler generated concrete types, so you should also be able to bind to anonymous types.

Your GridView may not have the AutoGeneratedColumns property set, which is what is required to have the data source define what columns appear. Try enabling that, and see if your GridView displays your queries results.

jrista
It actually turned out that is WAS the AutoGenerateColumns. I am using some code from HeroCoder.com that we use on smaller projects from time to time, to avoid having to wireup all of the membership stuff - it provides a simple and solid admin tool for asp.net membership. Anyway, it turns out that they are disabling AutoGenerateColumns in their code. Adding it back to my gridview explicitely did the trick as suggested. Thanks!
TheUXGuy
Glad to be of service. I've run into that problem so many times myself, I thought someone should mention it. :D
jrista