سؤال

Below is the code I am trying to implement.

I want to return data for the user logged in, when I run a listview using this datasource without WHERE UserName = @UserName I get a full list of my users.

However, I want only to return the logged in users. With WHERE UserName = @UserName and my Select Parameters set I get null data returned - how do I change my SELECT statement to select data for my logged in user?

<asp:sqldatasource 
    id="SqlDataSource1" 
    runat="server" 
    connectionstring="<%$ ConnectionStrings:SecondConnectionString %>"
    selectcommand="SELECT vw_aspnet_MembershipUsers.UserId, vw_aspnet_MembershipUsers.Email, vw_aspnet_MembershipUsers.CreateDate, vw_aspnet_MembershipUsers.LastLoginDate, vw_aspnet_MembershipUsers.UserName, vw_aspnet_MembershipUsers.LastActivityDate
    WHERE UserName = @UserName">
    <SelectParameters>
        <asp:Parameter name="UserName" type="String" DefaultValue="LoggedInUser" />
    </SelectParameters>
</asp:sqldatasource>
هل كانت مفيدة؟

المحلول

Your select parameter is probably never set. I suggest that you try to set the default value to something that exists in your database. If that makes a difference, you'll know that the parameter is never set to anything other than the default.

You can set the value of the parameter by using a controlparameter instead:

<SelectParameters>
    <asp:ControlParameter ControlID="__Page" Name="UserName" PropertyName="LoggedInUser" Type="String" />
</SelectParameters>

This will get the username from a property called LoggedInUser in the codebehind.

public string LoggedInUser
{
  get 
  {
    return "RJoness";
  }
}

Or you can set the parameter directly from codebehind:

SqlDataSource1.SelectParameters.Add("UserName", "RJoness");
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top