none
Access Violation Exception in Windows 7 and Windows Vista RRS feed

  • Question

  • I have a function that looks like:

     

      [System.Runtime.ExceptionServices.HandleProcessCorruptedStateExceptions()]
      public static DataTable GetData(
       string connection,
       string sql,
       string tableName)
      {
       DataTable dt = null;
    
       if (string.IsNullOrEmpty(connection) ||
        string.IsNullOrEmpty(sql) ||
        string.IsNullOrEmpty(tableName))
        return dt;
    
       // initialize the dataset
       DataSet ds = null;
       OleDbConnection cnn = null;
       try
       {
        ds = new DataSet();
    
        cnn = new OleDbConnection(connection);
        cnn.Open();
        OleDbCommand command = cnn.CreateCommand();
        command.CommandText = sql;
        command.Connection = cnn;
    
        using (OleDbDataAdapter adapter = new OleDbDataAdapter(command))
        {
         // fill the data set with whatever is specified to be read
         adapter.Fill(ds);
         // add the informaiton into the table.
         if (ds.Tables.Count > 0)
         {
          ds.Tables[0].TableName = tableName;
          dt = ds.Tables[0].Copy();
         }
        }
       }
       catch (AccessViolationException ex)
       {
        MessageBox.Show(
         Properties.Resources.ApplicationFailure_Message,
         Properties.Resources.ApplicationFailure_Title,
         MessageBoxButtons.OK,
         MessageBoxIcon.Stop);
        Application.Exit();
       }
       finally
       {
        if (cnn != null && cnn.State == ConnectionState.Open)
         cnn.Close();
        else if (cnn != null)
         cnn.Dispose();
        cnn = null;
        if (ds != null)
         ds.Dispose();
        ds = null;
       }
    
       return dt;
      }
    
    

    When it gets to the adapter.Fill(ds); line, I sometimes get an Access Violation exception.  Does not always happen, I could access the same files several times and then one time I get an access violation.

    The files I am working with are dBase IV files.  I am using the OLEDB connection strings for them that are well documented.  I have a call stack of:

       at System.Data.Common.UnsafeNativeMethods.ICommandText.Execute(IntPtr pUnkOuter, Guid& riid, tagDBPARAMS pDBParams, IntPtr& pcRowsAffected, Object& ppRowset)
       at System.Data.OleDb.OleDbCommand.ExecuteCommandTextForSingleResult(tagDBPARAMS dbParams, Object& executeResult)
       at System.Data.OleDb.OleDbCommand.ExecuteCommandText(Object& executeResult)
       at System.Data.OleDb.OleDbCommand.ExecuteCommand(CommandBehavior behavior, Object& executeResult)
       at System.Data.OleDb.OleDbCommand.ExecuteReaderInternal(CommandBehavior behavior, String method)
       at System.Data.OleDb.OleDbCommand.ExecuteReader(CommandBehavior behavior)
       at System.Data.OleDb.OleDbCommand.System.Data.IDbCommand.ExecuteReader(CommandBehavior behavior)
       at System.Data.Common.DbDataAdapter.FillInternal(DataSet dataset, DataTable[] datatables, Int32 startRecord, Int32 maxRecords, String srcTable, IDbCommand command, CommandBehavior behavior)
       at System.Data.Common.DbDataAdapter.Fill(DataSet dataSet, Int32 startRecord, Int32 maxRecords, String srcTable, IDbCommand command, CommandBehavior behavior)
       at System.Data.Common.DbDataAdapter.Fill(DataSet dataSet)
       at CMSCDQ.CMSCDQDataMethods.GetData(String connection, String sql, String tableName)

     

    It happens with a SQL statement that looks like:

     

    SELECT * FROM SYSPROF1
    

     

     

    The Exception never happens on the first call to this routine, but it could be the second or the 50th, I never know.  This is the only routine in my application that retrieves data and I use it for accessing dBase files and MS Access files.  The exception only ever happens accessing a dBase file.  I even took out any access to the MS Access file, thinking that something between accessing the two types of files was causing the problem, but it made no difference. 

    It happens only on Windows Vista and Windows 7.  I have not tried it on Windows Server 2008.  The program is compiled in x86 mode in the .Net 4.0 Client framework.  I have another application having the same issue that is built in x86 mode in .NET 3.5.  If the application is set to run in Windows XP compatibility mode in the shortcut then I do not receive the exception.  I have several applications that have been previously extensively tested in Windows Vista and Windows 7 and worked fine before and recently (in the last couple of months) started having this issue.  I don't know if there was some Windows Update that broke something in the WDAC for this.

    I would love to not have to access a DBF file, but I have customers to support for at least a year or two until all of my apps have been upgraded to SQL Server.. 


    David Parvin
    Friday, July 22, 2011 3:14 PM

