Wednesday, 17 July 2013

How To Access Web Services From Service Reference In Vb.net

Access WebServices  In Vb.net

Here i describe about the web service access from vb.net, some time our requirement data 
is depend upon in web service,Then  we get the data from available function of web service.
I write here steps of access the web services:-
1)Open the 'VS 2010' ,create a new project of  'Excel Addins 2010' .
2)Add  a Ribbon and Button , Write a code on Click_Event.
3)Before the writing  ,we need to add 'service reference' ,for exanple.
   Here i create a project named 'BvdDataAddIn'.
4)Right click on project select  "Add Service Reference" ,open a new window 
  asking for Url , you just feed  the url address  of web services and give it's 
  (like i give 'BvdServiceReference').  click 'Ok' button.
5)After add 'Web Reference' ,double click on 'FindByBvdID' button. 
6)Here I write the code  according my 'Web Reference'(You can change it).

Imports Microsoft.Office.Tools.Ribbon
Imports System
Imports System.Runtime.InteropServices
Imports System.ComponentModel
Imports System.Windows.Forms
'Imports AddinExpress.MSO
Imports Excel = Microsoft.Office.Interop.Excel
Imports System.Data
Imports System.IO
Imports System.Diagnostics
Imports BvdDataAddIn.BvdServiceReference

Public Class BvdID
    Dim activesheet As Excel.Worksheet
    Dim range As Excel.Range

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As       
                              Microsoft.Office.Tools.Ribbon.RibbonControlEventArgs)  Handles Button1.Click
        Try
            Dim service As New RemoteAccessSoapClient()
            Dim selectionResult As BvdServiceReference.SelectionResult
            Dim sessionHandle As String
            Dim bvddata As New BvdDataAddIn.BvdID
            Dim row As Integer
            Dim bvdId As String
            Dim data As String
            Dim dataSet As New System.Data.DataSet
            Dim dataTable As DataTable

            sessionHandle = service.Open("ijameel", "Pwcwelcome1")
            activesheet = Globals.ThisAddIn.Application.ActiveSheet
            'Lastrow = activesheet.Cells.Find("*", SearchOrder:=Excel.XlSearchOrder.xlByRows,  
                                 SearchDirection:=Excel.XlSearchDirection.xlPrevious).Row
            For row = 2 To 2
                'range = activesheet.Cells("A" & row, 1)

                'If range.Value = vbNull Then
                'End If
                bvdId = "BE0878480104"

                'Try
                '    bvdId = range.Value.ToString()
                'Catch ex As Exception
                '    MsgBox(ex.ToString)
                'End Try

                'Marshal.ReleaseComObject(range)
                If (String.IsNullOrEmpty(bvdId)) Then
                End If

                selectionResult = service.FindByBVDId(sessionHandle, bvdId)
                If selectionResult.SelectionCount <> 0 Then
                    data = service.GetData(sessionHandle, selectionResult, "SELECT 
                                NAME,ADDR,ADDR2,ADDR3,CITY,POSTCODE,STATE,COUNTY,
                                 COUNTRY,WEBSITE FROM RemoteAccess.A", 0, 1,
                                 BvdServiceReference.ResultFormatting.MSDataS et)
                    dataSet.ReadXml(New StringReader(data))
                    dataTable = dataSet.Tables(0)
                    activesheet.Cells(row, 2).Value = dataTable.Rows(0).Item(0)
                    activesheet.Cells(row, 3).Value = dataTable.Rows(0).Item(1)
                    activesheet.Cells(row, 4).Value = dataTable.Rows(0).Item(2)
                    activesheet.Cells(row, 5).Value = dataTable.Rows(0).Item(3)
                    activesheet.Cells(row, 6).Value = dataTable.Rows(0).Item(4)
                    activesheet.Cells(row, 7).Value = dataTable.Rows(0).Item(6)
                    activesheet.Cells(row, 8).Value = dataTable.Rows(0).Item(7)
                    activesheet.Cells(row, 9).Value = dataTable.Rows(0).Item(8)
                    activesheet.Cells(row, 10).Value = dataTable.Rows(0).Item(9)
                End If
            Next
            service.Close(sessionHandle)

        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try
    End Sub
End Class

7)Run the program and click on 'FindByBvdID' ,your data will display on worksheet.

No comments:

Post a Comment