当值为NULL时,无法在formview中删除记录

时间:2013-04-16 21:51:22

标签: c# asp.net linq formview linqdatasource

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

  

无法在类型上设置一个或多个属性   PRIDE.Info ___ Employee_Health_History。不是有效值   INT16。

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

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

这是我的表:

Table

我的FormView代码:

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

有人知道为什么只有在填写所有可空字段时才会删除记录吗?

0 个答案:

没有答案