4

我正在尝试制作一个具有乐观并发的简单 ListView。它使用 VS 自动生成的 Delete、Insert、Update 语句(除了 1 处更改为插入:见代码)。插入和编辑工作正常并提交到数据库。尝试删除时,出现此错误:

您已指定您的删除命令比较 SqlDataSource 'MySourceHere' 上的所有值,但传入值的字典为空。传入有效字典以删除或将模式更改为 OverwriteChanges。

我尝试将“CompareAllValues”更改为“OverwriteChanges”以推迟并发工作,但这会“中断”更新和删除命令,这似乎只是用旧值刷新 ListView。我能找到解决此问题的唯一线程涉及更改为“OverwriteChanges”,或者是没有声明“DataKeyNames”的人。相关标记如下(我希望)。

有什么想法吗?

<asp:SqlDataSource ID="ManageUsersSource" runat="server" 
    ConflictDetection="CompareAllValues" 
    ConnectionString="<%$ ConnectionStrings:ConnectionString %>" 
        DeleteCommand="DELETE FROM [DUSER] WHERE [userid] = ? AND (([username] = ?) OR ([username] IS NULL AND ? IS NULL)) AND (([userpass] = ?) OR ([userpass] IS NULL AND ? IS NULL))" 
    InsertCommand="INSERT INTO [DUSER] ([username], [userpass]) VALUES (?, ?)" 
    OldValuesParameterFormatString="original_{0}" 
    ProviderName="<%$ ConnectionStrings:ConnectionString.ProviderName %>" 
    SelectCommand="SELECT * FROM [DUSER] ORDER BY [userid] ASC" 
    UpdateCommand="UPDATE [DUSER] SET [username] = ?, [userpass] = ? WHERE [userid] = ? AND (([username] = ?) OR ([username] IS NULL AND ? IS NULL)) AND (([userpass] = ?) OR ([userpass] IS NULL AND ? IS NULL))">
    <DeleteParameters>
        <asp:Parameter Name="original_userid" Type="Int16" />
        <asp:Parameter Name="original_username" Type="String" />
        <asp:Parameter Name="original_username" Type="String" />
        <asp:Parameter Name="original_userpass" Type="String" />
        <asp:Parameter Name="original_userpass" Type="String" />
    </DeleteParameters>
    <InsertParameters>
    <%--This is removed since [userid] is an automatically generated index.--%>
        <%--<asp:Parameter Name="userid" Type="Int16" />--%>
        <asp:Parameter Name="username" Type="String" />
        <asp:Parameter Name="userpass" Type="String" />
    </InsertParameters>
    <UpdateParameters>
        <asp:Parameter Name="username" Type="String" />
        <asp:Parameter Name="userpass" Type="String" />
        <asp:Parameter Name="original_userid" Type="Int16" />
        <asp:Parameter Name="original_username" Type="String" />
        <asp:Parameter Name="original_username" Type="String" />
        <asp:Parameter Name="original_userpass" Type="String" />
        <asp:Parameter Name="original_userpass" Type="String" />
    </UpdateParameters>
