3

我有这个 sqlDataSource
@userId 是系统中当前用户的参数。

<asp:SqlDataSource ID="dsProfit" runat="server" ConnectionString="<%$ ConnectionStrings:DefaultConnection %>" SelectCommand="select name, sum(value) as suma  from AllProfitView where IdUser=@userId group by name  order by sum(value) desc">

</asp:SqlDataSource>

在后面的代码中,我在 Page_Load 中有这个:

 dsProfit.SelectParameters.Add("@userId", cui.getCurrentId());




 public Guid getCurrentId()
        {
            MembershipUser currentUser = Membership.GetUser();
            Guid currentUserId = (Guid)currentUser.ProviderUserKey;
            return currentUserId;
        }

启动页面时出现错误:必须声明标量变量“@userId”。

4

2 回答 2

1

添加SelectParameter到您的 sqlDataSource

<asp:SqlDataSource ID="dsProfit" runat="server"  
ConnectionString="<%$ ConnectionStrings:DefaultConnection %>"  
SelectCommand="select name, sum(value) as suma  from AllProfitView  
where IdUser=@userId group by name  order by sum(value) desc">
    <SelectParameters>
        <asp:Parameter Name="userId" Type="String" />
    </SelectParameters>
</asp:SqlDataSource>

并分配喜欢这个

  dsProfit.SelectParameters["userId"].DefaultValue =  
  cui.getCurrentId().ToString();
于 2013-07-27T10:13:04.113 回答
0

尝试在 SqlDatasource 中定义 selectparameter

<asp:SqlDataSource ID="dsProfit" runat="server" ConnectionString="<%$ ConnectionStrings:DefaultConnection %>" SelectCommand="select name, sum(value) as suma  from AllProfitView where IdUser=@userId group by name  order by sum(value) desc">
<SelectParameters>
            <asp:Parameter Name="userId" Type="int32" />
</SelectParameters>

</asp:SqlDataSource>

所以你的参数是 int 所以将它转换为字符串

 dsProfit.SelectParameters["userId"].DefaultValue = cui.getCurrentId().toString();
于 2013-07-27T10:13:18.597 回答