Friday, February 24, 2012

C# with ADO 2.8: Receiving "Current Recordset does not support updating"

Hi All,

I was using VB6 to access a MS SQL Server database. The code worked and works fine. I then decided to migrate the code to C#.Net 2005 using ADO 2.8 (not ADO.Net). Doing that yields with the same exact code the error message, "Current Recordset does not support updating".

I did a whole bunch of Google searches and didn't see anything useful. Mainly the advice from Microsoft and others is to make sure the mode on the connection string is set to "ReadWrite", as the default is "Read Only" and to make sure to set the lock type to either optimistic or pessimistic. Still others said that the code should set the CursorLocation property of the recordset.

I can safely say that I have been setting the mode to "Read/Write" since the start and have played around with the lock type, cursor location, and open method. Nothing works on C#, BUT VB6 is so totally happy with everything.

The provider works fine, as VB6 works fine, and the lock type is also fine, so therefore the built in suggestions do not apply.

My code is:

// Connection string template. Filled in properly in real code.
strConnect = "Server={0};Database={1};"

// Set the connection properties.
this.SQLConnection.ConnectionString = strConnect;
this.SQLConnection.Provider = "SQLOLEDB";
this.SQLConnection.Mode = adModeReadWrite;

// Open the connection.
this.SQLConnection.Open(strConnect, strUserName, strPassword, -1);

=================

// Create the ADO objects needed.
dbRSAdd = new Recordset();

// Open the recordset.
dbRSAdd.Open(strTable, dbCatalog.ActiveConnection, CursorTypeEnum.adOpenDynamic, LockTypeEnum.adLockOptimistic, (int)CommandTypeEnum.adCmdTable);

// Cycle through each record to add.
dbRS.MoveFirst();
for (lRecord = 0; lRecord < dbRS.RecordCount; lRecord++)
{
// Add a new record.
dbRS.AddNew(System.Reflection.Missing.Value, System.Reflection.Missing.Value);

...
}

// NOTE: The code crashes with the call to 'AddNew'.

Any advice?Hi All,

(Head between my legs.) I can't tell you how long I looked at the code only to look at the code on this post and immediately see the problem. I had two record sets. The first record set (dbRS) is read only. I made the stupid mistake of not putting dbRSAdd with the AddNew. VB6 uses a 'With' statement, whereas C# does not support a 'With' statement. My problem was that I wasn't careful in copying and pasting, when filling in the prefix before the '.'.

Oops!

No comments:

Post a Comment