Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

ASP.net 3.5 GridView Displaying DataTextField in ItemTemplate Column

Posted on 2010-08-16
7
710 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
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

839 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