Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/30.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
ASP.NET Formview未正确更新SQL数据库_Asp.net_Sql_Formview - Fatal编程技术网

ASP.NET Formview未正确更新SQL数据库

ASP.NET Formview未正确更新SQL数据库,asp.net,sql,formview,Asp.net,Sql,Formview,我有一个连接到SQL数据源的asp.net Formview。当我创建/编辑/删除记录时,列数据被擦除。我确信这是一些简单的错误编码,因为我所知道的关于SQL/asp.net的所有信息在过去几周都被谷歌搜索过 下面是SQLDataSource代码 <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:hulc01ConnectionStri

我有一个连接到SQL数据源的asp.net Formview。当我创建/编辑/删除记录时,列数据被擦除。我确信这是一些简单的错误编码,因为我所知道的关于SQL/asp.net的所有信息在过去几周都被谷歌搜索过

下面是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" />
            &nbsp;<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" />
            &nbsp;<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" />
            &nbsp;<asp:LinkButton runat="server" 
                ID="DeleteButton" 
                CausesValidation="False" 
                CommandName="Delete" Text="Delete" />
            &nbsp;<asp:LinkButton runat="server" 
                ID="NewButton" 
                CausesValidation="False" 
                CommandName="New" 
                Text="New" />

        </ItemTemplate>
    </asp:FormView>  

项目名称:

业务类别:
项目说明:
业务负责人:
项目名称:
业务类别:
项目说明:
业务负责人:
项目名称:
业务类别:
项目说明:
业务负责人:

谢谢

您的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
完成的自动更新和删除,因此这应该是表中的主键,并且应该在所有三个位置(UpdateCommand、DeleteCommand和DataKeyNames)匹配。发件人:

使用DataKeyNames属性指定以逗号分隔的字段名列表,这些字段名表示数据源的主键

(在该页面的代码示例中,您可以看到UpdateCommand的WHERE子句中使用的参数与
FormView
的DataKeyName中指定的值相同)

值得注意的是,在您展示的代码中,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
之外的控件对其进行过滤。如果这对您不起作用,请告诉我。

我遇到了另一个问题。我将在上面的代码中更改数据源。delete命令按预期工作,但insert命令插入空白记录,而不管我在文本框中输入什么,edit命令不会更新更改,我想象它会像新命令一样使用空白数据进行更新。“有什么想法吗?”克里斯,我明白了。这可能与参数的设置方式有关。编辑马上就来。谢谢你的投票和接受,顺便说一句,这就是事实。我假设参数名类似于ID,并且不会/不应该与列名相同。现在一切都很好,非常感谢。