1

I'm trying to use LINQ to bind the data in my GridView.

In my Service.cs file I have a method for fetching the entries for a specific logged in user:

public List<tidsregistrering> ShowTidregistreringer()
{
    var CurrentMedarbejder = FindUser(HttpContext.Current.Session["email"].ToString());

    var tempList = (from t in kdc.tidsregistrerings
                    join p in kdc.projekts on t.projektid equals p.id
                    join k in kdc.kundes on t.kundeid equals k.id
                    join o in kdc.øvriges on t.øvrigeid equals o.id
                    where t.medarbejderid == CurrentMedarbejder.id
                    select t).ToList();    

    return tempList;
}

Where the kdc is my DataContext. I have tried joining the different tables together, but no data is shown in my GridView. If I leave out the joins, I get data... In my other tables I have a column called navn (name). I want that name printet in my GridView instead of the foreign key reference...

My GridView:

<asp:GridView ID="GridView1" runat="server">
    <Columns>
        <asp:BoundField DataField="tidsforbrug" HeaderText="Tidsforbrug" />
        <asp:BoundField DataField="dato" HeaderText="Dato" />
        <asp:BoundField DataField="<%# Bind("projekt.id") %>" HeaderText="Projekt" />
        <asp:BoundField DataField="<%# Bind("kunde.id") %>" HeaderText="Kunde" />
        <asp:BoundField DataField="<%# Bind("øvrige.id") %>" HeaderText="Øvrigt" />
        <asp:BoundField DataField="done" HeaderText="Done" />
    </Columns>
</asp:GridView

My binding happens at Page_load:

GridView1.DataSource = service.ShowTidregistreringer();
GridView1.DataBind();

How do I do this?

Edit: And for good measure this is my list, as of now... And I want those numbers in projektid, kundeid and øvrigeid to be joined with my foreign key tables.

*Edit2:** For even better measure this is how my database tables are created:

CREATE TABLE chef(
    id int identity primary key,
    email varchar(100) unique not null,
    password char(100) not null,
    navn varchar(100) not null
);

CREATE TABLE medarbejder(
    id int identity primary key,
    email varchar(100) unique not null,
    password char(100) not null,
    navn varchar(100) not null,
    chefid int foreign key references chef(id)
);

CREATE TABLE projekt(
    id int identity primary key,
    navn varchar(50) not null,
    beskrivelse varchar(255) not null
);

CREATE TABLE kunde(
    id int identity primary key,
    navn varchar(50) not null,
    beskrivelse varchar(255) not null
);

CREATE TABLE øvrige(
    id int identity primary key,
    navn varchar(50) not null,
);

CREATE TABLE tidsregistrering(
    id int identity primary key,
    tidsforbrug float,
    dato datetime,
    projektid int foreign key references projekt(id),
    kundeid int foreign key references kunde(id),
    øvrigeid int foreign key references øvrige(id),
    medarbejderid int foreign key references  medarbejder(id) not null,
    done bit
);

Edit3: I have remade my LINQ-query to this:

List<tidsregistrering> tempList = (from t in kdc.tidsregistrerings
                                   join p in kdc.projekts on t.projektid equals p.id into p_t
                                   join k in kdc.kundes on t.kundeid equals k.id into k_t
                                   join o in kdc.øvriges on t.øvrigeid equals o.id into o_t
                                   from k in k_t.DefaultIfEmpty()
                                   from p in p_t.DefaultIfEmpty()
                                   from o in o_t.DefaultIfEmpty()
                                   where t.medarbejderid == CurrentMedarbejder.id
                                   select t).ToList();

But it still doesn't select what's joined...

https://i.stack.imgur.com/eSFQX.png

KristianB
  • 1,403
  • 3
  • 25
  • 46

2 Answers2

1

Well, there must be something wrong with your data in the database. Or rather, due to all the joins you do not get any data.

Update from the explanation of your db-structure I can see the problem is that you are joining on three tables with inner joins and some of the fields that are you are joining on are null. That way you will never get any results. You need to go to outer joins. Linq does not have an outer join statement but it can be done. have a look here: http://smehrozalam.wordpress.com/2009/06/10/c-left-outer-joins-with-linq/

And btw, if you have proper foreign keys in your database, you can simply get:

tempList = (from t in kdc.tidsregistrerings
where t.medarbejderid == CurrentMedarbejder.id 
select t).ToList();  

Update 2 And you can bind like this:

<asp:TemplateField HeaderText="navn" SortExpression="projekts.navn">
                        <ItemTemplate>
                            <asp:literal ID="Label2" runat="server" Text='<%# Eval("projekts.navn") %>'></asp:literal>
                        </ItemTemplate>
                    </asp:TemplateField>
Pleun
  • 8,856
  • 2
  • 30
  • 50
  • assuming you do not dispose your datacontext in the service.cs -> otherwise to to this binding you need to add dataloadoptions to make sure the project data is eagerly loaded – Pleun May 18 '11 at 09:46
  • I tried this method too, and I get "Code blocks not supported when I enter the `` within the GridView's tag. – KristianB May 18 '11 at 10:27
  • I've read the example link you referred to, and edited my question. – KristianB May 18 '11 at 10:39
  • The binding probably needs to be in a templatefield, I have updateed my answer. Also you may have to handle the null values. – Pleun May 18 '11 at 12:47
1

Run this query in SQL Server Management studio

select t.* from tidsregistrerings t 
inner join projekts p on t.projektid = p.id
inner join kundes k on t.kundeid    = k.id 
inner join øvriges o on t.øvrigeid = o.id 
where t.medarbejderid = [whatever CurrentMedarbejder id is]

Then comment out the joins one at a time, until you get your results returned. This will show you which table is preventing your data from being returned.

markpsmith
  • 4,860
  • 2
  • 33
  • 62
  • When I run this: `select t.* from tidsregistrering t inner join projekt p on t.projektid = p.id inner join kunde k on t.kundeid = k.id inner join øvrige o on t.øvrigeid = o.id where t.medarbejderid = 1` I get an empty table – KristianB May 18 '11 at 09:53
  • ok, so now run:select t.* from tidsregistrering t inner join projekt p on t.projektid = p.id inner join kunde k on t.kundeid = k.id where t.medarbejderid = 1 – markpsmith May 18 '11 at 09:54
  • in the example data you posted kundeid is empty => this will never return any data in your inner join... – Pleun May 18 '11 at 09:56
  • Okay, to explain my database setup: if the type is "projekt", the "kunde" and "øvrige" fields will be null... And vice versa if the type is "kunde" the other two fields are null... – KristianB May 18 '11 at 09:57
  • in that case you need to do outer joins and not inner joins... An inner join will only return data if there is data on both sides of the equasion. – Pleun May 18 '11 at 09:59
  • So it looks as though you need to have left outer joins in your linq: [see here for examples](http://stackoverflow.com/questions/700523/linq-to-sql-left-outer-join) – markpsmith May 18 '11 at 10:08
  • `select t.tidsforbrug, t.dato, p.navn, k.navn, o.navn from tidsregistrering t left outer join projekt p on t.projektid = p.id left outer join kunde k on t.kundeid = k.id left outer join øvrige o on t.øvrigeid = o.id where t.medarbejderid = 1` Made me able to get data! So that's solved... Now I just need to translate that into "LINQ". – KristianB May 18 '11 at 10:25