VB.NET Samples Connecting to Couchbase

I’m new to Couchbase and am trying to get up to speed using the .NET SDK and VB.NET. I am unable to connect to the database. Here is my example:

Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim cbCluster As New Cluster

    Dim doc As New cbDoc
    With doc
        .SSN = "123-45-6789"
        .First = "John"
        .Middle = ""
        .Last = "Smith"
        .Addr = "123 Main Street"
        .City = "Brooklyn"
        .State = "NY"
        .ZipCode = "11211"
    End With

    cbCluster.OpenBucket()

End Sub

End Class

Any help would be greatly appreciated.

1 Like

@todd_bryant

The example you provided will only work connecting to a copy of Couchbase on your local machine with a bucket named “default”.

Your best bet is to do the following:

  1. Add configuration on how to connect to your cluster to your app.config file
  2. During load, call ClusterHelper.Initialize(“couchbase/couchbaseClient”). The string might vary depending on the section names you used in your configuration file.
  3. Use ClusterHelper.GetBucket(“myBucketName”) to get the bucket you want to use. Note that you should not dispose this when you are done, it is returning a singleton shared across multiple calls to GetBucket.
  4. Call ClusterHelper.Close when your application is closed.

An example XML configuration can be found here:
http://developer.couchbase.com/documentation/server/4.1/sdks/dotnet-2.2/configuring-the-client.html

And an example of using ClusterHelper can be found here, you’ll just want to use the configuration section name instead of making the ClientConfiguration in memory:
http://developer.couchbase.com/documentation/server/4.1/sdks/dotnet-2.2/cluster-helper.html

Brant

Thanks Brant. I modified the code as you specified, but how do I post (insert) data ?

Imports Couchbase
Imports Couchbase.Core
Imports Couchbase.N1QL
Imports Couchbase.CouchbaseBucket

Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
’Dim cbCluster As New Cluster
’Dim config = New Couchbase.Configuration()

    ClusterHelper.Initialize("couchbase/couchbaseClient")
    ClusterHelper.GetBucket("Default")

    Dim doc As New cbDoc
    With doc
        .SSN = "123-45-6789"
        .First = "John"
        .Middle = ""
        .Last = "Smith"
        .Addr = "123 Main Street"
        .City = "Brooklyn"
        .State = "NY"
        .ZipCode = "11211"
    End With



End Sub

End Class

@todd_bryant

Change the second line to:

Dim bucket = ClusterHelper.GetBucket("Default")

Then change your document creation to:

Dim doc = New Document(Of cbDoc)() With {
    .Id = "my_unique_key",
    .Content = New cbDoc() With {
        .SSN = "123-45-6789",
        .First = "John",
        .Middle = "",
        .Last = "Smith",
        .Addr = "123 Main Street",
        .City = "Brooklyn",
        .State = "NY",
        .ZipCode = "11211"
    }
}

Dim result = bucket.Insert(doc)

' Check for result.Success, etc

Updates are via .Update, and .Upsert will do an insert or update. So forth and so on.

Brant

Hi Brant, this throws an error

{“Could not load file or assembly ‘Common.Logging.Core, Version=3.3.1.0, Culture=neutral, PublicKeyToken=af08829b84f0328e’ or one of its dependencies. The located assembly’s manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)”:“Common.Logging.Core, Version=3.3.1.0, Culture=neutral, PublicKeyToken=af08829b84f0328e”}

Here is the full code. What am I doing wrong?

Imports System.IO
Imports Couchbase
Imports Couchbase.Core
Imports Couchbase.N1QL
Imports Couchbase.CouchbaseBucket

Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim g As Guid
g = Guid.NewGuid()

    ClusterHelper.Initialize()
    Dim bucket = ClusterHelper.GetBucket("Default")

    Dim doc = New Document(Of cbDoc)() With {
        .Id = g.ToString,
        .Content = New cbDoc() With {
        .SSN = "123-45-6789",
        .First = "John",
        .Middle = "",
        .Last = "Smith",
        .Addr = "123 Main Street",
        .City = "Brooklyn",
        .State = "NY",
        .ZipCode = "11211"
    }
    }

    Dim result = bucket.Upsert(doc)






End Sub

End Class

@todd_bryant did you add CouchbaseNetClient with NuGet or some other way? If you installed with NuGet, it should automatically add the Common.Logging libraries as well. If you didn’t use NuGet, you may be missing those Common.Logging libraries.

Open the NuGet package manager and update the Common.Logging dependencies. This should resolve the issue. Alternatively, remove the Common.Logging and associated dependencies and remove the related sections from the App.Config file.

-Jeff