0

我在删除连接到 LinqDataSource 的 FormView 中的记录时遇到问题。LinqDataSource 使用的表允许几乎所有字段的 NULL 值。如果我在 FormView 中创建一条新记录并填写所有值,然后尝试删除它,它工作正常。但是,如果我将任何字段留空然后保存并尝试删除,则会引发以下异常:

未能在类型 PRIDE.Info___Employee_Health_History 上设置一个或多个属性。不是 Int16 的有效值。

表中的real列也是如此。如果我将其中一个留空,然后保存并尝试删除,我会得到不同类型的相同异常。

我最大的问题是,为什么它在删除时尝试设置 ANY 值?

这是我的桌子:

桌子

我的表单视图代码:

<asp:FormView ID="fvHealthHistory" runat="server" CssClass="full" 
    AllowPaging="True" DataSourceID="ldsHealthHistory" DataKeyNames="ID" 
    oniteminserting="fvHealthHistory_ItemInserting">
    <EmptyDataTemplate>
        <p>No records found.</p>
        <p>
            <asp:Button ID="btnNew" runat="server" Text="New Record" CommandName="New" />
        </p>
    </EmptyDataTemplate>
    <InsertItemTemplate>
        <table class="pad5">
            <tr>
                <td class="field-name">Date:</td>
                <td>
                    <asp:TextBox ID="txtDate" runat="server" Text='<%#Bind("Date", "{0:dd MMM yyyy}") %>' />
                    <asp:CalendarExtender ID="CalendarExtender1" runat="server" TargetControlID="txtDate" Format="dd MMM yyyy" />
                </td>
            </tr>
            <tr>
                <td class="field-name">Weight:</td>
                <td><asp:TextBox ID="txtWeight" runat="server" Text='<%#Bind("Weight") %>' /></td>
            </tr>
            <tr>
                <td class="field-name">Height:</td>
                <td><asp:TextBox ID="txtHeight" runat="server" Text='<%#Bind("Height") %>' /></td>
            </tr>
            <tr>
                <td class="field-name">Pulse:</td>
                <td><asp:TextBox ID="txtPulse" runat="server" Text='<%#Bind("Pulse") %>' /></td>
            </tr>
            <tr>
                <td class="field-name">Systolic:</td>
                <td><asp:TextBox ID="txtSystolic" runat="server" Text='<%#Bind("Systolic") %>' /></td>
            </tr>
            <tr>
                <td class="field-name">Diastolic:</td>
                <td><asp:TextBox ID="txtDiastolic" runat="server" Text='<%#Bind("Diastolic") %>' /></td>
            </tr>
        </table>

        <p>
            <asp:Button ID="btnInsert" runat="server" Text="Insert" CommandName="Insert" />
            <asp:Button ID="btnCancel" runat="server" Text="Cancel" CommandName="Cancel" />
        </p>
    </InsertItemTemplate>
    <EditItemTemplate>
        <table class="pad5">
            <tr>
                <td class="field-name">Date:</td>
                <td>
                    <asp:TextBox ID="txtDate" runat="server" Text='<%#Bind("Date", "{0:dd MMM yyyy}") %>' />
                    <asp:CalendarExtender ID="CalendarExtender1" runat="server" TargetControlID="txtDate" Format="dd MMM yyyy" />
                </td>
            </tr>
            <tr>
                <td class="field-name">Weight:</td>
                <td><asp:TextBox ID="txtWeight" runat="server" Text='<%#Bind("Weight") %>' /></td>
            </tr>
            <tr>
                <td class="field-name">Height:</td>
                <td><asp:TextBox ID="txtHeight" runat="server" Text='<%#Bind("Height") %>' /></td>
            </tr>
            <tr>
                <td class="field-name">Pulse:</td>
                <td><asp:TextBox ID="txtPulse" runat="server" Text='<%#Bind("Pulse") %>' /></td>
            </tr>
            <tr>
                <td class="field-name">Systolic:</td>
                <td><asp:TextBox ID="txtSystolic" runat="server" Text='<%#Bind("Systolic") %>' /></td>
            </tr>
            <tr>
                <td class="field-name">Diastolic:</td>
                <td><asp:TextBox ID="txtDiastolic" runat="server" Text='<%#Bind("Diastolic") %>' /></td>
            </tr>
        </table>

        <p>
            <asp:Button ID="btnUpdate" runat="server" Text="Update" CommandName="Update" />
            <asp:Button ID="btnCancel" runat="server" Text="Cancel" CommandName="Cancel" />
        </p>
    </EditItemTemplate>
    <ItemTemplate>
        <table class="pad5">
            <tr>
                <td class="field-name">Date:</td>
                <td>
                    <asp:Label ID="txtDate" runat="server" Text='<%#Bind("Date", "{0:dd MMM yyyy}") %>' />
                </td>
            </tr>
            <tr>
                <td class="field-name">Weight:</td>
                <td><asp:Label ID="txtWeight" runat="server" Text='<%#Bind("Weight") %>' /></td>
            </tr>
            <tr>
                <td class="field-name">Height:</td>
                <td><asp:Label ID="txtHeight" runat="server" Text='<%#Bind("Height") %>' /></td>
            </tr>
            <tr>
                <td class="field-name">Pulse:</td>
                <td><asp:Label ID="txtPulse" runat="server" Text='<%#Bind("Pulse") %>' /></td>
            </tr>
            <tr>
                <td class="field-name">Systolic:</td>
                <td><asp:Label ID="txtSystolic" runat="server" Text='<%#Bind("Systolic") %>' /></td>
            </tr>
            <tr>
                <td class="field-name">Diastolic:</td>
                <td><asp:Label ID="txtDiastolic" runat="server" Text='<%#Bind("Diastolic") %>' /></td>
            </tr>
        </table>

        <p>
            <asp:Button ID="btnNew" runat="server" Text="New" CommandName="New" />
            <asp:Button ID="btnEdit" runat="server" Text="Edit" CommandName="Edit" />
            <asp:Button ID="btnDelete" runat="server" Text="Delete" CommandName="Delete" OnClientClick="return confirm('Are you sure you want to delete this record?')" />
        </p>
    </ItemTemplate>
    <PagerSettings Mode="NextPreviousFirstLast" 
        FirstPageText="&amp;lt;&amp;lt; First" LastPageText="Last &amp;gt;&amp;gt;" 
        NextPageText="Next &amp;gt;" PreviousPageText="&amp;lt; Previous" />
    <PagerStyle CssClass="pager" />
