我有一个连接到SQL数据源的asp.net Formview。当我创建/编辑/删除记录时,列数据被删除。我确信这是一些简单的错误编码,因为我对SQL / asp.net所知的一切都在过去几周内用Google搜索过。
这是SQLDataSource代码。
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:hulc01ConnectionString %>"
SelectCommand="SELECT * FROM [Common]"
InsertCommand="INSERT INTO [Common] ([Project_Name],
[Business_Category], [Project_Description], [Operations_Owner])
VALUES (@ProjectName, @BusinessCategory, @ProjectDescription,
@OperationsOwner)"
DeleteCommand="DELETE FROM [Common] WHERE [ProjectKey] = @ProjectKey"
UpdateCommand="UPDATE [Common]
SET
[Project_Name] = @ProjectName,
[Business_Category] = @BusinessCategory,
[Project_Description] = @ProjectDescription,
[Operations_Owner] = @OperationsOwner
WHERE ProjectKey=@ProjectKey" >
<DeleteParameters>
<asp:Parameter Name="ProjectKey" Type="Int32" />
</DeleteParameters>
<UpdateParameters>
<asp:Parameter Name="ProjectKey" Type="Int32" />
<asp:Parameter Name="ProjectName" Type="String" />
<asp:Parameter Name="BusinessCategory" Type="String" />
<asp:Parameter Name="ProjectDescription" Type="String" />
<asp:Parameter Name="OperationsOwner" Type="String" />
</UpdateParameters>
<InsertParameters>
<asp:Parameter Name="ProjectName" Type="String" />
<asp:Parameter Name="BusinessCategory" Type="String" />
<asp:Parameter Name="ProjectDescription" Type="String" />
<asp:Parameter Name="OperationsOwner" Type="String" />
</InsertParameters>
</asp:SqlDataSource>
这是formview代码
<asp:FormView ID="FormView1" runat="server" AllowPaging="True"
DataKeyNames="ProjectKey"
DataSourceID="SqlDataSource1"
Width="249px">
<EditItemTemplate>
Project Name:
<asp:TextBox runat="server"
ID="ProjectName"
Text='<%# Bind("Project_Name") %>' />
<br />
Business Category:
<asp:TextBox runat="server"
ID="BusinessCategory"
Text='<%# Bind("Business_Category") %>' />
<br />
Project Description:
<asp:TextBox runat="server"
ID="ProjectDescription"
Text='<%# Bind("Project_Description") %>' />
<br />
Operations Owner:
<asp:TextBox runat="server"
ID="OwnerTextBox"
Text='<%# Bind("Operations_Owner") %>' />
<br />
<asp:LinkButton runat="server"
ID="UpdateButton"
CausesValidation="True"
CommandName="Update"
Text="Update" />
<asp:LinkButton runat="server"
ID="UpdateCancelButton"
CausesValidation="False"
CommandName="Cancel"
Text="Cancel" />
</EditItemTemplate>
<InsertItemTemplate>
Project Name:
<asp:TextBox runat="server"
ID="ProjectName"
Text='<%# Bind("Project_Name") %>' />
<br />
Business Category:
<asp:TextBox runat="server"
ID="BusinessCategory"
Text='<%# Bind("Business_Category") %>' />
<br />
Project Description:
<asp:TextBox runat="server"
ID="Description"
Text='<%# Bind("Project_Description") %>' />
<br />
Operations Owner:
<asp:TextBox runat="server"
ID="TitleTextBox"
Text='<%# Bind("Operations_Owner") %>' />
<br />
<asp:LinkButton runat="server"
ID="InsertButton"
CausesValidation="True"
CommandName="Insert"
Text="Insert" />
<asp:LinkButton runat="server"
ID="InsertCancelButton"
CausesValidation="False"
CommandName="Cancel"
Text="Cancel" />
</InsertItemTemplate>
<ItemTemplate>
Project Name:
<asp:Label runat="server"
ID="ProjectNameLabel"
Text='<%# Bind("Project_Name") %>' />
<br />
Business Category:
<asp:Label runat="server"
ID="BusinessCategoryLabel"
Text='<%# Bind("Business_Category") %>' />
<br />
Project Description:
<asp:Label runat="server"
ID="ProjectDescriptionLabel"
Text='<%# Bind("Project_Description") %>' />
<br />
Operations Owner:
<asp:Label runat="server"
ID="OwnerLabel"
Text='<%# Bind("Operations_Owner") %>' />
<br />
<asp:LinkButton runat="server"
ID="EditButton"
CausesValidation="False"
CommandName="Edit"
Text="Edit" />
<asp:LinkButton runat="server"
ID="DeleteButton"
CausesValidation="False"
CommandName="Delete" Text="Delete" />
<asp:LinkButton runat="server"
ID="NewButton"
CausesValidation="False"
CommandName="New"
Text="New" />
</ItemTemplate>
</asp:FormView>
谢谢!
答案 0 :(得分:5)
您的DeleteCommand和UpdateCommand未使用与FormView
的DataKeyNames属性中声明的密钥相同的密钥。它们应该是这样的:
DeleteCommand="DELETE FROM [Common] WHERE [Project_Key] = @ProjectKey"
UpdateCommand="UPDATE [Common] " +
"SET [Project_Name] = @ProjectName, [Business_Category] = @BusinessCategory, [Project_Description] = @ProjectDescription, [Operations_Owner] = @OperationsOwner" +
"WHERE Project_Key=@ProjectKey>"
DataKeyNames属性用于控制使用FormView
完成的自动UPDATE和DELETE,因此这应该是表中的主键,并且它应该在所有三个位置匹配(UpdateCommand,DeleteCommand和DataKeyNames) )。来自MSDN:
使用DataKeyNames属性指定以逗号分隔的字段名称列表,这些字段名称表示数据源的主键。
(在该页面的代码示例中,您可以看到UpdateCommand的WHERE子句中使用的参数与FormView
的DataKeyNames中指定的值相同)
请注意,在您显示的代码中,您的UpdateCommand根本不使用where子句。这意味着它将使用相同的值集更新表中的每条记录。仅供参考=)
编辑:当您使用这样的FormView
时,您实际上并不需要按照它们的方式设置UpdateParameters(因为您正在使用双向数据绑定)编辑/插入模板中的控件。试试SQLDataSource
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:hulc01ConnectionString %>"
SelectCommand="SELECT * FROM [Common]"
InsertCommand="INSERT INTO [Common] ([Project_Name], [Business_Category], [Project_Description], [Operations_Owner]) VALUES (@Project_Name, @Business_Category, @Project_Description, @Operations_Owner)"
DeleteCommand="DELETE FROM [Common] WHERE [ProjectKey] = @ProjectKey"
UpdateCommand="UPDATE [Common] SET [Project_Name] = @Project_Name, [Business_Category] = @Business_Category, [Project_Description] = @Project_Description, [Operations_Owner] = @Operations_Owner WHERE ProjectKey=@ProjectKey" >
</asp:SqlDataSource>
当我不使用FormView
时,我通常只使用UpdateParameters,DeleteParameters等,或者我需要根据FormView
之外的控件过滤它们。如果这不适合你,请告诉我。