使用Guid参数的SqlDataSource

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”。
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();

dsProfit.SelectParameters["userId"].DefaultValue = cui.getCurrentId().ToString(); - 无法将 Guid 转换为字符串 - Tedi Hadjieva
请在 Tedi 的 GUID 前添加 Type="Int32" 并将其转换为整数。我已经编辑了我的回答! - zey
仍然无法将整数隐式转换为字符串。 - Tedi Hadjieva

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>

所以你的参数是整数,将其转换为字符串

 dsProfit.SelectParameters["userId"].DefaultValue = cui.getCurrentId().toString();

我认为,不是 QueryStringParameter Kemal :) 它只是从查询字符串中获取值。但 OP 想要在代码后台进行赋值。 - zey

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接