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...