</asp:SqlDataSource>
<asp:ListView ID="ListView1" runat="server" DataKeyNames="userid" 
    DataSourceID="ManageUsersSource" InsertItemPosition="LastItem">
    <AlternatingItemTemplate>
        <tr style="background-color:#FFF8DC;">
            <td>
                <asp:Button ID="DeleteButton" runat="server" CommandName="Delete" 
                    Text="Delete" />
                <asp:Button ID="EditButton" runat="server" CommandName="Edit" Text="Edit" />
            </td>
            <td>
                <asp:Label ID="useridLabel" runat="server" Text='<%# Eval("userid") %>' />
            </td>
            <td>
                <asp:Label ID="usernameLabel" runat="server" Text='<%# Eval("username") %>' />
            </td>
            <td>
                <asp:Label ID="userpassLabel" runat="server" Text='<%# Eval("userpass") %>' />
            </td>
        </tr>
    </AlternatingItemTemplate>
    <EditItemTemplate>
        <tr style="background-color:#008A8C;color: #FFFFFF;">
            <td>
                <asp:Button ID="UpdateButton" runat="server" CommandName="Update" 
                    Text="Update" />
                <asp:Button ID="CancelButton" runat="server" CommandName="Cancel" 
                    Text="Cancel" />
            </td>
            <td>
                <asp:Label ID="useridLabel1" runat="server" Text='<%# Eval("userid") %>' />
            </td>
            <td>
                <asp:TextBox ID="usernameTextBox" runat="server" 
                    Text='<%# Bind("username") %>' />
            </td>
            <td>
                <asp:TextBox ID="userpassTextBox" runat="server" 
                    Text='<%# Bind("userpass") %>' />
            </td>
        </tr>
    </EditItemTemplate>
    <EmptyDataTemplate>
        <table runat="server" 
            style="background-color: #FFFFFF;border-collapse: collapse;border-color: #999999;border-style:none;border-width:1px;">
            <tr>
                <td>
                    No data was returned.</td>
            </tr>
        </table>
    </EmptyDataTemplate>
    <InsertItemTemplate>
        <tr style="">
            <td>
                <asp:Button ID="InsertButton" runat="server" CommandName="Insert" 
                    Text="Insert" />
                <asp:Button ID="CancelButton" runat="server" CommandName="Cancel" 
                    Text="Clear" />
            </td>
            <td>
                &nbsp;</td>
            <td>
                <asp:TextBox ID="usernameTextBox" runat="server" 
                    Text='<%# Bind("username") %>' />
            </td>
            <td>
                <asp:TextBox ID="userpassTextBox" runat="server" 
                    Text='<%# Bind("userpass") %>' />
            </td>
        </tr>
    </InsertItemTemplate>
    <ItemTemplate>
        <tr style="background-color:#DCDCDC;color: #000000;">
            <td>
                <asp:Button ID="DeleteButton" runat="server" CommandName="Delete" 
                    Text="Delete" />
                <asp:Button ID="EditButton" runat="server" CommandName="Edit" Text="Edit" />
            </td>
            <td>
                <asp:Label ID="useridLabel" runat="server" Text='<%# Eval("userid") %>' />
            </td>
            <td>
                <asp:Label ID="usernameLabel" runat="server" Text='<%# Eval("username") %>' />
            </td>
            <td>
                <asp:Label ID="userpassLabel" runat="server" Text='<%# Eval("userpass") %>' />
            </td>
        </tr>
    </ItemTemplate>
    <LayoutTemplate>
        <table runat="server">
            <tr runat="server">
                <td runat="server">
                    <table ID="itemPlaceholderContainer" runat="server" border="1" 
                        style="background-color: #FFFFFF;border-collapse: collapse;border-color: #999999;border-style:none;border-width:1px;font-family: Verdana, Arial, Helvetica, sans-serif;">
                        <tr runat="server" style="background-color:#DCDCDC;color: #000000;">
                            <th runat="server">
                            </th>
                            <th runat="server">
                                userid</th>
                            <th runat="server">
                                username</th>
                            <th runat="server">
                                userpass</th>
                        </tr>
                        <tr ID="itemPlaceholder" runat="server">
                        </tr>
                    </table>
                </td>
            </tr>
            <tr runat="server">
                <td runat="server" 
                    style="text-align: center;background-color: #CCCCCC;font-family: Verdana, Arial, Helvetica, sans-serif;color: #000000;">
                    <asp:DataPager ID="DataPager1" runat="server">
                        <Fields>
                            <asp:NextPreviousPagerField ButtonType="Button" ShowFirstPageButton="True" 
                                ShowLastPageButton="True" />
                        </Fields>
                    </asp:DataPager>
                </td>
            </tr>
        </table>
    </LayoutTemplate>
    <SelectedItemTemplate>
        <tr style="background-color:#008A8C;font-weight: bold;color: #FFFFFF;">
            <td>
                <asp:Button ID="DeleteButton" runat="server" CommandName="Delete" 
                    Text="Delete" />
                <asp:Button ID="EditButton" runat="server" CommandName="Edit" Text="Edit" />
            </td>
            <td>
                <asp:Label ID="useridLabel" runat="server" Text='<%# Eval("userid") %>' />
            </td>
            <td>
                <asp:Label ID="usernameLabel" runat="server" Text='<%# Eval("username") %>' />
            </td>
            <td>
                <asp:Label ID="userpassLabel" runat="server" Text='<%# Eval("userpass") %>' />
            </td>
        </tr>
    </SelectedItemTemplate>
</asp:ListView>

4

3 回答 3

4

在我的 ItemTemplate/AlternatingItemTemplate(就像编辑模板)中更改非主键字段以使用“ Bind ”而不是“ Eval ”显然需要正确传递旧值。

这是在没有完全理解的情况下使用自动生成的标记的情况(我仍然没有,真的。)

于 2012-06-06T15:56:22.587 回答
0

您可以尝试简化更新和删除命令。由于 userid(您的 DataKeyName)是自动生成的,因此是唯一的,因此您不需要额外的参数。

因此,您的 UpdateCommand 将是

"UPDATE [DUSER] SET [username] = ?, [userpass] = ? WHERE [userid] = ?"

具有以下参数:-

<UpdateParameters>
    <asp:Parameter Name="userid" Type="Int16" />
    <asp:Parameter Name="username" Type="String" />
    <asp:Parameter Name="userpass" Type="String" />
</UpdateParameters>

您的 DeleteCommand 将是

"DELETE FROM [DUSER] WHERE [userid] = ?"

具有以下参数:-

<DeleteParameters>
    <asp:Parameter Name="userid" Type="Int16" />
</DeleteParameters>
于 2012-06-05T20:26:37.643 回答
0

抱歉,我忘了更改 ?s 并且您想要乐观并发。

"UPDATE [DUSER] SET [username] = @username, [userpass] = @userpass WHERE [userid] = @userid And [username] = @original_username And [userpass] = @original_userpass"

"DELETE FROM [DUSER] WHERE [userid] = @userid And [username] = @original_username And [userpass] = @original_userpass"

当然,您需要检查该行是否已更新/删除。

您的 InserCommand 也应该是:-

"INSERT INTO [DUSER] ([username], [userpass]) VALUES (@username, @userpass)"

假设用户名和用户密码设置为不允许空值,在这种情况下它们应该是空值,那么如果任一参数为空,命令将失败。

为避免这种情况,<asp:RequiredFieldValidator>请向每个 usernameTextBox 和 userpassTextBox 控件添加一个以提供客户端验证。

您还应该更改 TextBox 控件的 ID 以匹配参数名称。

如果您不得不处理空值,那么您将需要生成的查询的其他部分开始使用空比较,将 ?s 替换为适当的参数。

于 2012-06-05T22:06:05.743 回答