Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Recordset AddNew() in C#

499 views
Skip to first unread message

Markus Frischholz

unread,
Apr 14, 2005, 10:14:07 AM4/14/05
to
I'm trying to add a new record to a Recordset object using the AddNew() method.

The AddNew() method uses the parameters "FieldList" and "Values". These
parameters are optional in VB but mandatory in C#.

If I pass null for both I get the following error:

System.Runtime.InteropServices.COMException (0x800A0CC1):
Item cannot be found in the collection corresponding to the requested name
or ordinal.

How can I get around this problem? Any help appreciated.

-Markus Frischholz


William (Bill) Vaughn

unread,
Apr 15, 2005, 1:05:20 PM4/15/05
to
Might I suggest that you switch to VB.NET? ;)
You'll get better results if you query the
microsoft.public.dotnetframework.adonet list...
IIRC there is some sort of placeholder you can use in C# to deal with this
issue.

--
____________________________________
William (Bill) Vaughn
Author, Mentor, Consultant
Microsoft MVP
www.betav.com/blog/billva
www.betav.com
Please reply only to the newsgroup so that others can benefit.
This posting is provided "AS IS" with no warranties, and confers no rights.
__________________________________

"Markus Frischholz" <MarkusFr...@discussions.microsoft.com> wrote in
message news:047CA015-2B28-4129...@microsoft.com...

Markus Frischholz

unread,
Apr 15, 2005, 1:41:02 PM4/15/05
to
Unfortunately I have to interface with legacy code and I have no choice. :(

I was able to work around - but I still wonder how I can deal with optional
parameters in C#. I will try a different newsgroup.

Thanks for your help
-Markus

Stephen Howe

unread,
Apr 15, 2005, 2:01:12 PM4/15/05
to
> How can I get around this problem? Any help appreciated.

I don't know C# whatsoever, but I think the Microsoft advice that is
applicable to ADO for VC++ should also do for C#, you just need to
"translate" it from C++ to C#.

You want
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/ado270/htm/mdhowhowvcusersshouldreadadodocumentation.asp?frame=true

In particular the section titled "Missing and Default parameters"

Now in the header file generated by #import I see

HRESULT AddNew (
const _variant_t & FieldList = vtMissing,
const _variant_t & Values = vtMissing );

vtMissing is a global variant generated by #import (and is mentioned in this
document)

You can create equivalents of vtMissing in C# making sure it is has a value
of DISP_E_PARAMNOTFOUND and a type of VT_ERROR and pass that to AddNew as
the first 2 parameters.

Because of the header file generated by #import for VC++, and because
default parameters are supported, I find myself just doing

pRst->AddNew(); // default parameters in C++ are supplying 2
vtMissing's for 2 parameters.

Now I don't really know what support Microsoft has in place for C# and
classic (old-style) ADO. It may well be not much as Microsoft probably
designed C# to work with ADO.NET

I would read the above document carefully.
I am reasonably certain that what you do for C++, you basically adapt for
C#.
You might want to search around from links under
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/ado270/htm/mdmscusingadowithmicrosoftvisualc.asp?frame=true

Hope this helps

Stephen Howe


Markus Frischholz

unread,
Apr 15, 2005, 2:23:04 PM4/15/05
to
Thanks for your help.

Actually I found a solution which is close to your example in C++:

object nil = Type.Missing;
rs.AddNew(nil, nil);

This also works with the Update() method which also takes two optional
parameters.

Thanks again,
-Markus Frischholz

0 new messages