</asp:FormView>

LinqDataSoure 的 ItemInserting 事件:

protected void fvHealthHistory_ItemInserting(object sender, FormViewInsertEventArgs e)
{
    e.Values["Worker_ID"] = Convert.ToInt32(Request.QueryString["id"]);
}

最后是我的 LinqDataSource:

<asp:LinqDataSource ID="ldsHealthHistory" runat="server" 
    ContextTypeName="PRIDE.PRIDEDataContext" EnableDelete="True" 
    EnableInsert="True" EnableUpdate="True" EntityTypeName="" OrderBy="Date desc" 
    TableName="Info___Employee_Health_Histories" Where="Worker_ID == @Worker_ID">
    <WhereParameters>
        <asp:QueryStringParameter DefaultValue="0" Name="Worker_ID" 
            QueryStringField="id" Type="Int32" />
    </WhereParameters>
    <InsertParameters>
        <asp:Parameter Name="Date" ConvertEmptyStringToNull="true" />
        <asp:Parameter Name="Weight" ConvertEmptyStringToNull="true" />
        <asp:Parameter Name="Height" ConvertEmptyStringToNull="true" />
        <asp:Parameter Name="Pulse" ConvertEmptyStringToNull="true" />
        <asp:Parameter Name="Systolic" ConvertEmptyStringToNull="true" />
        <asp:Parameter Name="Diastolic" ConvertEmptyStringToNull="true" />
    </InsertParameters>
    <UpdateParameters>
        <asp:Parameter Name="Date" ConvertEmptyStringToNull="true" />
        <asp:Parameter Name="Weight" ConvertEmptyStringToNull="true" />
        <asp:Parameter Name="Height" ConvertEmptyStringToNull="true" />
        <asp:Parameter Name="Pulse" ConvertEmptyStringToNull="true" />
        <asp:Parameter Name="Systolic" ConvertEmptyStringToNull="true" />
        <asp:Parameter Name="Diastolic" ConvertEmptyStringToNull="true" />
    </UpdateParameters>
</asp:LinqDataSource>

有谁知道为什么它只会让我删除所有可空字段都填写的记录?

4

0 回答 0