Search code examples
vb.netvbaodata

Connect to an Odata Service in Visual Studio


I am trying to create an application on visual studio using a windows form that connects to an Odata Service. I have looked on the MSDN website for help. Here is what I found.


In Solution Explorer, click the Data Sources node.

On the Project menu, click Add Data Source….

The Attach Data Source Wizard appears.

On the Choose a Data Source Type page, click OData Service, and then click Next.

In the OData Source Address field on the Enter Connection Information page, type the full URL address for the service (for example, http://services.odata.org/Northwind/Northwind.svc/).

Click Next.

Under Login Information, specify the type of authentication that the service uses.

If you specify Other Credentials, enter a valid User name and Password, which are typically supplied by the owner of the service. Click Next.

On the Choose your Entities page, specify the entities that you want to use in your LightSwitch application, and then click Finish.


I'm unable to find the Data Sources Node on the project menu to open the Attach Data Source Wizard.


Solution

  • I assume you are trying to consume the Northwind odata Service in a VB.Net Windows Form Application. In that case you can do the below.

    Open Visual Studio

    Click File -> New Project -> Visual Basic -> Windows Forms Application

    Click Project -> Add New Data Source enter image description here

    Choose Data Type as Service

    enter image description here

    Add the address as http://services.odata.org/Northwind/Northwind.svc/ , Click GO and select NorthwindEntities and Click 'OK'. Thats it. You have now added the service reference and can now consume it.

    enter image description here

    Double click the Windows Form1.vb and add the below code. The below code queries the Odata service to get the all the Territory Description from the Territories Collection, and adds them to the list box

    Imports WindowsApplication1.ServiceReference1
    
    Public Class Form1
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Dim northwindUri As Uri = New Uri("http://services.odata.org/Northwind/Northwind.svc/", UriKind.Absolute)
    
    
            ' Create a new instance of the typed DataServiceContext.
            Dim context As NorthwindEntities = New NorthwindEntities(northwindUri)
    
            Dim query = From territory In context.Territories
                        Select territory
    
            Try
                For Each t As Territory In query
                    ListBox1.Items.Add(t.TerritoryDescription)
                Next
    
            Catch ex As Exception
    
            End Try
        End Sub
    End Class
    

    Now when you run the form, you should see the below.

    enter image description here