Solved

ASP.net 3.5 GridView Displaying DataTextField in ItemTemplate Column

Posted on 2010-08-16
7
709 Views
Last Modified: 2012-05-10
I give up.  I'm trying to create a GridView that displays a value in a related table in the Item Template field.  Here's what I have.

I created a test WebSite with the following tables

Customer = CustomerID, CustomerName, CustomerTypeCode
CustomerType = CustomerTypeCode, CustomerType

I defined the relationship between the tables in SQL Server CustomerTypeCode = CustomerTypeCode

Then I created the test web site.  I'm using Linq to Sql so I created a DBML file.  Dragged the tables to the DBML.   THe links appear as entered.  

THen I added an asp web form and dragged a Gridview to the form.    Next I set the Datasource as a Linq to SQL datasource based on the Customer table.  I changed the CustomerTypeCode column to a Template Field

THen I went to the source code and changed the eval("CustomerTypeCode") to eval("CustomerType") in the Item Template.  

When I display in Browser, I get the following message.


DataBinding: 'Customer' does not contain a property with the name 'CustomerType'.

What am I doing wrong?
0
Comment
Question by:deepPowdah
7 Comments
 
LVL 41

Accepted Solution

by:
guru_sami earned 168 total points
ID: 33450366
I don't think it will work that way. Since you are binding your GV to Customers table you won't get CustomerType.
You will have to write a separate join query that will return the Customer Columns + CustomerType in one resultset and then bind that to your GV.
0
 

Author Comment

by:deepPowdah
ID: 33450675
I have tried that but when you add the Customer Type table the recordset is no longer updateable.  I attempted to add the CUstomerType table using a view.  So when you try to edit a customer record, say change the customer name, you get an error message that it cannot be updated.  
0
 

Author Comment

by:deepPowdah
ID: 33450685
Using the Linq to SQL you must choose 1 item as your data source.  So it must be either the CustomerTable by itself, or the CustomerTable and CustomerType Table in a View.  How else would you do this/
0
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
LVL 16

Assisted Solution

by:Kamal Khaleefa
Kamal Khaleefa earned 166 total points
ID: 33452629
write a query like this
select
CustomerID, CustomerName, CustomerTypeCode,
CustomerTypeCode, CustomerType
from
Customer inner join
CustomerType on
Customer.CustomerTypeCode=CustomerType .CustomerTypeCode
0
 

Author Comment

by:deepPowdah
ID: 33453424
I used a left outer join to do this in a view.  I believe you can only use a table or view as a source for a Linq to SQL datasource.  The left outer join ensures that all customers are displayed even if they do not have a customer type.

SELECT     dbo.Customer.*, dbo.CustomerType_Lookup.CustomerType
FROM         dbo.Customer LEFT OUTER JOIN
                      dbo.CustomerType_Lookup ON dbo.Customer.CustomerTypeCode = dbo.CustomerType_Lookup.CustomerTypeCode

When I attempt to edit a record based on this view, I get the following error message.

Could not find a row that matches the given keys in the original values stored in ViewState.  Ensure that the 'keys' dictionary contains unique key values that correspond to a row returned from the previous Select operation
0
 
LVL 2

Assisted Solution

by:ahmadmaredia
ahmadmaredia earned 166 total points
ID: 33453655
Assuming you are using SQL Server 2005 or above, try the following query... This should generate a unique key for each row of your resultset for ASP.Net to work properly.  In the first line of the query, replace CustomerId with the column name of the PK of your Customer table.
SELECT     ROW_NUMBER() OVER (ORDER BY CustomerId ASC) AS ROWID,
dbo.Customer.*, dbo.CustomerType_Lookup.CustomerType
FROM         dbo.Customer LEFT OUTER JOIN
                      dbo.CustomerType_Lookup ON dbo.Customer.CustomerTypeCode = dbo.CustomerType_Lookup.CustomerTypeCode

Open in new window

0
 

Author Comment

by:deepPowdah
ID: 33470571
Thanks for all your input.  I have figured out how to do this using Linq to SQL.  When you create a data content page (dbml) by dragging tables from the Server View, the relationships you created in SQL Server are brought across too.  Then when you create the Linq DataSource for the GV, there is what appears to be an extra field for the name of each related table.   Then you create a template field for the lookup field, e.g. CustomerTypeCode. You can add a drop down list to enter and update the CustomerTypeCode.  However in the Item Template, you can display the CustomerType, not the CustomerType using the following code.

                    <ItemTemplate>
                        <asp:Label runat="server" Text='<%#Eval("CustomerType_Lookup.CustomerType") %>'></asp:Label>
                    </ItemTemplate>

Linq to SQL has its moments and this capability is one of them.  I wish someone would write a book about how to use Linq to SQL.
0

Featured Post

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I recently went through the process of creating a Calendar Control of events with the basis of using a database to keep track of the dates that are selectable, one requirement was to have the selected date pop-up in a simple lightbox.  At first this…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

821 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question