Solved

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

Posted on 2014-03-06
7
257 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Why Does This Page Not Show Up? 4 24
Error in page 3 46
Host asp.net pages 5 25
Export import database 4 43
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …

910 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

21 Experts available now in Live!

Get 1:1 Help Now