Visual Basic 2008 Express - Getting Started

Discussion in 'Developers Forum' started by greenfrog, Nov 14, 2009.

  1. greenfrog New Member

    I am no expert with Visual Basic, and it took me a few days of trial and error, (and google) to get somewhere with the KashFlow API, so I thought I should record the following here, in case it is of use to others making a start.


    download Visual Basic Express 2008 from here:

    http://www.microsoft.com/express/vb/

    Install Visual Basic Express 2008.

    Set up a free KashFlow account to play with for 60 days.

    Go to 'Settings', then 'API Settings'.
    Enable the API for your KashFlow account.
    Either remove the tick from 'Only Allow specific IP addresses ...' or for more security, find your IP address (http://whatismyipaddress.com/) and add it to the list of allowed IP addresses.

    Note that your IP address will probably change each time you power up your PC, so you will have to repeat this step.

    Create a Customer in KashFlow with a Customer Code of 'CUST01'.

    Start Visual Basic Express 2008.

    Select 'File', 'New Project', then choose a Windows Forms Application, enter a name for your project, and press the 'Finish' button.

    A small square blank window will be seen. This is the window you will be constructing. To the left is a vertical tab labelled 'Toolbox'. Click on this, and it expands to show things you can drag onto your window. First, drag a button onto you new window, then drag a label onto your window, below the button. You can drag these around to line them up neatly in the middle of the window.

    From the top menu, select 'Project', 'Add Service Reference'. Type into the 'Address' box the URL:

    https://securedwebapp.com/api/service.asmx

    Then, press the 'Go' button. When VB finds the KashFlow API, press the 'OK' button.

    Back at your new window, double click on your new button. A block of code is displayed, with the cursor in the middle. Enter the following code at the cursor position:

    Code:
            Dim myCustomer As New ServiceReference1.Customer
    
            Dim bind As New ServiceModel.BasicHttpBinding
            bind.Security.Mode = ServiceModel.BasicHttpSecurityMode.Transport
    
            Dim endpoint As New ServiceModel.EndpointAddress ("https://securedwebapp.com/api/service.asmx")
    
            Dim wsService As New ServiceReference1.KashFlowAPISoapClient(bind, endpoint)
    
            Dim status As String = ""
            Dim statusDetail As String = ""
    
            Try
                Label1.Text = "calling"
                myCustomer = wsService.GetCustomer(status, statusDetail, "kfuser", "kfpassword", "CUST01")
                Label1.Text = myCustomer.Name
            Catch
                Label1.Text = "Failed. " + status + " " + statusDetail
            End Try
    Replace "kfuser" and "kfpassword" with the username and password you chose for your KashFlow account.

    Save the form, using 'File', 'Save Form1.vb' from the top menu. Then select the menu 'Debug', and 'Start Debugging'.

    Your new window will appear. Single click on your button, and the text 'Label1' should get replaced by the name of the customer that you created in KashFlow.

    Close your window application by clicking the red X button at the top right of the window.

    Now write the rest of your code ...

    Ian Morgan
    Hampshire.
  2. DuaneJackson Administrator

    Excellent. Thanks for sharing
  3. David Spink New Member

    Visual Basic 2010 Express API connection

    Thanks for the code Ian - I've been struggling with how to get Visual Basic 2010 Express to connect to the KashFlow API until I saw your example!

    It does look to me that the error handling method in your example won't actually catch a KashFlow processing error however. If the API can't process your request (say for example because the username or password is wrong) it doesn't actually throw an exception, it just sets the return variable "status" to "NO" which then needs to be checked. An exception would be thrown if, say, there was no internet connection - but this wouldn't be reflected in the "status" and "statusDetail" variables.

    Working from your example I've come up with the following code. I've changed the name of the service reference from "ServiceReference1" to "KashFlow" (when adding the reference you can rename from the default suggestion) just to make it look nicer. This code catches a KashFlow processing error (like invalid username/password) and situations where the API can't be contacted at all (like where there is no internet connection).

    My example uses "GetCustomers()" and then displays a message showing how many there are.

    Hopefully this will be useful for anyone else struggling with this!

    Code:
     'KashFlow username and password
            Const KFUsername = "kfusername" : Const KFPassword = "kfpassword"
            Const KFServiceAddress = "https://securedwebapp.com/api/service.asmx"
    
            Dim bind As New ServiceModel.BasicHttpBinding
            bind.MaxReceivedMessageSize = 100000 'Default is 65535 which isn't enough for large amounts of data
            bind.Security.Mode = ServiceModel.BasicHttpSecurityMode.Transport
    
            Dim myStatus As String = ""
            Dim myError As String = ""
    
            Dim myKF As New KashFlow.KashFlowAPISoapClient(bind, New ServiceModel.EndpointAddress(KFServiceAddress))
    
            'Speak to KashFlow..
            Try
                Dim myCustomers() As KashFlow.Customer = myKF.GetCustomers(myStatus, myError, KFUsername, KFPassword)
    
                If myStatus = "OK" Then
                    MsgBox(String.Format("You have {0} customers!", myCustomers.Count))
                Else
                    MsgBox(String.Format("KashFlow problem: {0}", myError))
                End If
            Catch commProblem As ServiceModel.CommunicationException
                MsgBox(String.Format("Communication problem: {0} (Inner Exception: {1})", commProblem.Message, commProblem.InnerException.Message))
            End Try

Share This Page