Tedi Hadjieva
Tedi Hadjieva

Reputation: 63

SqlDataSource with Guid Parameter

I have this sqlDataSource
@userId is my parameter for Current user in a system .

<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>

In code Behind I have this in Page_Load :

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




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

when start the page it's blows with ERROR :Must declare the scalar variable "@userId".

Upvotes: 3

Views: 1999

Answers (2)

zey
zey

Reputation: 6103

Add SelectParameter to your 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>

and assign likes this

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

Upvotes: 1

Kemal Umut
Kemal Umut

Reputation: 23

try to define selectparameter inside of your 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="int32" />
</SelectParameters>

</asp:SqlDataSource>

so your parameter is int so convert it to string

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

Upvotes: 0

Related Questions