Solved

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

Posted on 2014-03-06
7
261 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
[X]
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
  • 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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

739 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