Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Bobby Wallace 37 posts 58 karma points
    Jan 16, 2012 @ 00:47
    Bobby Wallace
    0

    Accessing Member Information

    I need to access member information from within a master page. Either Email or nodeId would suffice.

    I tried declaring a local variable:

     

    <asp:Content ContentPlaceHolderID="HeadContent" runat="server">
        <script runat="server">
            var member = new System.Web.Security.Membership.GetUser();
        </script>
    </asp:Content>

    This produces the error:

    The contextual keyword 'var' may only appear within a local variable declaration

    Ultimately, I simply need to plug in either the User's Email or nodeId into a SQL Query: 

    <asp:SqlDataSource ID="dsProductPricing" runat="server" <blah... blah...>
    <SelectParameters>
    <asp:Parameter DefaultValue="<-- INSERT USER HERE -->" 
                    Name="[Email | nodeId]" />
            </SelectParameters>
    </asp:SqlDataSource>

    Any help would be greatly apprecaited...
    -Bobby

     

     

  • Jeroen Breuer 4908 posts 12265 karma points MVP 4x admin c-trib
    Jan 16, 2012 @ 10:54
    Jeroen Breuer
    2

    There are a couple of ways to do this. I prefer to use umbraco.library for this since it can return cached data. Here is a code sample where I get the member and store the data in a custom object:

    /// <summary>
    /// Return the current member.
    /// </summary>
    /// <returns></returns>
    public MemberData GetCurrentMember()
    {
        //Get the current member id.
        int currentMemberId = Convert.ToInt32(Membership.GetUser().ProviderUserKey);
    
        //Get the current member from the library method (which has a cache system).
        XPathNodeIterator memberIterator = library.GetMember(currentMemberId);
    
        //Convert the XPathNodeIterator to an XElement.
        XElement member = XDocument.Parse(memberIterator.Current.OuterXml).Root;
    
        //Convert the XElement to MemberData and return it.
        return new MemberData()
        {
            MemberId = Convert.ToInt32(member.Attribute("id").Value),
            LoginName = member.Attribute("loginName").Value,
            MemberName = member.Attribute("nodeName").Value,
            Bedrijfsnaam = member.Element("bedrijfsnaam").Value,
        };
    }

    Jeroen

  • Dirk De Grave 4541 posts 6021 karma points MVP 3x admin c-trib
    Jan 16, 2012 @ 12:06
    Dirk De Grave
    0

    Hi Bobby,

    you can't use that script tag in your content placeholder, you need to move that out of these placeholders. Anyway, Jeroen's suggestion should get you the info you need.

     

    Cheers,

    /Dirk

  • Bobby Wallace 37 posts 58 karma points
    Jan 17, 2012 @ 04:45
    Bobby Wallace
    0

    Ok guys, from what was posted, I attempted to create (through Visual Studio) a custom class - MemberData (App_Code/MemberData.cs). Without compiling, I can already see I'm a little off-base here. I like the class idea, but I feel so out of the box on this one. I promise I can get up to speed quickly, and I believe you two have me really close, but can you give me a little more?

    Thanks in advance,
    -Bobby 

  • Bobby Wallace 37 posts 58 karma points
    Jan 17, 2012 @ 04:56
    Bobby Wallace
    0

    Based upon what you've given me, I would like to declare the member attributes as properties of the class, and simply publish "get's" to retrieve the values. I hope you don't think I'm asking you to do the work here, and I'm very appreciative. I just feel like I'm trying to play bar-chords on a guitar before I learn my C-D-E's.

  • Jeroen Breuer 4908 posts 12265 karma points MVP 4x admin c-trib
    Jan 17, 2012 @ 06:50
    Jeroen Breuer
    0

    Here is an example of my MemberData object:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace Project.BO
    {
        public class MemberData
        {
            public int MemberId
            {
                get;
                set;
            }
    
            public string LoginName
            {
                get;
                set;
            }
    
            public string MemberName
            {
                get;
                set;
            }
    
            public string Bedrijfsnaam
            {
                get;
                set;
            }
        }
    }
    

    I hope this is what you where looking for.

    Jeroen

  • Bobby Wallace 37 posts 58 karma points
    Jan 18, 2012 @ 03:48
    Bobby Wallace
    0

    I feel like I'm getting others to write code for me. My knowledge of C# is pretty lacking. And while I don't want to switch to VB, getting these ideas to work is just not happening. I'm going to continue to hack around here, but I can't help but feel like what I'm looking for is relatively simple.

    I would really like to implement a class and simply pull the properties on an as needed basis. To me, that would be the clean/reusable approach.

    For the time being, I just want to modify a template and pull in the membership values. If anyone has something short and sweet, I would greatly appreciate the help.

    Thanks in advance,
    -Bobby 

  • Jeroen Breuer 4908 posts 12265 karma points MVP 4x admin c-trib
    Jan 18, 2012 @ 06:57
    Jeroen Breuer
    0

    The best of way doing this is to create a usercontrol (see this tutorial) and use the code samples I showed in there. You can add the usercontrol as a macro to your template. That way the membership values are available in your template.

    Jeroen

  • Bobby Wallace 37 posts 58 karma points
    Jan 19, 2012 @ 04:41
    Bobby Wallace
    0

    Jeroen - when you mentioned usercontrol, I got excited. I've implemented usercontrols for other things, primarily gridViews with encapsulated data. But this didn't seem to work.

    1. I created a project in visual studio named MemberData.
    2. I created a user control named MemberData
    3. I edited the code (MemberData.ascx.cs) and inserted the following:
    /// <summary>
    /// Return the current member.
    /// </summary>
    /// <returns></returns>
    publicMemberDataGetCurrentMember()
    {
       
    //Get the current member id.
       
    int currentMemberId =Convert.ToInt32(Membership.GetUser().ProviderUserKey);

       
    //Get the current member from the library method (which has a cache system).
       
    XPathNodeIterator memberIterator = library.GetMember(currentMemberId);

       
    //Convert the XPathNodeIterator to an XElement.
       
    XElement member =XDocument.Parse(memberIterator.Current.OuterXml).Root;

       
    //Convert the XElement to MemberData and return it.
       
    returnnewMemberData()
       
    {
           
    MemberId=Convert.ToInt32(member.Attribute("id").Value),
           
    LoginName= member.Attribute("loginName").Value,
           
    MemberName= member.Attribute("nodeName").Value,
           
    Bedrijfsnaam= member.Element("bedrijfsnaam").Value,
       
    };
    }

    Skip forward... when I "Browse Properties," I get I really ugly message:

    Error reading usercontrols/MemberData/MemberData.ascx


    Before you ask, I copied both the .ascx file, as well as the .dll. As I've hinted, I've had great success in implementing some controls via macros in the past. No, obiously I'm still a newbie when it comes to this stuff, and while I've done a ton of C++ in the past, I've only used VB with .NET. I chose Umbraco because of the .NET and I really don't want to change horses at this point.

    I'm sure I'll have more issues implementing the VB code I have already tested for this project, but I need to cross this threshold in order to even bring up the other objects.

    Again, help here is greatly appreciated,
    -Bobby 

     
  • Jeroen Breuer 4908 posts 12265 karma points MVP 4x admin c-trib
    Jan 19, 2012 @ 06:49
    Jeroen Breuer
    0

    Did you use the exact code example from above or did this forum change the markup, because publicMemberDataGetCurrentMember needs spaces and returnnewMemberData also. My examples might be in c#, but you can also use vb in Umbraco if you want to. You can use this converter to change c# in vb and than use that: http://www.developerfusion.com/tools/convert/csharp-to-vb/.

    Jeroen

  • Bobby Wallace 37 posts 58 karma points
    Jan 19, 2012 @ 14:11
    Bobby Wallace
    0

    Wow... I did copy-paste, including back into the forum. I never re-read the post. I did, just now, check the .ascx.cs file and the spaces are there.

    At any rate, I would like to stick with c#, but I am going to user this converter so maybe I can understand more.

  • Bobby Wallace 37 posts 58 karma points
    Jan 19, 2012 @ 15:27
    Bobby Wallace
    0

    I tried building a VB version, and I believe I got a bit further. Although, now when I try and browse the properties, I get:

    System.Web.HttpParseException (0x80004005): 'memberData.memberData' is not allowed here because it does not extend class 'System.Web.UI.UserControl'

    I understand the message, but I don't understand why. I don't want a UserControl... I simply want a class whereas I can reference the Membership data. I don't even want to display the information, I want to use it's values in things like SQL queries and such.

    Thanks again for all of your help,
    -Bobby 

  • Bobby Wallace 37 posts 58 karma points
    Jan 20, 2012 @ 01:09
    Bobby Wallace
    0

    Ok... I think I may actually have this. I've decided to create my form as a custom control. Then I can incorporate this class within the same code and it should work fine. Matter of fact, I can test all of the parts and peices and roll it out together.

  • Bobby Wallace 37 posts 58 karma points
    Jan 20, 2012 @ 15:22
    Bobby Wallace
    0

    Just when I though I was on a roll, I get the following:

    Error creating control (usercontrols/MemberData/MemberData.ascx).
    Maybe file doesn't exists or the usercontrol has a cache directive, which is not allowed! See the tracestack for more information!

    Any thoughts?

  • Barry Fogarty 493 posts 1129 karma points
    Jan 20, 2012 @ 15:46
    Barry Fogarty
    0

    Append ?umbDebugShowTrace=true to the end of your URL and Umbraco will display the trace stack, which should give you more info on the error.

    Assuming the UserControl is actually there, you can attach your Visual Studio debugger to the IIS worker porocess (commonly w3wp.exe) so you can set a breakpoint and step through your code.

  • Bobby Wallace 37 posts 58 karma points
    Jan 23, 2012 @ 15:03
    Bobby Wallace
    0

    Jeroen - Thanks again for all of your help. After some work and re-work, I've managed to come up with a series of user controls that I am happy with.

  • Jeroen Breuer 4908 posts 12265 karma points MVP 4x admin c-trib
    Jan 23, 2012 @ 15:05
    Jeroen Breuer
    0

    Happy to help :). Could you mark the post which was the most helpful as the solution?

    Jeroen

  • Jeroen Breuer 4908 posts 12265 karma points MVP 4x admin c-trib
    Jan 26, 2012 @ 17:56
    Jeroen Breuer
    0

    Made some small improvements on the GetCurrentMember method. Here it is :).

    /// <summary>
    /// Return the current member.
    /// If it's used multiple times on 1 request it get's stored in HttpContext.Current.Items.
    /// </summary>
    /// <returns></returns>
    public MemberData GetCurrentMember()
    {
        //Get the current member id.
        int currentMemberId = Convert.ToInt32(Membership.GetUser().ProviderUserKey);
    
        return GetMember(currentMemberId);
    }
    
    /// <summary>
    /// Return the member based on his id.
    /// If it's used multiple times on 1 request it get's stored in HttpContext.Current.Items.
    /// </summary>
    /// <param name="currentMemberId"></param>
    /// <returns></returns>
    public MemberData GetMember(int memberId)
    {
        MemberData member = (MemberData)HttpContext.Current.Items["Member-" + memberId];
    
        if (member == null)
        {
            //Get the member from the library method (which has a cache system).
            XPathNodeIterator memberIterator = library.GetMember(memberId);
    
            //Convert the XPathNodeIterator to an XElement.
            XElement memberXml = XDocument.Parse(memberIterator.Current.OuterXml).Root;
    
            //Convert the XElement to MemberData.
            member = new MemberData()
            {
                MemberId = Convert.ToInt32(memberXml.Attribute("id").Value),
                LoginName = memberXml.Attribute("loginName").Value,
                MemberName = memberXml.Attribute("nodeName").Value
            };
    
            HttpContext.Current.Items["Member-" + memberId] = member;
        }
    
        return member;
    }

    Jeroen

Please Sign in or register to post replies

Write your reply to:

Draft