• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 951
  • Last Modified:

my formview does not have a value for its datakey

Hi

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">

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

...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
0
andieje
Asked:
andieje
  • 2
  • 2
1 Solution
 
curlypinheadCommented:
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.
0
 
andiejeAuthor Commented:
hi, the employerid is in the underlying data :(
0
 
curlypinheadCommented:
does it work if you display the employerid in the formview?
0
 
andiejeAuthor Commented:
Hi

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

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now