There is already an open DataReader associated with this Command which must be closed first

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."

Else

VerifyUsername()

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 Sub

 

Protected 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 MembershipPasswordException

Msg.Text = "Invalid password answer. Please re-enter and try again."

Return

Catch ex As Exception

Msg.Text = ex.Message

Return

End Try

If Not newPassword Is Nothing Then

Msg.Text = "Password reset. Your new password is: " & newPassword

Else

Msg.Text = "Password reset failed. Please re-enter your values and try again."

End If

 

End Sub

0
whitewolve
4/29/2009 10:35:10 PM
asp.net.security 27051 articles. 1 followers. Follow

8 Replies
1437 Views

Similar Articles

[PageSpeed] 55

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.
0
AceCorban
4/29/2009 11:09:28 PM

Same Error man...

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."

'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.Click

VerifyUsername()

End Sub

 

Any suggestion on how to rewrite the Password Reset Function?

0
whitewolve
4/29/2009 11:28:31 PM
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
0
Paul
4/30/2009 2:25:59 AM

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.

0
whitewolve
4/30/2009 5:02:35 AM

 

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 Sub

 

Protected 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 MembershipPasswordException

Msg.Text = "Invalid password answer. Please re-enter and try again."

Return

Catch ex As Exception

Msg.Text = ex.Message

Return

End Try

If Not newPassword Is Nothing Then

Msg.Text = "Password reset. Your new password is: " & newPassword

Else

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

 


Please remember to click “Mark as Answer” on the post that helps you.
This can be beneficial to other community members reading the thread.
0
asifchouhan
4/30/2009 5:12:16 AM

 

 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

0
whitewolve
4/30/2009 5:28:32 AM

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
0
Paul
4/30/2009 11:47:43 AM

Hi,whitewolve

whitewolve:
I am using a custom membership class

Please 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 not
Regards

 
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.
0
Andrew
5/5/2009 6:24:05 AM
Reply:

Similar Artilces:

