带Guid参数的SqlDataSource

时间:2013-07-27 10:05:24

标签: asp.net

我有这个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”。

2 个答案:

答案 0 :(得分: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();

答案 1 :(得分: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();