LINQ DataSource and Foreign Keys
-
Hi all, i've probably just missed something amazingly simple but.... I've been watching the tutorials based on Scott Gu's blog and the tutorial on binding a GridView to a LINQ datasource (video 4 i think) would really save me loads of development time while i'm creating prototypes for a project, if i could get it to work. An example table structure is as follows Customer ID (int) CustomerName (varchar) UserID (int) User ID (int) UserName (varchar) Now, as you can see, the userID field in customer maps to the user table quite nicely. I create a LINQ to Sql dbml file and drag the two tables into the page and i get a relationship created for me. Next up i create a LinqDataSource pointing at the Customer table with the * box ticked to select all columns. Then i bind a GridView control to the LinqDataSource and run the app, everything looks OK but i get the UserID int where i want the UserName. In the code behind file i can create a new Customer object (cust) and use the following to get at the username
cust.User1.UserName
but if i bind an itemtemplate field on the datagrid to this withEval(User1.UserName)
it doesn't work, i get the error message "User1.UserName was not found on the selected data source." I have tried User.UserName as well but to no avail, anyone have an answer for this? I'm running a web app in C# if it makes any difference :-D -
Hi all, i've probably just missed something amazingly simple but.... I've been watching the tutorials based on Scott Gu's blog and the tutorial on binding a GridView to a LINQ datasource (video 4 i think) would really save me loads of development time while i'm creating prototypes for a project, if i could get it to work. An example table structure is as follows Customer ID (int) CustomerName (varchar) UserID (int) User ID (int) UserName (varchar) Now, as you can see, the userID field in customer maps to the user table quite nicely. I create a LINQ to Sql dbml file and drag the two tables into the page and i get a relationship created for me. Next up i create a LinqDataSource pointing at the Customer table with the * box ticked to select all columns. Then i bind a GridView control to the LinqDataSource and run the app, everything looks OK but i get the UserID int where i want the UserName. In the code behind file i can create a new Customer object (cust) and use the following to get at the username
cust.User1.UserName
but if i bind an itemtemplate field on the datagrid to this withEval(User1.UserName)
it doesn't work, i get the error message "User1.UserName was not found on the selected data source." I have tried User.UserName as well but to no avail, anyone have an answer for this? I'm running a web app in C# if it makes any difference :-DWell i figured it out and the problem was with my Eval statement, nothing to do with LINQDataSources or the gridview. Eval("User1.UserName") is how it should be done instead of Eval(User1.UserName). I thought the days of missing ; or " off your code were long gone but obviously not :laugh: