David Kethel
David Kethel

Reputation: 2560

No Id in ObjectDataSource Update method Parrameter

I have a .Net 3.5 application with a simple form view and an object data source.

<asp:ObjectDataSource ID="odsDevice" runat="server" SelectMethod="GetDeviceByDeviceNumber" EnableViewState="True"
     UpdateMethod="updateDevice" DataObjectTypeName="GPSO.Repository.Device" TypeName="GPSOnline.ATOMWebService">
<SelectParameters>
    <asp:SessionParameter DbType="Guid" Name="deviceid" SessionField="deviceid" />
</SelectParameters>

and here is the form view

<asp:FormView ID="fvDevices" runat="server" DataSourceID="odsDevice">
<EditItemTemplate>
    <table cellpadding="0" cellspacing="0" class="AdminContent">
        <tr>
            <td class="Content">
                <h1>
                    Device Details</h1>
                <hr />
                <table class="AdminDetails" cellpadding="0" cellspacing="0">
                    <tr>
                        <th>
                            Atom Serial #
                        </th>
                        <td class="control">
                            <asp:TextBox ID="txtDeviceNumber" runat="server" Text='<%#Bind("DeviceNumber")%>' />
                        </td>
                    </tr>
                    <tr>
                        <th>
                            &nbsp;
                        </th>
                        <td class="control">
                            <asp:RequiredFieldValidator ID="rfvDeviceNumber" runat="server" ToolTip="Must specify Device Number"
                                Display="Dynamic" ErrorMessage="Device Number is Required" EnableClientScript="false"
                                ControlToValidate="txtDeviceNumber"></asp:RequiredFieldValidator>
                            <asp:RegularExpressionValidator ID="revDeviceAddId" runat="server" ToolTip="Device ID must be an Integer."
                                ValidationExpression="[0-9]{1,6}" Display="Dynamic" ErrorMessage="Device ID must be an Number, less then 1000000."
                                EnableClientScript="false" ControlToValidate="txtDeviceNumber"></asp:RegularExpressionValidator>
                            <asp:CustomValidator ID="cvDeviceAddId" runat="server" ErrorMessage="Device ID already Exists."
                                Display="Dynamic" ToolTip="Device ID already Exists." ControlToValidate="txtDeviceNumber"
                                OnServerValidate="CheckDeviceDoesNotExist"></asp:CustomValidator>
                        </td>
                    </tr>
                    <tr>
                        <th>
                            &nbsp;
                        </th>
                        <td class="control">
                            <asp:Label runat="server" ForeColor="Red" ID="lblFeedback" Text="" />
                        </td>
                    </tr>
                    <tr>
                        <th>
                            &nbsp;
                        </th>
                        <td class="control">
                            &nbsp;
                        </td>
                    </tr>
                    <tr>
                        <th>
                            &nbsp;
                        </th>
                        <td class="control">
                            <asp:Button runat="server" ID="btnUpdate" Text="Update" CausesValidation="true" CommandName="Update"
                                Width="160px" />
                        </td>
                    </tr>
                </table>
            </td>
        </tr>
    </table>
</EditItemTemplate>

and here is the code for the object data source methods.

    [OperationContract]
    [WebGet]
    public  void updateDevice(Device device)
    {
        DeviceRep.updateDevice(device);

    }

    [OperationContract]
    [WebGet]
    public Device GetDeviceByDeviceNumber(Guid deviceid)
    {
        return DeviceRep.GetDeviceByDeviceNumber(deviceid);}

Now the business object contains a far few fields that I am not interested in for the update, I only really want to update the Device Number.

However the problem I have is that the device object that I get as a parameter into the " public void updateDevice(Device device)" method does not have an ID for the object that is being updated. Which make it impossible to know which record in the repository to update.

Is there a way to persist the ID of the device so that its part of the object when it comes to be updated?

Upvotes: 1

Views: 452

Answers (1)

KV Prajapati
KV Prajapati

Reputation: 94643

Not sure but you have to set DataKeyNames property of FormView with,

 <asp:FormView ID="fvDevices" runat="server" 
   DataSourceID="odsDevice" DataKeyNames="deviceid" ..

Upvotes: 2

Related Questions