Error: Attempted to read or write protected memory (while filling a datareader)
Hello all,
I am having a problem with some code, and haven't been able to work it out on my own :confused: . Hopefully one of you nice people can help me clear this up :thumb: .
Here is the code:
Code:
Private Sub BindSummary()
Dim _sql As String = "SELECT * FROM [qry_Project_ProjectedFee_Summary] WHERE [Month/Year] = ? ORDER BY [Name]"
Dim _dbConn As New OleDbConnection(ConfigurationManager.ConnectionStrings("MyDbConn1").ToString)
Dim _dbComm As New OleDbCommand(_sql, _dbConn)
_dbComm.Parameters.Add(New OleDbParameter("[Month/Year]", CDate(lstCurrentMonth.SelectedItem.Value)))
_dbComm.Connection.Open()
Summary.DataSource = _dbComm.ExecuteReader
Summary.DataBind()
_dbComm.Connection.Close()
' Get Totals
_sql = "SELECT SUM(Total) AS Total, SUM(Claimed) AS Claimed, SUM(Initial) - SUM(SubInitial) AS Initial" & _
", SUM(Actual) - SUM(Actual) AS Actual, SUM(Revised) - SUM(SubRevised) AS Revised" & _
", SUM(Future1) - SUM(SubFuture1) AS Future1, SUM(Future2) - SUM(SubFuture2) AS Future2" & _
", SUM(Remaining) - SUM(SubRemaining) AS Remaining " & _
"FROM(qry_Project_ProjectedFee_Summary) WHERE [Month/Year] = ?"
_dbComm.CommandText = _sql
_dbComm.Parameters.Add(New OleDbParameter("[Month/Year]", CDate(lstCurrentMonth.SelectedItem.Value)))
_dbComm.Connection.Open()
Dim _dbRead As OleDbDataReader = _dbComm.ExecuteReader()
_dbComm.Connection.Close()
_CurrentMonth = lstCurrentMonth.SelectedItem.Value
_dbRead = Nothing
_dbComm = Nothing
_dbConn = Nothing
End Sub
The error occurs at the code in bold.
This is the exact error:
Code:
Server Error in '/' Application.
--------------------------------------------------------------------------------
Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
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.AccessViolationException: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
Source Error:
Line 89: _dbComm.Connection.Open()
Line 90:
Line 91: Dim _dbRead As OleDbDataReader = _dbComm.ExecuteReader()
Line 92:
Line 93: _dbComm.Connection.Close()
Source File: C:\Inetpub\pmacs\FeeProjection.aspx Line: 91
Stack Trace:
[AccessViolationException: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.]
System.Data.Common.IAccessor.ReleaseAccessor(IntPtr hAccessor, Int32& pcRefCount) +0
System.Data.OleDb.RowBinding.Dispose() +90
System.Data.OleDb.Bindings.Dispose() +29
System.Data.OleDb.OleDbCommand.InitializeCommand(CommandBehavior behavior, Boolean throwifnotsupported) +46
System.Data.OleDb.OleDbCommand.ExecuteCommand(CommandBehavior behavior, Object& executeResult) +16
System.Data.OleDb.OleDbCommand.ExecuteReaderInternal(CommandBehavior behavior, String method) +106
System.Data.OleDb.OleDbCommand.ExecuteReader(CommandBehavior behavior) +111
System.Data.OleDb.OleDbCommand.ExecuteReader() +6
ASP.FeeProjection_aspx.BindSummary() in C:\Inetpub\pmacs\FeeProjection.aspx:91
ASP.FeeProjection_aspx.lstCurrentMonth_SelectedIndexChanged(Object sender, EventArgs e) in C:\Inetpub\pmacs\FeeProjection.aspx:129
System.Web.UI.WebControls.ListControl.OnSelectedIndexChanged(EventArgs e) +105
System.Web.UI.WebControls.ListBox.RaisePostDataChangedEvent() +132
System.Web.UI.WebControls.ListBox.System.Web.UI.IPostBackDataHandler.RaisePostDataChangedEvent() +7
System.Web.UI.Page.RaiseChangedEvents() +137
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +5330
--------------------------------------------------------------------------------
Version Information: Microsoft .NET Framework Version:2.0.50215.44; ASP.NET Version:2.0.50215.44
It seems like no matter what I do, the .ExecuteReader method always fails.
I have tried creating a new command object for the second execution of executereader, but I still had the same error.
Any help, much appreciated :wave:
Re: Error: Attempted to read or write protected memory (while filling a datareader)
Oh S##t Michael, that's the first time I see that error! :eek:
Just a silly suggestion, have you tried
Code:
Dim _dbRead As New OleDbDataReader = _dbComm.ExecuteReader()
¿
I've searched basically through the whole web, and everybody's posts didn't have any answers,
I've also seen that it not only happens with DataReaders, it happens in games and other non-programming related programs as well!.
Bad Memory location ¿¿
Re: Error: Attempted to read or write protected memory (while filling a datareader)
Hi Hannes,
I tried using the 'new' statement, but the OleDbDataReader class doesn't have any constructors.
I doubt that the memory is actually bad as it is a brand new server, and is running a lot of stuff (seemingly with no problems).
I thought maybe i'd left an object open, but haven't been able to spot it. I even restarted the server, but same error in the same place.
Cheers man ;)
Re: Error: Attempted to read or write protected memory (while filling a datareader)
Just because the server is new doesn't mean that some memory can't be faulty/corrupted.
My first step would be to try running the code of a different (physical) server just to elimate this factor.
Also - is it a general problem you have with datareaders in this project, or is it only this one particular ?
Re: Error: Attempted to read or write protected memory (while filling a datareader)
Also, is there a particular reason you're using a datareader for this instead of a dataadapter? The datareaders are typically for when you need to sequentially access data / perform an operation on it / etc. Since this is a web app, this is basically going to read the data into a datarowcollection. Try using an adapter and fill a dataset table. You can then fill two tables for each query and assign the datasources as the table.
If I were to guess, I'd say the datareader backend isn't finished when you try to call it again... (though technically that shouldn't happen)
Re: Error: Attempted to read or write protected memory (while filling a datareader)
@Alsvha:
I only have access to this server on my company's network (the other one is novell - sucks bigtime!). The application uses trusted sql connection - and the sql server isn't visible to outside networks.
Thanks though ;)
@mmetzger:
I hear ya :) I'm going to try the dataadapter now..
Thanks :wave:
Re: Error: Attempted to read or write protected memory (while filling a datareader)
Quote:
Originally Posted by HairyMonkeyMan
@Alsvha:
I only have access to this server on my company's network (the other one is novell - sucks bigtime!). The application uses trusted sql connection - and the sql server isn't visible to outside networks.
Thanks though ;)
<snip>
Could it be this architeture which is the problem?
That you are trying to read data from your connection when it is no longer "trusted" so to speak.
I am not terrible familiar with sql connections and doens't hold much network knowlegde - but I do seem to recall that datareaders continue to hold the reference/connection to the database while "alive", so possible it could be the reader no longer has permision to hold this reference when you try to read from it?
Just brainstorming here :)
Re: Error: Attempted to read or write protected memory (while filling a datareader)
i don't think it has something to do with the datareader thing..
i get exactly the same error in a different scenario..i used a third party software for my report to be converted to pdf..i don't integrate my work with any sql connections..it seems fine when executed on localhost, i can generate all of my reports in pdf format, but when i uploaded it to the server, there i got the error..
it can show the report via html format but won't do in pdf..
so i guess its not with the datareader, but perhaps with some conflicts in the dll files..am not sure though, but as far as i can see with all the other posts, they seem to have the dll trouble prior to some migration activities they did..
but if by any chance somebody gets to share some light on this matter, please be kind enough to post them here..thank you:)
Re: Error: Attempted to read or write protected memory (while filling a datareader)
I also had the same problem in one of my components developed in .NET 2.0. I tried all of the options/suggestions given by others. But finally i got my problem solved just be re-installing the .NET Framework 2.0 on our server and everything worked as it should. Please try yourself and let me know if this helps anyone.
-Gurdev
Re: Error: Attempted to read or write protected memory (while filling a datareader)
I got the same error "System.AccessViolationException: Attempted to read or write protected memory."
Restarting VisualStudio fixed it. Lucky!