?
Solved

ASP.net 3.5 GridView Displaying DataTextField in ItemTemplate Column

Posted on 2010-08-16
7
Medium Priority
?
725 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 672 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 16

Assisted Solution

by:Kamal Khaleefa
Kamal Khaleefa earned 664 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 664 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

I have developed many web applications with asp & asp.net and to add and use a dropdownlist was always a very simple task, but with the new asp.net, setting the value is a bit tricky and its not similar to the old traditional method. So in this a…
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…
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
Integration Management Part 2
Suggested Courses

840 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