There is already an open DataReader associated with this Command which must be closed first.
Hi there,I get this error line 34.If I close datareader on line28 dr.close --> I can not go on read the data at while (dr.read()) scopeWhat should I do ? 1 2 SqlDataReader dr = nul; 3 sqlcommand cmd = nul; 4 protected void Page_Load(object sender, EventArgs e) 5 { 6 //COMMAND ITEMS CMD,CONN ETC 7 8 conn.Open(); 9 dr = cmd.ExecuteReader(); 10 while (dr.Read()) 11 { 12 I READ THE DATA 13 MYFUNCTION RUN HERE 14 } 15 16 dr.close(); 17 ...

There is already an open DataReader associated with this Command which must be closed first.
Hi, Could you please help me with the following issue?  1 SqlConnection thisConnection = new SqlConnection(@"Network Library=*******;Data Source=*******;database=******;User id=*****;Password=****");2 thisConnection.Open();3 SqlCommand thisCommand = thisConnection.CreateCommand();4 5 SqlCommand thisCommand2 = thisConnection.CreateCommand();6 thisCommand2.CommandText = "SELECT DISTINCT username FROM Employment";7 SqlDataReader thisReader2 = thisCommand2.E...

There is already an open DataReader associated with this Command which must be closed first.
System.InvalidOperationException: There is already an open DataReader associated with this Command which must be closed first. System.InvalidOperationException:   I need to insert in one table based on another table results so I made this, but I have that error on the executenonquery line. If I close the datareader I wont be able to insert the rows,    SqlCommand cmdConsultarEvaluadores = new SqlCommand("select * from TGSC_EVALUADORESXCICLOEMPRESA where nmciclo=@NmCiclo and nmempresa=@NmEmpresa and nmcargoevaluando=@NmCargoEvaluando", conn);    ...

There is already an open DataReader associated with this Command which must be closed first.
This doesn't seem to be a true statement based on my code, as I far as I can tell.  So, what needs to change in my code? Partial Class AddCall    Inherits System.Web.UI.Page    Protected Sub DropDownList1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles DropDownList1.SelectedIndexChanged        DropDownList2.SelectedIndex = 0    End Sub    Protected Sub DropDownList2_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles Drop...

There is already an open DataReader associated with this Command which must be closed first. #2
I'm running a recordset against another, but is displaying the error: There is already an open DataReader associated with this Command which must be closed first. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. Exception Details: System.InvalidOperationException: There is already an open DataReader associated with this Command which must be closed first.Source Error: Line 77: sqlCmd2 = New SqlCommand("select modulos...

There is already an open DataReader associated with this Command which must be closed first. #2
 Greetings!I'm experiencing this problem. Any suggestions? Here's the code below...  1 Dim Mycmd As SqlCommand 2 Mycmd = New SqlCommand("WEB_CT_select_CountryListNotNull", myConnection) 3 Mycmd.CommandType = CommandType.StoredProcedure 4 5 Dim myReader As SqlDataReader 6 7 myReader = Mycmd.ExecuteReader() 8 9 While myReader.Read() 10 'Response.Write("country code " & myReader(0).ToString() & " - ")...

There is already an open DataReader associated with this Command which must be closed first. #2
I have the below code and when I run it I get the error stated in the title, I have Dim dsEmail As DataSet on top in the class.   Dim sqlCon As New SqlConnection Dim sqlCmd As New SqlCommand Dim sqlDA As New SqlDataAdapter '* Configure and open connection sqlCon.ConnectionString = My.Settings.ConnectionString sqlCon.Open() '* Configure command object sqlCmd.CommandText = "EmailToSend" sqlCmd.CommandType = CommandType.StoredProcedure sqlCmd.Connection = sqlCon ...

Already an opened DataReader associated with this command exists, must close it first
Hello I have a page asp.net that does a query to database directly.using (SqlConnection con = new SqlConnection(ObtenerCadenaConx(bd))) { con.Open();SqlCommand cmd = new SqlCommand(sql, con); SqlDataReader r = cmd.ExecuteReader(); ** } This works fine except sometimes this produces the error "Already an opened DataReader associated with this command exists, must close it first" in the line **.But after some minutes, already works fine. Why will it be produced? Thanks for your attention. Regards. C Use this behavior;  SqlDataReader r = cmd.ExecuteReader...

There is already an open DataReader associated with this Command which must be closed first. #3
Hi I am getting the following error message "There is already an open DataReader associated with this Command which must be closed first."when the code below hits the line:sqltrans.commit() it throws the exception above.I am running VS2005, .NET 2.0 against a SQL Server 2000 database.This code worked fine on .NET 1.1 but since the conversion it no longer works.I would prefer not to use MARS multiple active record sets and I don't believe SQL Server 2000 supports it regardless.This error occured multiple times in other parts of this web app and I was able to fix them, this one d...

There is already an open DataReader associated with this Command which must be closed first. #4
 hi,i  m geting an error "There is already an open DataReader associated with this Command which must be closed first."but when im working whith MS access im not geting this error ,but in sql server2005 produce this errror ,..... plz hlp me out of this when you use datareader you have to close it after finished using it and before close the connection by using close() method ex. dr.Close() this should solve your problemRegards...Nassar, RamiMy Blog || E-Mail Don't forget to click "Mark as Answer" on the post that helped you.This credits that membe...

There is already an open DataReader associated with this Command which must be closed first. #2
hi guys, i have a code liek this dim rs as data.sqlclient.sqldatareader rs = objcmd.executereader while rs.has rows  dim objcmd2 as new sqlclient.sqlcommand("call storedprocedure,connection") objcmd2.parameters.addwithavlue("",rs.item()) objcmd2.executenonquery end while   the syntax so far is ok but everytime when i run it has this error message There is already an open DataReader associated with this Command which must be closed first. why is that guys? if any one knows please help me. thank you Try this: http://dotnetslackers.com/Community/blogs/...

How to fix this error? (There is already an open DataReader associated with this Command which must be closed first.)
This is my code:1 If Session("ctr") = False Then 2 3 Connect() 4 5 SQL = "SELECT * FROM counter" 6 SQL = SQL & " WHERE ipaddress='" & Request.ServerVariables("REMOTE_ADDR") & "'" 7 dbRead() 8 9 If dbReader.HasRows = True Then 10 11 dbReader.Read() 12 hits = dbReader("hits") 13 hits = hits + 1 14 ...

Run-time exception thrown : System.InvalidOperationException
I don't understand why i am getting this error. i got MARS enabled in the connection string. i have a single connection object and a single datareader, using visual studio 2005  Dim showString As String = "Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\ASPNETDB.MDF;Integrated Security=True;User Instance=True;Connection TimeOut=60;MultipleActiveResultSets=True;" Dim myDataReader2 As SqlDataReadereId = Long.Parse(Me.Request("id"))Dim get_Single = "some sql query here"             ...

Web resources about - There is already an open DataReader associated with this Command which must be closed first - asp.net.security

Highest Voted 'entity-framework' Questions - Stack Overflow
Q&A for professional and enthusiast programmers

Microsoft Data Access Components - Wikipedia, the free encyclopedia
This article has an unclear citation style . The references used may be made clearer with a different or consistent style of citation , footnoting ...

E-Trade and eBay Join Ad Age Data Conference October 28-29
... and more. Be sure to reserve your spot today. We're offering a limited 10% discount this week only to those who use the discount code DATAREADER. ...

Windows 8 WriteableBitmap Pixel Arrays in C# and C++
August 5, 2012 Roscoe, N.Y. Sometimes I thiink of myself as a scientist who studies operating systems. Although these operating systems form ...

Latest Updates
Latest Updates - Free source code and tutorials for Software developers and Architects.; Updated: 15 Feb 2013

moredotnet
M ore d otnet .NET Interview Questions, ASP.NET, C#, VB.NET, XML, SQL Check Out http://www.dotnetUncle.com √ .NET NOTES OOPS in .NET .NET ...

The Weekly Source Code 51 - Asynchronous Database Access and LINQ to SQL Fun - Scott Hanselman
Scott Hanselman on Programming, User Experience, The Zen of Computers and Life in General

Should you use Entity Framework for .NET applications?
I have been working on a project which I thought would be simpler than it turned out to be – nothing new there, most software projects are like ...

MSDN Magazine December 2001
The Windows XP kernel includes a number of improvements over Windows 2000 that promote better scalability and overall performance. This article ...

A Map for the Microsoft Application Development Platform
... N-Tier Remote Data Security Silverlight SQL Azure SQL Server Streaming XML Features ADO.NET DataSet ADO.NET DataTable ADO.NET DataReader ...

Resources last updated: 2/4/2016 1:59:56 PM