Error: Attempted to read or write protected memory (while filling a datareader)
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10

Thread: Error: Attempted to read or write protected memory (while filling a datareader)

  1. #1
    Join Date
    Dec 2003
    Location
    Northern Ireland
    Posts
    1,362

    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 . Hopefully one of you nice people can help me clear this up .

    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
    Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. - Rich Cook


    0100 1101 0110 1001 0110 0011 0110 1000 0110 0001 0110 0101 0110 1100 0010 0000 0100 0101 0110 1100 0110 1100 0110 0101 0111 0010

  2. #2
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,074

    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!

    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

  3. #3
    Join Date
    Dec 2003
    Location
    Northern Ireland
    Posts
    1,362

    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
    Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. - Rich Cook


    0100 1101 0110 1001 0110 0011 0110 1000 0110 0001 0110 0101 0110 1100 0010 0000 0100 0101 0110 1100 0110 1100 0110 0101 0111 0010

  4. #4
    Join Date
    Feb 2005
    Location
    Denmark
    Posts
    742

    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 ?
    Last edited by Alsvha; November 25th, 2005 at 06:45 AM.

  5. #5

    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)

  6. #6
    Join Date
    Dec 2003
    Location
    Northern Ireland
    Posts
    1,362

    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
    Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. - Rich Cook


    0100 1101 0110 1001 0110 0011 0110 1000 0110 0001 0110 0101 0110 1100 0010 0000 0100 0101 0110 1100 0110 1100 0110 0101 0111 0010

  7. #7
    Join Date
    Feb 2005
    Location
    Denmark
    Posts
    742

    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

  8. #8
    Join Date
    Sep 2006
    Posts
    1

    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

  9. #9
    Join Date
    Feb 2007
    Posts
    1

    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

  10. #10
    Join Date
    Aug 2008
    Posts
    1

    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!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center