Microsoft KB Archive/314362

= How to create a contact by using ADO and ExOLEDB in Visual Basic .NET =

Article ID: 314362

Article Last Modified on 11/29/2007

-

APPLIES TO


 * Microsoft Visual Basic .NET 2003 Standard Edition
 * Microsoft Visual Basic .NET 2002 Standard Edition
 * Microsoft ActiveX Data Objects 2.5
 * Microsoft ActiveX Data Objects 2.6
 * Microsoft ActiveX Data Objects 2.7
 * Collaboration Data Objects for Exchange 2000
 * Microsoft Exchange 2000 Server Standard Edition

-



This article was previously published under Q314362



Caution ADO and ADO MD have not been fully tested in a Microsoft .NET Framework environment. They may cause intermittent issues, especially in service-based applications or in multithreaded applications. The techniques that are discussed in this article should only be used as a temporary measure during migration to ADO.NET. You should only use these techniques after you have conducted complete testing to make sure that there are no compatibility issues. Any issues that are caused by using ADO or ADO MD in this manner are unsupported. For more information, see the following article in the Microsoft Knowledge Base:

840667 You receive unexpected errors when using ADO and ADO MD in a .NET Framework application





For a Microsoft Visual C# .NET version of this article, see 310199.



SUMMARY
This article describes how to create a contact by using ActiveX Data Objects (ADO) and the Exchange OLE DB provider (ExOLEDB) in Microsoft Visual Basic .NET.



MORE INFORMATION
Note You must run the following code sample on a computer that is running Microsoft Exchange 2000 Server for the code sample to run correctly.

Sample
 Start Microsoft Visual Studio .NET. On the File menu, point to New, and then click Project. In the Visual Basic Projects list, click Console Application.

By default, Module1.vb is created. Add a reference to the Microsoft CDO for Exchange 2000 Library. To do so, follow these steps:  On the Project menu, click Add Reference.</li> Click the COM tab, locate the Microsoft CDO for Exchange 2000 Library, and then click Select.</li> In the Add References dialog box, click OK.</li> If you are prompted to generate wrappers for the libraries that you selected, click Yes.

Note In Microsoft .NET Framework applications, Collaboration Data Objects for Exchange 2000 (CDOEX) is supported only through a COM interop. For additional information, click the following article number to view the article in the Microsoft Knowledge Base:

813349 Support policy for Microsoft Exchange APIs with .NET Framework applications

</li></ol> </li> Repeat step 4 to add a reference to the Microsoft ActiveX Data Objects 2.5 Library.</li>  In the code window, replace the code with the following code: Module Module1

Sub Main ' TODO: Replace with your new item URL Dim sURL As String sURL = &quot;http://<ExchServer>/Exchange/<UserAlias>/Contacts/Test.eml&quot;

Dim oCn As ADODB.Connection = New ADODB.Connection Dim oRc As ADODB.Record = New ADODB.Record Dim oFields As ADODB.Fields

oCn.Provider = &quot;exoledb.datasource&quot;

oCn.Open(sURL, &quot;&quot;, &quot;&quot;, 0) If oCn.State = 1 Then Console.WriteLine(&quot;Good Connection&quot;) Else Console.WriteLine(&quot;Bad Connection&quot;) Return End If

oRc.Open(sURL, oCn, _        ADODB.ConnectModeEnum.adModeReadWrite, _         ADODB.RecordCreateOptionsEnum.adCreateNonCollection, _         ADODB.RecordOpenOptionsEnum.adOpenRecordUnspecified, _         &quot;&quot;, &quot;&quot;) If oRc.State = ADODB.ObjectStateEnum.adStateOpen Then Console.WriteLine(&quot;Record Open Success&quot;) Else Console.WriteLine(&quot;Record Open Fails&quot;) Return End If

oFields = oRc.Fields

oFields.Item(&quot;DAV:contentclass&quot;).Value = &quot;urn:content-classes:person&quot; oFields.Item(&quot;http://schemas.microsoft.com/exchange/outlookmessageclass&quot;).Value = &quot;IPM.Contact&quot; oFields.Item(&quot;urn:schemas:contacts:cn&quot;).Value = &quot;David Jones&quot; oFields.Item(&quot;urn:schemas:contacts:nickname&quot;).Value = &quot;Dave&quot; oFields.Item(&quot;urn:schemas:contacts:title&quot;).Value = &quot;Engineer&quot; oFields.Item(&quot;urn:schemas:contacts:department&quot;).Value = &quot;Microsoft&quot; oFields.Item(&quot;urn:schemas:contacts:email1&quot;).Value = &quot;someone@example.com&quot; oFields.Item(&quot;urn:schemas:contacts:fileas&quot;).Value = &quot;Dave Jones&quot;

oFields.Update

oCn = Nothing oRc = Nothing oFields = Nothing End Sub

End Module </li> Search for TODO in the code, and then modify the code for your environment.</li> Press F5 to build and to run the program.</li> Make sure that the contact was created.</li></ol>

<div class="references_section">