Wednesday 17 December 2008

Changing password to a password of your choosing using Asp.Net Membership

You can use the Membership.Provider.ResetPassword method to change someone's password however you don't get to dictate the form of the password that is generated to any great extent (passwords will look like :{nV{l4ec2Z@#5 which isn't very helpful).

There is a method Membership.Provider.ChangePassword which sounds like it would do the job however it requires that you know the existing password. If you are using any form of encryption for the passwords in your Membership provider then you're not going to be able to get hold of that password!!

So your two options are:

1. Write a Custom Provider and override the GeneratePassword method
2. use this little trick:

string oldPassword = Membership.Provider.ResetPassword(username, null);

string newPassword = CreateRandomPassword(); // This is your custom GeneratePassword method

Membership.Provider.ChangePassword(username, oldPassword, newPassword);

So what you're doing here is first changing the password using the Membership provider's ResetPassword method, this will return you the password as a unencrypted string which is what you need! Then you can use the ChangePassword method and give it both the reset password one and a password that you want and the password will be changed successfully.

Easy when you know how!!

Thursday 4 December 2008

Button updating a RadAjaxPanel

I encountered this error today when putting a RadAjaxPanel around a load of form controls and a button and linking them to a RadAjaxManager and RadAjaxLoadingPanel:

Invalid postback or callback argument. Event validation is enabled using in configuration or <%@ Page EnableEventValidation="true" %> in a page.

The solution it seems is to put EnableEventValidation="false" in the Page directive at the top of the aspx page.

Wednesday 3 December 2008

PIVOT statement in Sql 2005

Wrote my first PIVOT statement today:

SELECT *
FROM (SELECT u_id, u_firstname, u_lastname, u_email, a.ag_name, u.u_phone, u.u_jobtitle, rolename
FROM [user] u INNER JOIN
agency a ON u.u_ag_id = a.ag_id INNER JOIN
aspnet_usersinroles m ON u.u_asp_userid = m.userid INNER JOIN
aspnet_roles r ON m.roleid = r.roleid) o PIVOT (count(rolename) FOR rolename IN ([Basic], [Practitioner], [TrainingManager], [CPDManager], [Administrator], [Assessor])) p

Tuesday 2 December 2008

Mailbox unavailable. The server response was 5.7.1 Unable to relay for (email)

Encountered this today after deploying a project to a server. The email component stopped working and gave me this error:

Mailbox unavailable. The server response was 5.7.1 Unable to relay for (email)

The solution was to add the following:

deliveryMethod="PickupDirectoryFromIis"

into the system.net/mailSettings/smtp node in the web.config as an attribute. Put that in and it works fine!

Monday 1 December 2008

Javascript not running on PostBack inside an UpdatePanel

I had some Javascript that I wanted to call on every Page_Load. I had an UpdatePanel on my page and I found that the Javascript wouldn't run on a PostBack. My problem was that I needed to user ScriptManager instead of Page.ClientScript. So I switched:

Page.ClientScript.RegisterStartupScript(typeof(Page), "onload", "alert('hello"');", true);

to:

ScriptManager.RegisterStartupScript(this, typeof(Page), "onload", "alert('hello"');", true);

And it worked!

Monday 24 November 2008

Telerik RadGrid - Getting the id of the selected row through a RadContextMenu

Client side code:

<script type="text/javascript">
function RowContextMenu(sender, e)
{
var index = e.get_itemIndexHierarchical();
document.getElementById("radGridClickedRowIndex").value = index;
var menu = $find("<%= PractitionerContextMenu.ClientID %>");
var evt = e.get_domEvent();
menu.show(evt);

e.cancelBubble = true;
e.returnValue = false;

if (e.stopPropagation)
{
e.stopPropagation();
e.preventDefault();
}
sender.get_masterTableView().selectItem(sender.get_masterTableView().get_dataItems()[index].get_element(), true);
}
</script>

Add the following into your RadGrid

<ClientSettings>
<ClientEvents OnRowContextMenu="RowContextMenu"></ClientEvents>
<Selecting AllowRowSelect="true" />
</ClientSettings>

Also make sure you have the DataKeyNames property set on the RadGrid:

DataKeyNames="Id"

Then in the code-behind have this method for the OnItemClick property of the RadContextMenu

protected void ContextMenu_ItemClick(object sender, RadMenuEventArgs e)
{

int gridClickedRowIndex = Convert.ToInt32(Request.Form["radGridClickedRowIndex"], CultureInfo.CurrentCulture);

int id = (int)PractitionerGrid.Items[gridClickedRowIndex].GetDataKeyValue("Id");

// Do whatever
}

Thursday 7 August 2008

Implementing IDisposable

Here's a test class which implements IDisposable and shows the methods and implementations you need to include:

class Test : IDisposable
{
private bool isDisposed = false;

~Test()
{
Dispose(false);
}

protected void Dispose(bool disposing)
{
if (disposing)
{
// Code to dispose the managed resources of the class
}
// Code to dispose the un-managed resources of the class

isDisposed = true;
}

public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
}

The type initializer for '...Configuration.ObjectBuilder.EnterpriseLibraryFactory' threw an exception

Saw this error today:

The type initializer for 'Microsoft.Practices.EnterpriseLibrary.Common.Configuration.ObjectBuilder.EnterpriseLibraryFactory' threw an exception

A quick googling tells me that I needed to add a reference into my project to Microsoft.Practices.ObjectBuilder.dll

I added that in and hey presto, problem solved!