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>
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,
};
}
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.
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?
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.
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 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.
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 - 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.
I created a project in visual studio named MemberData.
I created a user control named MemberData
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:
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.
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/.
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.
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.
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!
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.
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;
}
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:
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:
Any help would be greatly apprecaited...
-Bobby
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:
Jeroen
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
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
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.
Here is an example of my MemberData object:
I hope this is what you where looking for.
Jeroen
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
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
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.
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
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
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.
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
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.
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?
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.
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.
Happy to help :). Could you mark the post which was the most helpful as the solution?
Jeroen
Made some small improvements on the GetCurrentMember method. Here it is :).
Jeroen
is working on a reply...