Return to Snippet

Revision: 28110
at July 1, 2010 00:15 by mmfusion


Initial Code
protected void fixProfile(aspnet_Profile p)
{
  string[] okfields = { "names", "of", "profile", "properties",
                        "you", "want", "to", "keep"};

  // search for: fieldname:S:startpos:length
  string regex = @"([a-zA-Z0-9]+):S:([0-9]+):([0-9]+):";

  string newNames = string.Empty;
  string newValues = string.Empty;
  
  int pos = 0;
  System.Text.RegularExpressions.Regex rgx 
    = new System.Text.RegularExpressions.Regex(regex);
  System.Text.RegularExpressions.Match match 
    = rgx.Match(p.PropertyNames, pos);

  while ( match != null && match.Success ) {
    string propertyName = match.Groups[1].Value;
    if (okfields.Contains(propertyName))
    {
      // We want to keep this profile property. Extract the 
      // value and then append to newNames/newValues
      int startPos = Convert.ToInt32(match.Groups[2].Value);
      int len = Convert.ToInt32(match.Groups[3].Value);
      string value = p.PropertyValuesString.Substring(startPos, len);
      newNames += string.Format(
        "{0}:S:{1}:{2}:",
        propertyName,
        newValues.Length,
        len);
      newValues += value;
    }
    pos = match.Index + match.Length;
    match = rgx.Match(p.PropertyNames, pos);
  }

  p.PropertyNames = newNames;
  p.PropertyValuesString = newValues;

}

protected void cleanMyData()
{
  // db is your DataContext
  foreach (var user in db.aspnet_Users)
  {
    aspnet_Profile p = user.aspnet_Profile;
    if (p == null) continue;

    fixProfile(p);
    db.SubmitChanges();
  }
}

Initial URL

                                

Initial Description
I removed several properties from my custom Profile class, and wanted to clean out the data from the aspnet\_Profile table. This code will remove all unwanted data from the PropertyNames and PropertyValuesString columns of aspnet\_Profile. Note this assumes PropertyValuesBinary is not used, though it wouldn't be hard to support that too.

Initial Title
ASP.NET Membership Provider - Remove Unwanted Profile Properties

Initial Tags
Net, aspnet

Initial Language
C#