C# Can';当值为空时,不能删除formview中的记录
我在删除连接到LinqDataSource的FormView中的记录时遇到问题。LinqDataSource使用的表允许几乎所有字段都为空值。如果我在FormView中创建一个新记录并填写所有值,然后尝试删除它,效果很好。但是,如果我将任何字段留空,然后保存并尝试删除,则会引发以下异常: 未能设置类型的一个或多个属性 PRIDE.Info\员工\健康\历史记录。不是的有效值 Int16 表中的C# Can';当值为空时,不能删除formview中的记录,c#,asp.net,linq,formview,linqdatasource,C#,Asp.net,Linq,Formview,Linqdatasource,我在删除连接到LinqDataSource的FormView中的记录时遇到问题。LinqDataSource使用的表允许几乎所有字段都为空值。如果我在FormView中创建一个新记录并填写所有值,然后尝试删除它,效果很好。但是,如果我将任何字段留空,然后保存并尝试删除,则会引发以下异常: 未能设置类型的一个或多个属性 PRIDE.Info\员工\健康\历史记录。不是的有效值 Int16 表中的real列也是如此。如果我将其中一个保留为空,然后保存并尝试删除,则会得到具有不同类型的相同异常 我最大
real
列也是如此。如果我将其中一个保留为空,然后保存并尝试删除,则会得到具有不同类型的相同异常
我最大的问题是,为什么它在删除时试图设置任何值
这是我的桌子:
我的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="&lt;&lt; First" LastPageText="Last &gt;&gt;"
NextPageText="Next &gt;" PreviousPageText="&lt; Previous" />
<PagerStyle CssClass="pager" />
</asp:FormView>
最后是我的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>
有人知道为什么只有在填写了所有可为空的字段后才允许我删除记录吗?您在请求中获取的内容。查询字符串[“id”][worker\u id]或[id]我在查询字符串中获取
id
。LinqDataSource正在使用query string变量进行正确筛选,它只是不希望在FormView本身中存在NULL
值时删除。
<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>