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 prima开发者_StackOverflow社区ry 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...
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.
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>
精彩评论