Call to LotusSession.GetDatabase works in VB but not in C#
I have some old VB code to send mails using Lotus Notes that works, I have re-written it into C#, but it behaves differently:
NotesSession = CreateObject("Notes.Notessession") NotesDb = NotesSession.GetDatabase("", "")
_notesSession = new NotesSession(); _notesSession.Initialize(passwordString); _notesDatabase = _notesSession.GetDatabase( "", "");
First of in C# I need to Initialize the NotesSession with a password, and secondly it will not accept empty string parameters at runtime. Exception is thrown: "A database name must be provided".
In both VB and C# I refer to the same COM : Lotus Domino Objects
I need to be able to call the GetDatabase without specifying the server and database file.
Thanks in advance.
Solution (Thanks guys):
dynamic _notesSession = Activator.CreateInstance(Type.GetTypeFromProgID("Notes.NotesSession")); _notesDatabase = _notesSession.GetDatabase("", "");
This way you have no intellisense but all properties and methods can be found here
When you create a new instance of the NoteSession type in C# using the new keyword, it will use the COM-interop dll that was referenced by the project at build-time. That is not exactly the same thing as calling CreateObject, which requires no interop dll. The closer equivalent in C# would be:
Type t = Type.GetTypeFromProgID("Notes.Notessession"); _notesSession = Activator.CreateInstance(t);
Or, if you really need to do the exact same thing, you could always add a reference to the Microsoft.VisualBasic.dll library and then call the Microsoft.VisualBasic.Interaction.CreateObject method from C#.
As Richard pointed out in the comments below, the likely reason why you are seeing a difference in behavior is because you are creating two different types of objects. Presumably, when you call new NotesSession in C#, it is using the NotesSession class from the Lotus namespace, rather than the one in the Notes namespace.