Linq to Sql: Retrieve properties from related data (LoadWith)

You have to specify which related-data you want to retrieve from a object so you can access them outside the Linq data-context. You can achieve this by using the LoadWith method of the DataLoadOptions Class. The LoadWith method accepts an lambda expression that specifies which object you want to retrieve.

In the following example I have a employee table that has a relation with the company table. In my code I want to show the employee with the company name (outside the DataContext).


Employee employee;

using (LinqDataContext db = new (LinqDataContext())
{
   DataLoadOptions dlo = new DataLoadOptions(); 
   dlo.LoadWith(e => e.Company);
   db.LoadOptions = dlo;

   employee = from item in db.Employees
                      select item).First();
}

string companyName = employee.Company.Name;


Because of the DataLoadOptions I can now use the company properties to print the company name outside the DataContext.

Enjoy.

Membership: Update Password MembershipUser (ChangePassword)

You can change the password of a MembershipUser object in C# with the ChangePassword method. The only problem of this method is that it needs the old password(string) as input parameter and I only have the hashed version off the password. To get the old password you can user the MembershipUser.ResetPassword() method. This method will reset the password and returns the new password as string.



MembershipUser user = Membership.GetUser(userNameTxt.Text);
user.ChangePassword(user.ResetPassword(), passwordTxt.Text);


Don’t for get to check that the enablePasswordReset setting is true in the web.config.

[code:xml]

    <membership>
      <providers>
        <remove name=”AspNetSqlMembershipProvider” />
        <add name=”AspNetSqlMembershipProvider” type=”System.Web.Security.SqlMembershipProvider, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a”
        connectionStringName=”SiteSqlServer”
        enablePasswordRetrieval=”false”
        enablePasswordReset=”true
        requiresQuestionAndAnswer=”false”
        applicationName=”/”
        requiresUniqueEmail=”false”
        minRequiredPasswordLength=”3″
        minRequiredNonalphanumericCharacters=”0″
        passwordFormat=”Hashed”
        maxInvalidPasswordAttempts=”5″
        passwordAttemptWindow=”10″
        passwordStrengthRegularExpression=””/>
      </providers>
    </membership>

[/code]

Goodluck!

Microsoft Tag: Snap my vCard

When surfing the internet I start reading about Microsoft Tag Beta (of course). With Microsoft Tag you can create links with a image-pattern. The image-pattern can be decoded to a link with the Microsoft Tag program on your Windows Mobile (with camera).

You can create graphical image-patterns with the following actions:

  • Link to an URL
  • Free text
  • vCard
  • Dialer

It looks very promising. Not every tag on a screen gets recognized at once, but the printed tags work very well.

I've placed my vCard in a tag on my blog. Just try to tag it 😉

Some tips for the Asp.net dropdown control

I always have problems implementing the Asp.Net dropdown control. Here are some simple tips to make your life (with dropdown controls) easier.

Adding an default item with data binded dropdown

To add an default listItem to your data binded dropdown list you just have to add a ListItem on the first place (0) of your control


CountryDropdown.DataSource = countryList;
CountryDropdown.DataValueField = "CountryId";
CountryDropdown.DataTextField = "CountryName";
CountryDropdown.DataBind();

//Add the item
CountryDropdown.Items.Inser(0, new ListItem("== Choose a Country =="));



Getting the selected value


CountryDropdown.Items[CountryDropdown.SelectedIndex].Text;



Setting dropdown listitem by value


dropDownList.SelectedIndex = dropDownList.Items.IndexOf(dropDownList.Items.FindByValue(option.OptionValue));


Validation on a dropdownlist





Hope it helps.