Solved

How can I determine which table record I'm viewing in a detailsview?

Posted on 2014-03-06
7
260 Views
Last Modified: 2014-03-07
Hi,

I'm using entity framework to connect to database tables.

I have bound a table to a detailsview.  How can I write code to find the record within the table that is currently displayed on the webpage?

Thanks

W
0
Comment
Question by:WhyDidntItWork
  • 3
  • 3
7 Comments
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 39912114
If you have bound to a record with a key field, then you can use the DataKey.Value property of the DetailsView control to get the key of the current record:
int currentID = int.Parse(DetailsView1.DataKey.Value.ToString());

Open in new window

0
 
LVL 16

Expert Comment

by:SriVaddadi
ID: 39912137
DataKey returns the primary key and DataItem property returns the record itself.

Also, please note that DataKey class has different properties returning objects to get the primary keys.
0
 

Author Comment

by:WhyDidntItWork
ID: 39913502
Hi Experts,

When the page is loaded, I use a linq query to bind the database table to the detailsview.

Based on the above comments, for test purposes, I added a label on the webpage and tried to write to it the datakey value.  I didn't get the expected result.  Instead, I got an "Object reference not set to an instance of an object" error.

Here is the code:
protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
            HCLEntities4 Context = new HCLEntities4();
                                             
            var query = from q in Context.Pes
                        where q.Pending != null
                        where q.Approver == null
                        select q;

            DetailsView.DataSource = query.ToList();
            DetailsView.DataBind();

            Label1.Text = DetailsView.DataKey.Value.ToString();    
            }
        }

Not sure why I'm getting the error.  Suggestions?

W
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

Expert Comment

by:SriVaddadi
ID: 39913570
Try

Label1.Text = (DetailsView.DataItem as Pes).RowIdProperty.ToString();
0
 

Author Comment

by:WhyDidntItWork
ID: 39913623
I used SQL Server Management Studio to create the database table first.  One field in the table has been assigned as the primary key.

The table was then replicated as a model in Visual Studio.  The primary key field in the database model has a "key" icon beside it.
0
 
LVL 16

Accepted Solution

by:
SriVaddadi earned 500 total points
ID: 39913630
Try using DataItem Property or use DateItemIndex

query.ToList()[Detailsview.DataItemIndex] will give you the row

DataItem would also give the row currently displaying.

DetailsView control will create a DataKey object only when DataKeyNames property is set. Not sure if you set in the aspx page.
0
 

Author Closing Comment

by:WhyDidntItWork
ID: 39913775
Thanks for helping me get from Point A to B.
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

828 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