Solved

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

Posted on 2014-03-06
7
256 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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

707 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now