必须在delete语句中声明标量变量“@UserId”。

时间:2012-07-09 07:39:32

标签: c# asp.net sql runtime-error

我遇到了这个错误:当我尝试删除SqlServer中的一个特定行时,必须声明标量变量“@UserId”。我已经在DeleteParameters中声明UserId,但仍会出现此错误。

  

异常详细信息:System.Data.SqlClient.SqlException:必须声明标量变量> "@UserId".

     

来源错误:

     

在执行当前Web请求期间生成了未处理的异常。可以使用下面的异常堆栈跟踪来识别有关异常的起源和位置的信息。

这是我的代码:

<asp:SqlDataSource ID="SqlDataSource1" runat="server" 
    ConnectionString="<%$ ConnectionStrings:ASPNETDBConnectionString1 %>" 
    SelectCommand="SELECT aspnet_Membership.Email, Details.CustName, Details.CustNum, Details.CustRole, Details.CustStatus, Details.PName, Details.PEmail, Details.PRole, Details.WedDate, aspnet_Users.UserName, Details.UserId FROM Details INNER JOIN aspnet_Membership ON Details.UserId = aspnet_Membership.UserId INNER JOIN aspnet_Users ON aspnet_Membership.UserId = aspnet_Users.UserId WHERE (Details.UserId = @UserId)"
    UpdateCommand="update Details SET CustName = @CustName, CustNum = @CustNum, CustRole = @CustRole, CustStatus = @CustStatus, PName = @PName, PEmail = @PEmail, PRole = @PRole, WedDate = @WedDate WHERE [UserId] = @UserId Update aspnet_Membership Set Email= @email WHERE [UserId] = @UserId"
    DeleteCommand="DELETE FROM Details WHERE [UserId] = @UserId">
    <DeleteParameters>
        <asp:Parameter Name="UserId" type="String" />
        <asp:Parameter Name="CustName" />
        <asp:Parameter Name="CustNum" />
        <asp:Parameter Name="CustRole" />
        <asp:Parameter Name="CustStatus" />
        <asp:Parameter Name="PName" />
        <asp:Parameter Name="PEmail" />
        <asp:Parameter Name="PRole" />
        <asp:Parameter Name="WedDate" />
        <asp:Parameter Name="email" />
    </DeleteParameters>
    <SelectParameters>
        <asp:ControlParameter ControlID="lblHidden" Name="UserId"
        PropertyName="Text" />
    </SelectParameters>
    <UpdateParameters>
        <asp:Parameter Name="CustName" />
        <asp:Parameter Name="CustNum" />
        <asp:Parameter Name="CustRole" />
        <asp:Parameter Name="CustStatus" />
        <asp:Parameter Name="PName" />
        <asp:Parameter Name="PEmail" />
        <asp:Parameter Name="PRole" />
        <asp:Parameter Name="WedDate" />
        <asp:Parameter Name="UserId" />
        <asp:Parameter Name="email" />
    </UpdateParameters>
</asp:SqlDataSource>
<asp:DetailsView ID="DetailsView1" runat="server"
    AutoGenerateRows="False" DataSourceID="SqlDataSource1"
    Height="50px" Width="125px">
    <Fields>
        <asp:BoundField DataField="Email" HeaderText="Email"
            SortExpression="Email" />
        <asp:BoundField DataField="CustName" HeaderText="CustName"
            SortExpression="CustName" />
        <asp:BoundField DataField="CustNum" HeaderText="CustNum"
            SortExpression="CustNum" />
        <asp:BoundField DataField="CustRole" HeaderText="CustRole"
            SortExpression="CustRole" />
        <asp:BoundField DataField="CustStatus" HeaderText="CustStatus"
            SortExpression="CustStatus" />
        <asp:BoundField DataField="PName" HeaderText="PName"
            SortExpression="PName" />
        <asp:BoundField DataField="PEmail" HeaderText="PEmail"
            SortExpression="PEmail" />
        <asp:BoundField DataField="PRole" HeaderText="PRole"
            SortExpression="PRole" />
        <asp:BoundField DataField="WedDate" HeaderText="WedDate"
            SortExpression="WedDate" />
        <asp:BoundField DataField="UserName" HeaderText="UserName"
            SortExpression="UserName" />
        <asp:BoundField DataField="UserId" HeaderText="UserId"
            SortExpression="UserId" />
        <asp:CommandField ShowDeleteButton="True"
            ShowEditButton="True" />
    </Fields>
</asp:DetailsView>
<asp:Label ID="lblHidden" runat="server" Text="Label" Visible="False">
</asp:Label>

1 个答案:

答案 0 :(得分:0)

我认为你已经粘贴了T-SQL查询。我想解释一下你在这里做了什么:

SELECT aspnet_Membership.Email, Details.CustName, Details.CustNum, 
    Details.CustRole, Details.CustStatus, Details.PName, Details.PEmail, 
    Details.PRole, Details.WedDate, aspnet_Users.UserName, Details.UserId 
FROM Details 
INNER JOIN aspnet_Membership ON Details.UserId = aspnet_Membership.UserId 
INNER JOIN aspnet_Users ON aspnet_Membership.UserId = aspnet_Users.UserId 
WHERE (Details.UserId = @UserId) 

此查询基于UserId

从下表中检索数据
  1. 详细
  2. aspnet_Membership
  3. aspnet_Users
  4. 您必须将UserId传递给SqlDataSource才能执行其功能:了解如何INSERT, UPDATE and DELETE with SQLDataSource