0

I have a drop down menu. My goal is to make it where the currently logged in person is first on the list, followed by an alphabetical list:

<asp:DropDownList ID="userNameDropDown" runat="server" DataSourceID="SqlDataSource1"
    DataTextField="Name" DataValueField="PK_User" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged" AutoPostBack="true">
</asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ApplicationServices %>"
    SelectCommand="SELECT [PK_User], [Name] FROM [User] ORDER BY CASE WHEN [LoginName] = '@userLoggedIn' THEN 1 ELSE 2 END, [Name]">
<SelectParameters>
    <asp:QueryStringParameter Name="userLoggedIn" Type="String" />
</SelectParameters>
</asp:SqlDataSource>

Here is my C# that should set the parameter:

protected void Page_Load(object sender, EventArgs e)
{
    SqlDataSource1.SelectParameters["userLoggedIn"].DefaultValue = User.Identity.Name;
}

The result is that the list is shown in alphabetical order, ignoring the logged in person. If I change the SelectCommand and make it hardcoded it works fine:

SelectCommand="SELECT [PK_User], [Name] FROM [User] ORDER BY CASE WHEN [LoginName] = 'tunnelld' THEN 1 ELSE 2 END, [Name]

In this case it does exactly what I want.

System.Diagnostics.Debug.Write(User.Identity.Name);

results in the output of : tunnelld

1
  • Does wrapping your Page_Load code in a if(!IsPostBack) conditional make any difference? Commented Jul 5, 2013 at 20:26

1 Answer 1

1

I figured out the problem:

[LoginName] = '@userLoggedIn'

should not have quotes, the following fixes it:

[LoginName] = @userLoggedIn
Sign up to request clarification or add additional context in comments.

Comments

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.