ASP.net Xero Invoice Error

Murray Brown
Murray Brown used Ask the Experts™
on
Hi. I am using the following VB.net code in ASP.net to create authenticate the user and then
create an invoice. The authentication works but then there is a problem with one line of code
  .Contact = pContact
The error is "Value of type 'Contact' cannot be converted into 'Contact'"

       
Imports Xero.Api.Core
Imports Xero.Api.Infrastructure.Interfaces
Imports Xero.Api.Example.Applications.Public
Imports Xero.Api.Infrastructure.OAuth
Imports System.Collections.Concurrent
Imports Xero.Api.Core.Model
Imports Xero.Api.Core.Model.Invoice





Public Class WebForm1

    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim myxeroapi As XeroCoreApi
        If IsPostBack = False Then
            'SO PAGE LOADS TWICE
            If Request.QueryString("oauth_verifier") Is Nothing Then
           
                myxeroapi = XeroAuthenticate(Me, False, "")
            Else

                myxeroapi = XeroAuthenticate(Me, True, "")
                If Not myxeroapi Is Nothing Then

                    Dim i As Integer = myxeroapi.Contacts.Find.Count 'Quck Test
                    Call oCreateInvoice(myxeroapi)


                End If
            End If
        End If
    End Sub



    Sub oCreateInvoice(ByVal myxeroapi As XeroCoreApi)

        Dim oEx As String

        Try
            Dim pContact As New Contact
            pContact.Name = "Test Company"

            'Create the list of lineitems to add to the invoice
            Dim LineItems As New List(Of LineItem)

            'This section should be repeated for multiple items...
            'in this example we just add another lineitem for simplicity
            Dim pLineItem As New LineItem
            With pLineItem
                .Description = "Services Rendered"
                .Quantity = 1
                .UnitAmount = "1234.56"
                .AccountCode = "4000"
            End With

            LineItems.Add(pLineItem)

            Dim pLineItem2 As New LineItem
            With pLineItem2
                .Description = "More Services Rendered"
                .Quantity = 1
                .UnitAmount = "7891.23"
                .AccountCode = "4000"
            End With

            LineItems.Add(pLineItem2)


            'Create the invoice
            Dim invoice As New Invoice
            '...and fill in it's properties
            With invoice
                '.Type = Types.InvoiceType.AccountsPayable
                .Type = Types.InvoiceType.AccountsReceivable
                .Contact = pContact

                .Date = Now
                .DueDate = Now.AddMonths(1)
                .Status = Xero.Api.Core.Model.Status.InvoiceStatus.Draft
                .LineItems = LineItems
            End With

            'and submit it to the Xero API
            myxeroapi.Create(invoice)

        Catch ex As Exception
            oEx = ex.Message
        End Try

    End Sub



    Protected Function XeroAuthenticate(inpage As Page, authpagereturnedauthentication As Boolean, incode As String) As XeroCoreApi
        ' This procedure will return w working XeroCoreApi if successful and nothing otherwise
        ' Set client secrets for general use and then load any override ones from the web.config file
        Dim myxeroconsumer As New Consumer("xxx", "yyy")
        Dim myxerouser = New ApiUser() With {.Name = "murb@yahoo.com"}

        ' If user defined ID and Secrets exist then use those instead
        If Not System.Configuration.ConfigurationManager.AppSettings("XeroAPIClientId") Is Nothing Then
            If Not System.Configuration.ConfigurationManager.AppSettings("XeroAPIClientSecret") Is Nothing Then
                If System.Configuration.ConfigurationManager.AppSettings("XeroAPIClientId").ToString <> "" Then
                    myxeroconsumer = New Consumer(System.Configuration.ConfigurationManager.AppSettings("XeroAPIClientId").ToString, System.Configuration.ConfigurationManager.AppSettings("XeroAPIClientSecret").ToString)
                End If
            End If
        End If

        ' Define Page Call Information
        Dim myxerocallbackurl = "http://localhost:62054/WebForm1.aspx"
        Dim myxeromemorystore = New MemoryAccessTokenStore()
        Dim myxerorequestTokenStore = New MemoryRequestTokenStore()
        Dim myxerobaseapiurl = "https://api.xero.com"

        ' Authenticate with Xero
        Dim myxeroauthenticator As New PublicMvcAuthenticator(myxerobaseapiurl, myxerobaseapiurl, myxerocallbackurl, myxeromemorystore, myxeroconsumer, myxerorequestTokenStore)

        If authpagereturnedauthentication = False Then
            ' Redirect to the authentication page
            Dim requri As String = myxeroauthenticator.GetRequestTokenAuthorizeUrl(myxerouser.Name)
            If requri <> "" Then Response.Redirect(requri, True)
            Return Nothing
        Else
            ' Validate token using querystrings that have been returned from the authentication process
            Dim myxerotoken As IToken = myxeroauthenticator.RetrieveAndStoreAccessToken(myxerouser.Name, Request.QueryString("oauth_token").ToString, Request.QueryString("oauth_verifier").ToString, Request.QueryString("org").ToString)
            If Not myxerotoken Is Nothing Then
                Dim myxeroapi As New XeroCoreApi("https://api.xero.com", myxeroauthenticator, myxeroconsumer, myxerouser)
                Return myxeroapi
            Else
                Return Nothing
            End If
        End If
    End Function
End Class
Public Class MemoryAccessTokenStore
    Implements ITokenStore
    Private Shared ReadOnly _tokens As IDictionary(Of String, IToken) = New ConcurrentDictionary(Of String, IToken)()
    Protected Function Find(userId As String) As IToken Implements ITokenStore.Find
        Dim token As IToken = Nothing
        If String.IsNullOrWhiteSpace(userId) Then
        Else
            _tokens.TryGetValue(userId, token)
        End If
        Return token
    End Function
    Protected Sub Add(token As IToken) Implements ITokenStore.Add
        _tokens(token.UserId) = token
    End Sub
    Protected Sub Delete(token As IToken) Implements ITokenStore.Delete
        If _tokens.ContainsKey(token.UserId) Then
            _tokens.Remove(token.UserId)
        End If
    End Sub
End Class
Public Class MemoryRequestTokenStore
    Implements ITokenStore
    Private Shared ReadOnly _tokens As IDictionary(Of String, IToken) = New ConcurrentDictionary(Of String, IToken)()
    Protected Function Find(userId As String) As IToken Implements ITokenStore.Find
        If String.IsNullOrWhiteSpace(userId) Then
            Return Nothing
        End If

        Dim token As IToken = Nothing

        _tokens.TryGetValue(userId, token)
        Return token
    End Function
    Protected Sub Add(token As IToken) Implements ITokenStore.Add
        _tokens(token.UserId) = token
    End Sub
    Protected Sub Delete(token As IToken) Implements ITokenStore.Delete
        If _tokens.ContainsKey(token.UserId) Then
            _tokens.Remove(token.UserId)
        End If
    End Sub






End Class

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Commented:
You need to edit one line only:

Dim pContact As New Xero.Api.Core.Model.Contact

(because Web UI has its own Contact)

So keep watching this for ALL new Contacts you're making in this WHOLE project to adjust accordingly.
Partha MandayamTechnical Director

Commented:
Are .Contact and pContact of the same type?
This error because a direct conversion is not possible between the two
See this article for more information
https://docs.microsoft.com/en-us/dotnet/visual-basic/misc/bc30311
Murray BrownASP.net/VSTO Developer

Author

Commented:
Fantastic answer! Thank you very much!

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial