Updating Access table through OleDb works but Odbc give Concurrency Violation error?!

  • Thread starter Kenneth Baltrinic
  • Start date
K

Kenneth Baltrinic

I developed a web site using the OleDb namespace, but I need to use an DSN
on the production web server and thus must convert the app to use Odbc
namespace. I have done this and all works find except for updating one
table. The following code example demonstrates the situation exactly. In
the code below example setting iTestCase to 0 results in "Success" being
outputted. Setting it to non-zero results in the following exception:

System.Data.DBConcurrencyException: Concurrency violation: the UpdateCommand
affected 0 records.
at System.Data.Common.DbDataAdapter.Update(DataRow[] dataRows,
DataTableMapping tableMapping)
at System.Data.Common.DbDataAdapter.Update(DataTable dataTable)
at TestConsole.Class1.Main(String[] args) in
f:\jobs\psc\testconsole\class1.cs:line 48

Not the ONLY difference between the two test cases in the code is the use of
the OleDb vs Odbc namespace for data access. Moreover I have simplified
this situation to the extreme. The test table tblTest has only two columns.
TestID is an autonumber and TestData is a DateTime. The record in question
of course already exists, and both columns are non-null. Any help would be
greatly appreciated. As a workaround, is there perchance a way to access a
DSN through OleDb?

using System;
using System.IO;
using System.Data;
using System.Data.Odbc;
using System.Data.OleDb;

namespace TestConsole
{
class Class1
{
[STAThread]
static void Main(string[] args)
{
try
{
int iTestCase = 1;

DataTable dt = new DataTable("Test");

if(iTestCase==0)
{
OleDbConnection conn = new
OleDbConnection(@"Provider=Microsoft.Jet.OleDb.4.0;Data Source=F:\Server
Data\wwwroot\svsc\svsc.mdb");
OleDbDataAdapter da = new OleDbDataAdapter("SELECT * FROM tblTest WHERE
TestID=3", conn);
da.Fill(dt);

dt.Rows[0]["TestDate"] = DateTime.Now;

OleDbCommandBuilder cb = new OleDbCommandBuilder(da);

da.Update(dt);
}
else
{
OdbcConnection conn = new OdbcConnection(@"DSN=svsc");
OdbcDataAdapter da = new OdbcDataAdapter("SELECT * FROM tblTest WHERE
TestID=3", conn);
da.Fill(dt);

dt.Rows[0]["TestDate"] = DateTime.Now;

OdbcCommandBuilder cb = new OdbcCommandBuilder(da);

da.Update(dt);
}
Console.Write("Success");
}
catch(Exception ex)
{
Console.Write(ex.ToString());
}
Console.Read();
}
}
}
 
K

Kenneth Baltrinic

On the odd chance that my table was corrupt, I deleted it and recreated it.
Added a record by hand in access and reran the test. I discoved that Odbc
works just fine as long as you never used Oledb to update the table. Once
Oledb has done an update, Odbc cannot make updates. Moreover, this seems to
apply on a record by record basis. I can create a second new record and
again, its works find until OleDB is used to update it. Oledb reads appear
safe.

--Ken
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top