<asp:LoginName> tag value, used as parameter in SqlDataSource ???

Posted on 2009-02-20
Last Modified: 2012-05-06

Wondering, is there a way I can ...
Use the UserName value that is returned from Code Snippet One (the asp:LoginName tag)

and use it as a parameter in my SqlDataSource later on the page ...
I basically need the userName obtained from the LoginName tag placed in code Snippet 2 at the marker _Parameter, but have been unable to achieve this so far, I've tried it a couple of ways ... like this ... and also in the code, but have encounted no success.

any Ideas?
Code Snippet One:

    <asp:LoginView ID="LoginView1" runat="server">


            UserName&nbsp;&nbsp;<asp:LoginName ID="LoginName1" runat="server" />



    <p>Please select a folder to upload your file to.  <b>If no folder is selected file will be placed in the default folder.</b></p>

    <asp:GridView ID="GridView_FolderName_Selector" runat="server" AutoGenerateColumns="False" AllowPaging="False" 



            <asp:TemplateField HeaderText="Selection">


                    <asp:CheckBox ID="FolderNameSelector" runat="server" />



            <asp:BoundField DataField="FolderName" HeaderText="FolderName" 

            SortExpression="FolderName" />



    <asp:SqlDataSource ID="SqlDataSource1" runat="server" 


    SelectCommand="SELECT [FolderName] FROM [FolderPermissions] WHERE UserName = _Parameter"></asp:SqlDataSource>

Open in new window

Question by:BrianNDN
    LVL 15

    Expert Comment

    Sure, just use the authenticated user name.


    Author Comment

    I've done that in the cs file and played with it, but wasn't seeming to get it to work.
    Can that be inserted like:
    SelectCommand="SELECT [FolderName] FROM [FolderPermissions] WHERE UserName = User.Identity.Name"></asp:SqlDataSource>

    ??? if so, Do I need to put it as:  @User.Identity.Name

    LVL 15

    Accepted Solution

    No, you'll want to add a parameter

    <asp:SqlDataSource ID="SqlDataSource1" runat="server"
            SelectCommand="SELECT [FolderName] FROM [FolderPermissions] WHERE ([UserName] = @UserName)"
            ConnectionString="<%$ ConnectionStrings:LocalSqlServer %>">
                <asp:Parameter DefaultValue="@UserName" Name="UserName" Type="String" />

    And then in the form load, put this.
    SqlDataSource1.SelectParameters("UserName").DefaultValue = User.Identity.Name


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Training Course: Java/J2EE and SOA

    This course will cover both core and advanced Java concepts like Database connectivity, Threads, Exception Handling, Collections, JSP, Servlets, XMLHandling, and more. You'll also learn various Java frameworks like Hibernate and Spring.

    Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
    Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
    Sending a Secure fax is easy with eFax Corporate ( First, Just open a new email message.  In the To field, type your recipient's fax number You can even send a secure international fax — just include t…
    This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor ( If you're looking for how to monitor bandwidth using netflow or packet s…

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now