0

I use visual studio 2013 to create a winform app with tabaleadapter to connect with database. My problem is insert query throw a unhandled exception when it inside a loop statement.

This code work:

abcTableAdapter.InsertQuery(param1, param2, param3);

But this code throw the exception:

for (int i = 1; i < 5; i++)
{
  abcTableAdapter.InsertQuery(param1, param2, param3);
}

The exception is:

An unhandled exception of type 'System.AccessViolationException' occurred in myApp.exe

Additional information: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.

I also have same problem with foreach statement.

What is wrong with my code? I googled whole day, but I cannot see any solution.

Brian Rasmussen
  • 114,645
  • 34
  • 221
  • 317
Nghi Ho
  • 463
  • 1
  • 6
  • 18
  • The reason is that you are using the same connection when you are inserting. This is causing a violation error because you are inserting on the same thread. You will need to dispose of the memory (connection) and restart it. This might help you out:(http://blogs.msdn.com/b/sqlservercompact/archive/2009/05/06/troubleshooting-access-violation-exception-while-using-sql-server-compact-database-with-ado-net-provider.aspx) – Jason Cidras Mar 11 '15 at 16:26
  • @Jason Cidras, thanks for your quick reply. I create a new class with the insert method inside. In for statement, I initialize the class and call the method. It seem to be not the best solution, but it works. – Nghi Ho Mar 12 '15 at 00:31

0 Answers0