my formview does not have a value for its datakey


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
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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.
andiejeAuthor Commented:
hi, the employerid is in the underlying data :(
does it work if you display the employerid in the formview?
andiejeAuthor Commented:

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.
Closed, 500 points refunded.
The Experts Exchange
Community Support Moderator of all Ages

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.