asp:SqlDataSource的UPDATE命令属性无法识别参数值

时间:2014-06-12 16:58:45

标签: c# asp.net sqldatasource

asp:ListView对象尝试调用内置于数据源的更新命令时,会发生错误。特别是错误说明:

  

无法将值NULL插入列'FirstName',表中   'SqlCatalog.dbo.Members';列不允许空值。更新失败。   该声明已被终止

我在ItemUpdateEventHandler中使用以下代码来验证没有将NULL插入到设置为NOT NULL的字段中。

protected void Members_VerifyChanges(Object sender, ListViewItemUpdateEventArgs eArgs)
{
    if(eArgs.NewValues["ID"] == null || eArgs.NewValues["FirstName"] == null || eArgs.NewValues["LastName"] == null)
    {
        eArgs.Cancel = true;
    }
}

以下是为数据源指定的UPDATE命令:

UPDATE Members SET [ID]=original_ID, [Approved]=@App, [FirstName]=@First, [MiddleInitial]=@Mid, [LastName]=@Last WHERE ID=@original_ID

由于事件处理程序函数没有取消UPDATE命令,我可以确定NewValues字典有除NULL以外的东西可以插入到FirstName字段中。

以下是我一直在使用的具体测试用例。

Old Values
 ________________________________________________________________________________
|____ID____|____Approved____|____First Name____|____Initial____|____Last Name____|
|__1001____|______[x]_______|_______Test_______|_______A_______|______Test_______|

New Values
 ________________________________________________________________________________
|____ID____|____Approved____|____First Name____|____Initial____|____Last Name____|
|__1001____|______[x]_______|______Bruce_______|_______A_______|______Wayne______|

对我而言似乎没有将NewValues字典添加到参数中,但我不知道如何检查它。我已经考虑过在事件处理程序中手动创建和执行更新命令,但这是一个我不想做的黑客攻击。

这是SqlDataSource的asp标记:

<asp:SqlDataSource ID="WaterFacultySqlDataSrc" runat="server" ConflictDetection="CompareAllValues" 
ConnectionString="<%$ ConnectionStrings:ConnectionString %>" OldValuesParameterFormatString="original_{0}"
SelectCommand="SELECT [ID], [Approved], [FirstName], [MiddleInitial], [LastName] FROM Members ORDER BY [ID] DESC"
InsertCommand="INSERT INTO [Members] (ID, Approved, FirstName, MiddleInitial, LastName VALUES (@ID, @App, @First, @Mid, @Last)"
UpdateCommand="UPDATE [Members] SET [ID]=@original_ID, [Approved]=@App, [FirstName]=@First, [MiddleInitial]=@Mid, [LastName]=@Last WHERE ID=@original_ID"
DeleteCommand="DELETE FROM [Members] WHERE ID=@original_ID">
<UpdateParameters>
    <asp:Parameter Name="ID" Type="Int32" />
    <asp:Parameter Name="original_ID" Type="Int32" />
    <asp:Parameter Name="App" Type="Boolean" />
    <asp:Parameter Name="First" Type="String" />
    <asp:Parameter Name="Mid" Type="Char" />
    <asp:Parameter Name="Last" Type="String" />
</UpdateParameters>
/**For Brevity I've excluded the other commands parameters*/

最后是ListView控制器的标记:

<asp:ListView ID="PendingUpdates" runat="server" DataKeyNames="ID" DataSourceID="WaterFacultySqlDataSrc" OnItemUpdating="Members_VerifyChanges">
    <LayoutTemplate>
        <table id="PendingUpdatesTable" runat="server">
            <tr id="Tr1" runat="server">
                <td id="Td1" 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 id="TableHeaders" runat="server" style="background-color: #E0FFFF;color: #333333;">
                             <th id="ApproveOrDeny" runat="server">
                                 Approve/Deny</th>
                             <th id="WF_ID_Header" runat="server">
                                 ID</th>
                             <th id="Approved_Header" runat="server">
                                 Approved</th>
                             <th id="First_Header" runat="server">
                                 First Name</th>
                             <th id="Middle_Header" runat="server">
                                 Initial</th>
                             <th id="Last_Header" runat="server">
                                 Last Name</th>
                         </tr>
                         <tr ID="itemPlaceHolder" runat="server">
                         </tr>
                     </table>
                 </td>
             </tr>
         </table>
    </LayoutTemplate>
    <ItemTemplate>
        <tr style="background-color: #E0FFFF;color: #333333;">
            <td>
                <asp:Button ID="Edit_Button" runat="server" CommandName="Edit" Text="Edit"/>
                <asp:Button ID="Delete_Button" runat="server" CommandName="Delete" Text="Delete"/>
            </td>
            <td>
                <asp:Label ID="ID_Val" runat="server" Text='<%# Bind("ID") %>' />
            </td>
            <td>
                <asp:Label ID="Approve_Val" runat="server" Text='<%# Bind("Approved") %>' />
            </td>
            <td>
                <asp:Label ID="First_Val" runat="server" Text='<%# Bind("FirstName") %>' />
            </td>
            <td>
                <asp:Label ID="Middle_Val" runat="server" Text='<%# Bind("MiddleInitial") %>' />
            </td>
            <td>
                <asp:Label ID="Last_Val" runat="server" Text='<%# Bind("LastName") %>' />
            </td>
        </tr>
    </ItemTemplate>
    <EditTemplate>
        <tr style="background-color: #E0FFFF;color: #333333;">
            <td>
                <asp:Button ID="Save_Button" runat="server" CommandName="Update" Text="Save"/>
                <asp:Button ID="Cancel_Button" runat="server" CommandName="Cancel" Text="Cancel"/>
            </td>
            <td>
                <asp:Label ID="ID_Val" runat="server" Text='<%# Bind("ID") %>' />
            </td>
            <td>
                <asp:checkBox ID="Approve_Val" runat="server" checked='<%# Bind("Approved") %>' />
            </td>
            <td>
                <asp:textBox ID="First_Val" runat="server" Text='<%# Bind("FirstName") %>' />
            </td>
            <td>
                <asp:textBox ID="Middle_Val" runat="server" Text='<%# Bind("MiddleInitial") %>' />
            </td>
            <td>
                <asp:textBox ID="Last_Val" runat="server" Text='<%# Bind("LastName") %>' />
            </td>
        </tr>
    </EditTemplate>

</asp:ListView>

1 个答案:

答案 0 :(得分:0)

不确定原因,但更改了2个参数的名称修复了此错误。

UPDATE Members SET [ID]=original_ID, [Approved]=@App, [FirstName]=@FirstName, [MiddleInitial]=@Mid, [LastName]=@LastName WHERE ID=@original_ID}

我猜测Microsoft SQL Server 2008 R2正在解析First和Last作为关键字,以指示按时间顺序排列的First和Last参数,而不是名为First和Last的参数。我无法证实这一点,但是当我找到答案时,我会继续挖掘并发布答案。