how validate VAT with WSDL SOAP Service with VB2019 ?

is sro 20 Reputation points
2023-07-13T08:13:26.14+00:00

i like add few lines in my program that validate VAT in Europe.

I use Visual Studio Community 2019, Windows form, Net Framework 4.5.2,

A// I created new Solution/Project with form1

B// i defined design

textbox1 - country / for example "SK"

textbox2 - numberpart of VAT /for example 2020332105/

Textbox3 - for output from webservice

Button1 - for handle to validate VAT

C// i added service reference. Right click on project - ADD - Service Reference

i put link wsdl for reference

[http://ec.europa.eu/taxation_customs/vies/checkVatTestService.wsdl]

i clicked OK and i have now in project added Connected Services -ServiceReferences1

D// and now i want to write some code for sending request and to get validation code but i didnt find any good help on web for VB. On the net is many articles in C, python but nothing useful a really simple for Visual Basic. Can you help to write code for sending CheckVat metode to this webservice ?

thx

VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,668 questions
0 comments No comments
{count} votes

Accepted answer
  1. Jiachen Li-MSFT 29,261 Reputation points Microsoft Vendor
    2023-07-13T09:28:56.8633333+00:00

    Hi @is sro ,

    Please check if the following code works for you.

            Dim client As New checkVatPortTypeClient()
    
            Dim countryCode As String = TextBox1.Text.Trim()
            Dim vatNumber As String = TextBox2.Text.Trim()
            Dim valid As Boolean
            Dim name As String
            Dim address As String
    
            Dim requestDate = client.checkVat(countryCode, vatNumber, valid, name, address)
    
            If valid Then
                Console.WriteLine("VAT number is valid.")
                Console.WriteLine("name: " + name + "; address: " + address + ".")
            Else
                Console.WriteLine("VAT number is invalid.")
            End If
    
            client.Close()
    

    Best Regards.

    Jiachen Li


    If the answer is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


4 additional answers

Sort by: Most helpful
  1. is sro 20 Reputation points
    2023-07-18T07:40:22.5733333+00:00

    Dear Jiachen,

    thx for your answer but i dont know what class i have to replace..

    I got namespace ServiceReference1 and in object browser i see:

    project: wsdl_vat

    namespace: wsdl_vat.ServiceReference1

    Classes:

    Public Class checkVatApproxRequest

    Public Class checkVatApproxResponse

    Public Interface checkVatPortType

    Public Class checkVatRequest

    Public Class checkVatResponse

    I changed class e.g. : ServiceReference1.checkVatApproxRequest()

    but i got warning - ValidateVATNumber is not member of class...

    and i didnt find class with this member ..

    IS

    0 comments No comments

  2. is sro 20 Reputation points
    2023-07-20T09:20:48.2233333+00:00

    Dear Jiachen,

    thx for your help. I changed a little code because my VB had too many red lines , i had to define DIM with ServiceReference1, now i dont have red lines, but it still is calling exception when i run the program.

    i believe you know what to change

    Lubomir

    more info;

    It stops at line 6 : request.Body.countryCode = TextBox1.Text.Trim()

    Write Exception:

    System.NullReferenceException: 'Object reference not set to an instance of an object.' /line

    Complet code /VB2019 /

    Public Class Form1
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dim client As New ServiceReference1.checkVatPortTypeClient()
            Dim request As New ServiceReference1.checkVatRequest()
            Dim response As New ServiceReference1.checkVatResponse()
            request.Body.countryCode = TextBox1.Text.Trim()
            request.Body.vatNumber = TextBox2.Text.Trim()
            request.Body.countryCode = "SK"
            response = client.ServiceReference1_checkVatPortType_checkVat(request)
            If response.Body.valid Then
    
                TextBox3.Text = "VAT number is valid"
            Else
                TextBox3.Text = "VAT number is valid"
            End If
            client.Close()
        End Sub
    End Class
    

  3. is sro 20 Reputation points
    2023-07-24T08:45:55.54+00:00

    Hi Jiachen, thx that you try to help me. i rechecked your modified code

    it looks fine, but when i debug it, it stops at this line:

    Dim requestDate = client.checkVat(countryCode, vatNumber, valid, name, address)

    and writes this exeption:

    Exeption unhandled

    System.ServiceModel.Security.SecurityNegotiationException: 'Could not establish secure channel for SSL/TLS with authority 'ec.europa.eu'.'

    Inner Exeption

    WebException: The request was aborted: Could not create SSL/TLS secure channel.

    Here i copy details from this exception

    System.ServiceModel.Security.SecurityNegotiationException
      HResult=0x80131501
      Message=Could not establish secure channel for SSL/TLS with authority 'ec.europa.eu'.
      Source=mscorlib
      StackTrace:
       at System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage reqMsg, IMessage retMsg)
       at System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData& msgData, Int32 type)
       at wsdl_vat.ServiceReference1.checkVatPortType.checkVat(checkVatRequest request)
       at wsdl_vat.ServiceReference1.checkVatPortTypeClient.ServiceReference1_checkVatPortType_checkVat(checkVatRequest request) in D:\adata\programovanie\wsdl_vat\Connected Services\ServiceReference1\Reference.vb:line 358
       at wsdl_vat.ServiceReference1.checkVatPortTypeClient.checkVat(String& countryCode, String& vatNumber, Boolean& valid, String& name, String& address) in D:\adata\programovanie\wsdl_vat\Connected Services\ServiceReference1\Reference.vb:line 366
       at wsdl_vat.Form1.Button2_Click(Object sender, EventArgs e) in D:\adata\programovanie\wsdl_vat\Form1.vb:line 35
       at System.Windows.Forms.Control.OnClick(EventArgs e)
       at System.Windows.Forms.Button.OnClick(EventArgs e)
       at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
       at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
       at System.Windows.Forms.Control.WndProc(Message& m)
       at System.Windows.Forms.ButtonBase.WndProc(Message& m)
       at System.Windows.Forms.Button.WndProc(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
       at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
       at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
       at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
       at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
       at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
       at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.OnRun()
       at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.DoApplicationModel()
       at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.Run(String[] commandLine)
       at wsdl_vat.My.MyApplication.Main(String[] Args) in :line 83
    
      This exception was originally thrown at this call stack:
        [External Code]
    
    Inner Exception 1:
    WebException: The request was aborted: Could not create SSL/TLS secure channel.
    
    

    i hope you can figure out problem...

    is

    0 comments No comments

  4. is sro 20 Reputation points
    2023-08-09T13:38:20.98+00:00

    Dear Jiachen,

    Thank you, i solved the problem with code, and your code really helped. Finally problem was with wsdl.

    Once again thx

    is

    0 comments No comments