All replies

  • I think you should using connection--> cnn = new OleDbConnection(connection);

    just Fill dt(DataSet), use dataTable.Copy() is not a good way.

     >> OleDbCommand command = cnn.CreateCommand();
     >>   command.CommandText = sql;
    >>    command.Connection = cnn;

    the command is created by Connection, so we shouldn't set it again.


    I am fish.
    Monday, July 25, 2011 1:28 PM
  • Thanks for pointing out the unneeded connection assignment.  I took that line out and the issue is still there. 

    The reason I fill into a Dataset is I never know when a programmer is going to do something like put two statements into the same string.  This way if they do, it puts multiple tables into the Dataset and returns only the first one.  Because this is a general routine and not all that specific, I try to error on the side of caution with this. 

    I also changed the code so it filled the dt varible instead and I get the same error with the following call stack:

       at System.Data.Common.UnsafeNativeMethods.ICommandText.Execute(IntPtr pUnkOuter, Guid& riid, tagDBPARAMS pDBParams, IntPtr& pcRowsAffected, Object& ppRowset)
       at System.Data.OleDb.OleDbCommand.ExecuteCommandTextForSingleResult(tagDBPARAMS dbParams, Object& executeResult)
       at System.Data.OleDb.OleDbCommand.ExecuteCommandText(Object& executeResult)
       at System.Data.OleDb.OleDbCommand.ExecuteCommand(CommandBehavior behavior, Object& executeResult)
       at System.Data.OleDb.OleDbCommand.ExecuteReaderInternal(CommandBehavior behavior, String method)
       at System.Data.OleDb.OleDbCommand.ExecuteReader(CommandBehavior behavior)
       at System.Data.OleDb.OleDbCommand.System.Data.IDbCommand.ExecuteReader(CommandBehavior behavior)
       at System.Data.Common.DbDataAdapter.FillInternal(DataSet dataset, DataTable[] datatables, Int32 startRecord, Int32 maxRecords, String srcTable, IDbCommand command, CommandBehavior behavior)
       at System.Data.Common.DbDataAdapter.Fill(DataTable[] dataTables, Int32 startRecord, Int32 maxRecords, IDbCommand command, CommandBehavior behavior)
       at System.Data.Common.DbDataAdapter.Fill(DataTable dataTable)
       at CMSCDQ.CMSCDQDataMethods.GetData(String connection, String sql, String tableName)

     

     


    David Parvin
    Monday, July 25, 2011 4:34 PM
  • Hi David,

    Would you please give us your CommandText, or ping me: Alan

    Have a nice day.


    Alan Chen[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Thursday, July 28, 2011 7:38 AM
    Moderator
  • Every one of the calls that are failing are doing it with a simple SELECT * FROM <Database table name>.  I can work up a sample app that fails with files to go with it if needed.
    David Parvin
    Friday, July 29, 2011 8:51 PM
  • Hi David,

    I get the same AccessViolationException Error, with a simple SELECT * FROM <Table>. My app isn't complex and for me it does it every time.

    I create a test project, with only one form that Query the database, and add the result to a listbox.

    It give me the error at : myDA.Fill(myDataSet, "MyTable")

    If you want my test project let me know.

    Thanks,

    Maxime

     

    Imports System.Data.SqlServerCe
    
    Public Class Form1
     Dim con As SqlCeConnection = New SqlCeConnection("Data Source=|DataDirectory|\Database1.sdf")
     Dim cmd As SqlCeCommand
     Dim myDA As SqlCeDataAdapter
     Dim myDataSet As DataSet
    
     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
      cmd = New SqlCeCommand("SELECT * FROM test", con)
      If con.State = ConnectionState.Closed Then con.Open()
      myDA = New SqlCeDataAdapter(cmd)
      myDataSet = New DataSet()
      myDA.Fill(myDataSet, "MyTable")
      Dim QueryResult = myDataSet.Tables("MyTable").DefaultView
    
      For Each Query As DataRowView In QueryResult
       ListBox1.Items.Add(Query(1).ToString)
      Next
     End Sub
    End Class
    


     


    Saturday, July 30, 2011 2:39 PM
  • I also have another test app that was created 2 years ago in XP, that work on the same computer with the same code for the connection to the database...
    Saturday, July 30, 2011 2:43 PM
  • I found a solution. In the Project Properties, Compile Tab, and Advanced Compile Options, I changed the Target CPU to x86 instead of anyCPU.

    I don't know if it's normal... I'm on Windows 7 Pro x64 SP1.

    Maxime

    Sunday, July 31, 2011 5:22 PM
  • Yep.  Mine has been failing part of the time with the x86 setting.  It has been compiled with x86 the whole time because I know that the x64 version of the Data components in Windows will not access a dBase file.  I have never had the first call to get data from a dBase table fail.  It is usually the 4th, 5th or nth access to one.
    David Parvin
    Monday, August 1, 2011 2:17 PM
  • Hi. I have the same intermittent error from our website when it runs a certain select. I've found another possible reference to the issue here: http://www.dotnetmonster.com/Uwe/Forum.aspx/vs-net-general/10965/Attempted-to-read-or-write-protected-memory-vb-Net-2005 and was wondering whether this might be the cause of both our problems. DBIV is closely related to Foxpro isn't it? Is this something you've already looked at? I know the code we're using doesn't specify MULTILOCKS ON or OFF at present. As I'm not really a web developer it'll be a very 'suck it and see' approach for me, so any input you may have will be gratefully received. Regards Graham
    Thursday, October 6, 2011 11:39 AM