my formview does not have a value for its datakey

Posted on 2006-03-29
Last Modified: 2007-12-19

I have a detailsview where i set the datakey to be the primary key of the record it is displaying.

I am trying to access the value of the datakey as follows:

dim id as integer = Employer.DataKey("EmployerID")   where Employer is the name of the FVand EmpID is the PK

When i do this i get an error saying the datakey does not have a value. I do not understand why. The only thing i can think of is that when the formview is created, there is nothing in the formview template that references the employerID.

The FV looks like this
    <asp:FormView ID="Employer" runat="server" DataKeyNames="EmployerID" DataSourceID="ObjectDataSource1" DefaultMode="Edit">

        <td colspan="3" height="30">
            <strong>Company Details</strong></td>
<asp:TextBox ID="CompanyName" runat="server" Text='<%# Bind("CompanyName") %>'></asp:TextBox>

...and so on

but there is nothing in the template that binds the employerID because I don't want this field displayed.

Do you know what is causing the FV to lose its datakey? How can i get it back?

Thanks, andrea
Question by:andieje
    LVL 2

    Expert Comment

    The employerID needs to be in the object that you are binding to the formview control (dataset, sqldatareader, etc).  It doesn't need to be in the form itself.  If it is not in the binding object, you might get that error.

    Author Comment

    hi, the employerid is in the underlying data :(
    LVL 2

    Expert Comment

    does it work if you display the employerid in the formview?

    Author Comment


    I have investigated the problem further. My fornview is bound manually in that I don't use a datasource oebject, i set the datasource myself from a dataset. It would seem that in this case the datakey is not set. The same applies to a gridview too. I created 2 equivalent pages, one binding manually and one binding with an object data source. The first had no datakeys, the second did. I did this for both a formview and a gridview.

    Accepted Solution

    Closed, 500 points refunded.
    The Experts Exchange
    Community Support Moderator of all Ages

    Featured Post

    Enabling OSINT in Activity Based Intelligence

    Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

    Join & Write a Comment

    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…
    Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
    In this sixth video of the Xpdf series, we discuss and demonstrate the PDFtoPNG utility, which converts a multi-page PDF file to separate color, grayscale, or monochrome PNG files, creating one PNG file for each page in the PDF. It does this via a c…
    Internet Business Fax to Email Made Easy - With eFax Corporate (, you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

    734 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

    23 Experts available now in Live!

    Get 1:1 Help Now