Hey Guyz,
Can anyone direct me in the right path here please.... I am trying to do a password reset algorithm and I get the error "There is already an open DataReader associated with this Command which must be closed first" Please find below my code:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load If Not Membership.EnablePasswordRetrieval Then
FormsAuthentication.RedirectToLoginPage()
End If
Msg.Text =
"" If Not IsPostBack ThenMsg.Text =
"Please supply a username." ElseVerifyUsername()
End If End Sub
Public Sub VerifyUsername() u = Membership.GetUser(UsernameTextBox.Text, False) If u Is Nothing Then
Msg.Text =
"Username " & UsernameTextBox.Text & " not found. Please check the value and re-enter."QuestionLabel.Text =
""QuestionLabel.Enabled =
FalseAnswerTextBox.Enabled =
FalseResetPasswordButton.Enabled =
False ElseQuestionLabel.Text = u.PasswordQuestion
QuestionLabel.Enabled =
TrueAnswerTextBox.Enabled =
TrueResetPasswordButton.Enabled =
True End IfEnd SubProtected Sub ResetPasswordButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles ResetPasswordButton.Click Dim newPassword As Stringu = Membership.GetUser(UsernameTextBox.Text, False)
If u Is Nothing Then
Msg.Text =
"Username " & UsernameTextBox.Text & " not found. Please check the value and re-enter." Return End IfTry
newPassword = u.ResetPassword(AnswerTextBox.Text)
Catch ex As MembershipPasswordExceptionMsg.Text =
"Invalid password answer. Please re-enter and try again." ReturnCatch ex As ExceptionMsg.Text = ex.Message
Return End Try If Not newPassword Is Nothing ThenMsg.Text = "Password reset. Your new password is: " & newPassword ElseMsg.Text =
"Password reset failed. Please re-enter your values and try again."End IfEnd Sub
![]() |
1 |
![]() |
I'm not familiar with the built in membership methods, but it looks like your VerifyUsername method is firing on every postback (to include the button click on your reset button). It's possible that the built in mechanisms for membership don't close their datareaders immediately (not sure why they wouldn't, but that's the only thing I can see). I'd instead make your VerifyUsername method be a button click event handler for your "Login" button, see if that does away with the error.
I never lose, some people are just better than me at winning.
![]() |
1 |
![]() |
Same Error man...
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load If Not Membership.EnablePasswordRetrieval ThenFormsAuthentication.RedirectToLoginPage()
End If
Msg.Text =
"" 'If Not IsPostBack Then ' Msg.Text = "Please supply a username." 'Else ' VerifyUsername() 'End If End Sub'other codes here
'other codes here
Protected Sub btnVerifyUser_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnVerifyUser.ClickVerifyUsername()
End Sub
Any suggestion on how to rewrite the Password Reset Function?
![]() |
-1 |
![]() |
What membership provider are you using? Can you supply the stacktrace for the exception?
Please mark this post as the answer if you found it useful.
Cheers
Paul
![]() |
-1 |
![]() |
Hi Paul, thanks for your reply...
I am using a custom membership class adapted from MSDN that works with an MS SQL database, and the passwords and password answers are hashed with the SHA1 encryption type.
![]() |
1 |
![]() |
Hey Guyz, Can anyone direct me in the right path here please.... I am trying to do a password reset algorithm and I get the error "There is already an open DataReader associated with this Command which must be closed first" Please find below my code: Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load If Not Membership.EnablePasswordRetrieval Then FormsAuthentication.RedirectToLoginPage() End If
Msg.Text = "" If Not IsPostBack Then Msg.Text = "Please supply a username." VerifyUsername() //Try this on every postback this function is fired. Else End If End Sub
Public Sub VerifyUsername() u = Membership.GetUser(UsernameTextBox.Text, False) If u Is Nothing Then Msg.Text = "Username " & UsernameTextBox.Text & " not found. Please check the value and re-enter." QuestionLabel.Text = "" QuestionLabel.Enabled = False AnswerTextBox.Enabled = False ResetPasswordButton.Enabled = False Else QuestionLabel.Text = u.PasswordQuestion QuestionLabel.Enabled = True AnswerTextBox.Enabled = True ResetPasswordButton.Enabled = True End If End SubProtected Sub ResetPasswordButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles ResetPasswordButton.Click Dim newPassword As String u = Membership.GetUser(UsernameTextBox.Text, False)
If u Is Nothing Then Msg.Text = "Username " & UsernameTextBox.Text & " not found. Please check the value and re-enter." Return End If
Try newPassword = u.ResetPassword(AnswerTextBox.Text) Catch ex As MembershipPasswordExceptionMsg.Text = "Invalid password answer. Please re-enter and try again." Return Catch ex As ExceptionMsg.Text = ex.Message Return End Try If Not newPassword Is Nothing Then Msg.Text = "Password reset. Your new password is: " & newPasswordElse Msg.Text = "Password reset failed. Please re-enter your values and try again." End If
End Sub http://msdn.microsoft.com/en-us/library/system.web.security.membership.aspx
Regards Asif
|
![]() |
-1 |
![]() |
Hey... thanks for your reply...
I have took that Verify to a different Sub that fires on user interaction, and even tried to disable it... still no luck...
Maybe my algorithm is just messed up, can anyone help with an algorithm they have tried that works... please?
Protected Sub btnVerifyUser_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnVerifyUser.Click
VerifyUsername()
End Sub
![]() |
-1 |
![]() |
I suspect that your custom provider is not disposing datareaders. Can you post the source of the exception,ie the complete method that is executing.
Please mark this post as the answer if you found it useful.
Cheers
Paul
![]() |
-1 |
![]() |
Hi,whitewolve
whitewolve:
I am using a custom membership classPlease check your customized membership provider class. to see if there are two readers using one connection like this:
cmd1.connection = this.conn cmd2.connection = this.conn MyReader1 = cmd1.ExecuteReader ..... MyReader2=cmd2.ExecuteReader <----this is ok MyReader2 = cmd1.ExecuteReader <------this is notRegards
Andrew Zhu
Microsoft online ASP.NET support
Please remember to click “Mark as Answer” on the post that helps you. This can be beneficial to other community members reading the thread.
![]() |
-1 |
![]() |