3

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

2 Answers 2

1

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();
Sign up to request clarification or add additional context in comments.

3 Comments

dsProfit.SelectParameters["userId"].DefaultValue =cui.getCurrentId(); - Cannot convert guid to string
add Type="Int32" and par your GUID to integer Tedi , I've edited my answer !
still cannot implicityconvert int to string
0

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

1 Comment

I think , not QueryStringParameter Kemal :) It just get value from query string . But OP want to assign it from code behind .

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.