Creating a Web Service in VB.net

By: Steven Holzner Viewed: 153263 times  Printer Friendly Format    


Creating a Web service is not difficult-just select the File|New|Project menu item, select the ASP.NET Web Service icon, and click OK. That creates a new Web service project, ready for use. Here's the code for the Web service we developed, Service1.asmx.vb:

Imports System.Web.Services

<WebService(Namespace := "http://tempuri.org/")> _
Public Class Service1
    Inherits System.Web.Services.WebService

#Region " Web Services Designer Generated Code "
Public Sub New()
    MyBase.New()

    'This call is required by the Web Services Designer.
    InitializeComponent()

    'Add your own initialization code after the
    'InitializeComponent() call

End Sub

'Required by the Web Services Designer
Private components As System.ComponentModel.IContainer

'NOTE: The following procedure is required by the Web Services Designer
'It can be modified using the Web Services Designer.
'Do not modify it using the code editor.
Friend WithEvents OleDbDataAdapter1 As _
    System.Data.OleDb.OleDbDataAdapter
Friend WithEvents OleDbSelectCommand1 As System.Data.OleDb.OleDbCommand
Friend WithEvents OleDbInsertCommand1 As System.Data.OleDb.OleDbCommand
Friend WithEvents OleDbUpdateCommand1 As System.Data.OleDb.OleDbCommand
Friend WithEvents OleDbDeleteCommand1 As System.Data.OleDb.OleDbCommand
Friend WithEvents OleDbConnection1 As System.Data.OleDb.OleDbConnection
<System.Diagnostics.DebuggerStepThrough()> Private Sub _
    InitializeComponent()
    Me.OleDbDataAdapter1 = New System.Data.OleDb.OleDbDataAdapter()
    Me.OleDbSelectCommand1 = New System.Data.OleDb.OleDbCommand()
    Me.OleDbInsertCommand1 = New System.Data.OleDb.OleDbCommand()
    Me.OleDbUpdateCommand1 = New System.Data.OleDb.OleDbCommand()
    Me.OleDbDeleteCommand1 = New System.Data.OleDb.OleDbCommand()
    Me.OleDbConnection1 = New System.Data.OleDb.OleDbConnection()
    '
    'OleDbDataAdapter1
    '
    Me.OleDbDataAdapter1.DeleteCommand = Me.OleDbDeleteCommand1
    Me.OleDbDataAdapter1.InsertCommand = Me.OleDbInsertCommand1
    Me.OleDbDataAdapter1.SelectCommand = Me.OleDbSelectCommand1
    Me.OleDbDataAdapter1.TableMappings.AddRange(New _
        System.Data.Common.DataTableMapping() {New _
        System.Data.Common.DataTableMapping("Table", "authors", New _
        System.Data.Common.DataColumnMapping() {New _
        System.Data.Common.DataColumnMapping("au_id", "au_id"), New _
        System.Data.Common.DataColumnMapping("au_lname", "au_lname"), _
        New System.Data.Common.DataColumnMapping("au_fname", _
        "au_fname"), _
        New System.Data.Common.DataColumnMapping("phone", _
        "phone"), New System.Data.Common.DataColumnMapping("address", _
        "address"), New System.Data.Common.DataColumnMapping("city", _
        "city"), New System.Data.Common.DataColumnMapping("state", _
        "state"), New System.Data.Common.DataColumnMapping("zip", _
        "zip"), New System.Data.Common.DataColumnMapping("contract", _
        "contract")})})
    Me.OleDbDataAdapter1.UpdateCommand = Me.OleDbUpdateCommand1
    '
    'OleDbSelectCommand1
    '
    Me.OleDbSelectCommand1.CommandText = "SELECT au_id, au_lname, _
        au_fname, phone, address, city, state, zip, contract FROM" & _
    " authors"
    Me.OleDbSelectCommand1.Connection = Me.OleDbConnection1
    '
    'OleDbInsertCommand1
    '
    Me.OleDbInsertCommand1.CommandText = "INSERT INTO authors(au_id, _
        au_lname, au_fname, phone, address, city, state, zip, " & _
        "contract) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?); SELECT au_id, _
        au_lname, au_fname, " & _
        "phone, address, city, state, zip, contract FROM " & _
        authors WHERE (au_id = ?)"
    Me.OleDbInsertCommand1.Connection = Me.OleDbConnection1
    Me.OleDbInsertCommand1.Parameters.Add(New _
    System.Data.OleDb.OleDbParameter("au_id", _
    System.Data.OleDb.OleDbType.VarChar, 11, "au_id")) _
    Me.OleDbInsertCommand1.Parameters.Add(New _
    System.Data.OleDb.OleDbParameter("au_lname", _
    System.Data.OleDb.OleDbType.VarChar, 40, "au_lname")) _
    Me.OleDbInsertCommand1.Parameters.Add(New _
    System.Data.OleDb.OleDbParameter("au_fname", _
    System.Data.OleDb.OleDbType.VarChar, 20, "au_fname")) _
    Me.OleDbInsertCommand1.Parameters.Add(New _
    System.Data.OleDb.OleDbParameter("phone", _
    System.Data.OleDb.OleDbType.VarChar, 12, "phone"))
    Me.OleDbInsertCommand1.Parameters.Add(New _
    System.Data.OleDb.OleDbParameter("address", _
    System.Data.OleDb.OleDbType.VarChar, 40, "address"))
    Me.OleDbInsertCommand1.Parameters.Add(New _
    System.Data.OleDb.OleDbParameter("city", _
    System.Data.OleDb.OleDbType.VarChar, 20, "city"))
    Me.OleDbInsertCommand1.Parameters.Add(New _
    System.Data.OleDb.OleDbParameter("state", _
    System.Data.OleDb.OleDbType.VarChar, 2, "state"))
    Me.OleDbInsertCommand1.Parameters.Add(New _
    System.Data.OleDb.OleDbParameter("zip", _
    System.Data.OleDb.OleDbType.VarChar, 5, "zip"))
    Me.OleDbInsertCommand1.Parameters.Add(New _
    System.Data.OleDb.OleDbParameter("contract", _
    System.Data.OleDb.OleDbType.Boolean, 1, "contract"))
    Me.OleDbInsertCommand1.Parameters.Add(New _
    System.Data.OleDb.OleDbParameter("Select_au_id", _
    System.Data.OleDb.OleDbType.VarChar, 11, "au_id"))
    '
    'OleDbUpdateCommand1
    '
    Me.OleDbUpdateCommand1.CommandText = _
    "UPDATE authors SET au_id = ?, au_lname = ?, " & _
    "au_fname = ?, phone = ?, address = ?," & _
    " city = ?, state = ?, zip = ?, contract = ? " & _
    "WHERE (au_id = ?) AND (address = ? O" & _
    "R ? IS NULL AND address IS NULL) AND (au_fname = ?) " & _
    "AND (au_lname = ?) AND (city" & _
    " = ? OR ? IS NULL AND city IS NULL) AND (contract = ?) " & _
    "AND (phone = ?) AND (stat" & _
    "e = ? OR ? IS NULL AND state IS NULL) AND (zip = ? OR " & _
    "? IS NULL AND zip IS NULL)" & _
    "; SELECT au_id, au_lname, au_fname, phone, address, " & _
    "city, state, zip, contract F" & _
    "ROM authors WHERE (au_id = ?)"
    Me.OleDbUpdateCommand1.Connection = Me.OleDbConnection1
    Me.OleDbUpdateCommand1.Parameters.Add(New _
    System.Data.OleDb.OleDbParameter("au_id", _
    System.Data.OleDb.OleDbType.VarChar, 11, "au_id")) _
    Me.OleDbUpdateCommand1.Parameters.Add(New _
    System.Data.OleDb.OleDbParameter("au_lname", _
    System.Data.OleDb.OleDbType.VarChar, 40, "au_lname"))
    Me.OleDbUpdateCommand1.Parameters.Add(New _
    System.Data.OleDb.OleDbParameter("au_fname", _
    System.Data.OleDb.OleDbType.VarChar, 20, "au_fname"))
    Me.OleDbUpdateCommand1.Parameters.Add(New _
    System.Data.OleDb.OleDbParameter("phone", _
    System.Data.OleDb.OleDbType.VarChar, 12, "phone"))
    Me.OleDbUpdateCommand1.Parameters.Add(New _
    System.Data.OleDb.OleDbParameter("address", _
    System.Data.OleDb.OleDbType.VarChar, 40, "address"))
    Me.OleDbUpdateCommand1.Parameters.Add(New _
    System.Data.OleDb.OleDbParameter("city", _
    System.Data.OleDb.OleDbType.VarChar, 20, "city"))
    Me.OleDbUpdateCommand1.Parameters.Add(New _
    System.Data.OleDb.OleDbParameter("state", _
    System.Data.OleDb.OleDbType.VarChar, 2, "state"))
    Me.OleDbUpdateCommand1.Parameters.Add(New _
    System.Data.OleDb.OleDbParameter("zip", _
    System.Data.OleDb.OleDbType.VarChar, 5, "zip"))
    Me.OleDbUpdateCommand1.Parameters.Add(New _
    System.Data.OleDb.OleDbParameter("contract", _
    System.Data.OleDb.OleDbType.Boolean, 1, "contract"))
    Me.OleDbUpdateCommand1.Parameters.Add(New _
    System.Data.OleDb.OleDbParameter("Original_au_id", _
    System.Data.OleDb.OleDbType.VarChar, 11, _
    System.Data.ParameterDirection.Input, False, _
    CType(0, Byte), CType(0, Byte), "au_id", _
    System.Data.DataRowVersion.Original, Nothing))
    Me.OleDbUpdateCommand1.Parameters.Add(New _
    System.Data.OleDb.OleDbParameter("Original_address", _
    System.Data.OleDb.OleDbType.VarChar, 40, _
    System.Data.ParameterDirection.Input, False, _
    CType(0, Byte), CType(0, Byte), "address", _
    System.Data.DataRowVersion.Original, Nothing))
    Me.OleDbUpdateCommand1.Parameters.Add(New _
    System.Data.OleDb.OleDbParameter("Original_address1", _
    System.Data.OleDb.OleDbType.VarChar, 40, _
    System.Data.ParameterDirection.Input, False, _
    CType(0, Byte), CType(0, Byte), "address", _
    System.Data.DataRowVersion.Original, Nothing))
    Me.OleDbUpdateCommand1.Parameters.Add(New _
    System.Data.OleDb.OleDbParameter("Original_au_fname", _
    System.Data.OleDb.OleDbType.VarChar, 20, _
    System.Data.ParameterDirection.Input, False, _
    CType(0, Byte), CType(0, Byte), "au_fname", _
    System.Data.DataRowVersion.Original, Nothing))
    Me.OleDbUpdateCommand1.Parameters.Add(New _
    System.Data.OleDb.OleDbParameter("Original_au_lname", _
    System.Data.OleDb.OleDbType.VarChar, 40, _
    System.Data.ParameterDirection.Input, False, _
    CType(0, Byte), CType(0, Byte), "au_lname", _
    System.Data.DataRowVersion.Original, Nothing))
    Me.OleDbUpdateCommand1.Parameters.Add(New _
    System.Data.OleDb.OleDbParameter("Original_city", _
    System.Data.OleDb.OleDbType.VarChar, 20, _
    System.Data.ParameterDirection.Input, False, _
    CType(0, Byte), CType(0, Byte), "city", _
    System.Data.DataRowVersion.Original, Nothing))
    Me.OleDbUpdateCommand1.Parameters.Add(New _
    System.Data.OleDb.OleDbParameter("Original_city1", _
    System.Data.OleDb.OleDbType.VarChar, 20, _
    System.Data.ParameterDirection.Input, False, _
    CType(0, Byte), CType(0, Byte), "city", _
    System.Data.DataRowVersion.Original, Nothing))
    Me.OleDbUpdateCommand1.Parameters.Add(New _
    System.Data.OleDb.OleDbParameter("Original_contract", _
    System.Data.OleDb.OleDbType.Boolean, 1, _
    System.Data.ParameterDirection.Input, False, _
    CType(0, Byte), CType(0, Byte), "contract", _
    System.Data.DataRowVersion.Original, Nothing))
    Me.OleDbUpdateCommand1.Parameters.Add(New _
    System.Data.OleDb.OleDbParameter("Original_phone", _
    System.Data.OleDb.OleDbType.VarChar, 12, _
    System.Data.ParameterDirection.Input, False, _
    CType(0, Byte), CType(0, Byte), "phone", _
    System.Data.DataRowVersion.Original, Nothing))
    Me.OleDbUpdateCommand1.Parameters.Add(New _
    System.Data.OleDb.OleDbParameter("Original_state", _
    System.Data.OleDb.OleDbType.VarChar, 2, _
    System.Data.ParameterDirection.Input, False, _
    CType(0, Byte), CType(0, Byte), "state", _
    System.Data.DataRowVersion.Original, Nothing))
    Me.OleDbUpdateCommand1.Parameters.Add(New _
    System.Data.OleDb.OleDbParameter("Original_state1", _
    System.Data.OleDb.OleDbType.VarChar, 2, _
    System.Data.ParameterDirection.Input, False, _
    CType(0, Byte), CType(0, Byte), "state", _
    System.Data.DataRowVersion.Original, Nothing))
    Me.OleDbUpdateCommand1.Parameters.Add(New _
    System.Data.OleDb.OleDbParameter("Original_zip", _
    System.Data.OleDb.OleDbType.VarChar, 5, _
    System.Data.ParameterDirection.Input, False, _
    CType(0, Byte), CType(0, Byte), "zip", _
    System.Data.DataRowVersion.Original, Nothing))
    Me.OleDbUpdateCommand1.Parameters.Add(New _
    System.Data.OleDb.OleDbParameter("Original_zip1", _
    System.Data.OleDb.OleDbType.VarChar, 5, _
    System.Data.ParameterDirection.Input, False, _
    CType(0, Byte), CType(0, Byte), "zip", _
    System.Data.DataRowVersion.Original, Nothing))
    Me.OleDbUpdateCommand1.Parameters.Add(New _
    System.Data.OleDb.OleDbParameter("Select_au_id", _
    System.Data.OleDb.OleDbType.VarChar, 11, "au_id"))
    '
    'OleDbDeleteCommand1
    '
    Me.OleDbDeleteCommand1.CommandText = _
    "DELETE FROM authors WHERE (au_id = ?) AND _
    (address = ? OR ? IS NULL AND address I" & _
    "S NULL) AND (au_fname = ?) AND " & _
    "(au_lname = ?) AND (city = ? OR ? IS NULL AND cit" & _
    "y IS NULL) AND (contract = ?) AND " & _
    "(phone = ?) AND (state = ? OR ? IS NULL AND st" & _
    "ate IS NULL) AND (zip = ? OR ? IS NULL AND zip IS NULL)"
    Me.OleDbDeleteCommand1.Connection = Me.OleDbConnection1
    Me.OleDbDeleteCommand1.Parameters.Add(New _
    System.Data.OleDb.OleDbParameter("Original_au_id", _
    System.Data.OleDb.OleDbType.VarChar, 11, _
    System.Data.ParameterDirection.Input, False, _
    CType(0, Byte), CType(0, Byte), "au_id", _
    System.Data.DataRowVersion.Original, Nothing))
    Me.OleDbDeleteCommand1.Parameters.Add(New _
    System.Data.OleDb.OleDbParameter("Original_address", _
    System.Data.OleDb.OleDbType.VarChar, 40, _
    System.Data.ParameterDirection.Input, False, _
    CType(0, Byte), CType(0, Byte), "address", _
    System.Data.DataRowVersion.Original, Nothing))
    Me.OleDbDeleteCommand1.Parameters.Add(New _
    System.Data.OleDb.OleDbParameter("Original_address1", _
    System.Data.OleDb.OleDbType.VarChar, 40, _
    System.Data.ParameterDirection.Input, False, _
    CType(0, Byte), CType(0, Byte), "address", _
    System.Data.DataRowVersion.Original, Nothing))
    Me.OleDbDeleteCommand1.Parameters.Add(New _
    System.Data.OleDb.OleDbParameter("Original_au_fname", _
    System.Data.OleDb.OleDbType.VarChar, 20, _
    System.Data.ParameterDirection.Input, False, _
    CType(0, Byte), CType(0, Byte), "au_fname", _
    System.Data.DataRowVersion.Original, Nothing))
    Me.OleDbDeleteCommand1.Parameters.Add(New _
    System.Data.OleDb.OleDbParameter("Original_au_lname", _
    System.Data.OleDb.OleDbType.VarChar, 40, _
    System.Data.ParameterDirection.Input, False, _
    CType(0, Byte), CType(0, Byte), "au_lname", _
    System.Data.DataRowVersion.Original, Nothing))
    Me.OleDbDeleteCommand1.Parameters.Add(New _
    System.Data.OleDb.OleDbParameter("Original_city", _
    System.Data.OleDb.OleDbType.VarChar, 20, _
    System.Data.ParameterDirection.Input, False, _
    CType(0, Byte), CType(0, Byte), "city", _
    System.Data.DataRowVersion.Original, Nothing))
    Me.OleDbDeleteCommand1.Parameters.Add(New _
    System.Data.OleDb.OleDbParameter("Original_city1", _
    System.Data.OleDb.OleDbType.VarChar, 20, _
    System.Data.ParameterDirection.Input, False, _
    CType(0, Byte), CType(0, Byte), "city", _
    System.Data.DataRowVersion.Original, Nothing))
    Me.OleDbDeleteCommand1.Parameters.Add(New _
    System.Data.OleDb.OleDbParameter("Original_contract", _
    System.Data.OleDb.OleDbType.Boolean, 1, _
    System.Data.ParameterDirection.Input, False, _
    CType(0, Byte), CType(0, Byte), "contract", _
    System.Data.DataRowVersion.Original, Nothing))
    Me.OleDbDeleteCommand1.Parameters.Add(New _
    System.Data.OleDb.OleDbParameter("Original_phone", _
    System.Data.OleDb.OleDbType.VarChar, 12, _
    System.Data.ParameterDirection.Input, False, _
    CType(0, Byte), CType(0, Byte), "phone", _
    System.Data.DataRowVersion.Original, Nothing))
    Me.OleDbDeleteCommand1.Parameters.Add(New _
    System.Data.OleDb.OleDbParameter("Original_state", _
    System.Data.OleDb.OleDbType.VarChar, 2, _
    System.Data.ParameterDirection.Input, False, _
    CType(0, Byte), CType(0, Byte), "state", _
    System.Data.DataRowVersion.Original, Nothing))
    Me.OleDbDeleteCommand1.Parameters.Add(New _
    System.Data.OleDb.OleDbParameter("Original_state1", _
    System.Data.OleDb.OleDbType.VarChar, 2, _
    System.Data.ParameterDirection.Input, False, _
    CType(0, Byte), CType(0, Byte), "state", _
    System.Data.DataRowVersion.Original, Nothing))
    Me.OleDbDeleteCommand1.Parameters.Add(New _
    System.Data.OleDb.OleDbParameter("Original_zip", _
    System.Data.OleDb.OleDbType.VarChar, 5, _
    System.Data.ParameterDirection.Input, False, _
    CType(0, Byte), CType(0, Byte), "zip", _
    System.Data.DataRowVersion.Original, Nothing))
    Me.OleDbDeleteCommand1.Parameters.Add(New _
    System.Data.OleDb.OleDbParameter("Original_zip1", _
    System.Data.OleDb.OleDbType.VarChar, 5, _
    System.Data.ParameterDirection.Input, _
    False, CType(0, Byte), CType(0, Byte), "zip", _
    System.Data.DataRowVersion.Original, Nothing))
        '
        'OleDbConnection1
        '
        Me.OleDbConnection1.ConnectionString =
        "Provider=SQLOLEDB.1;Integrated Security=" & _
        "SSPI;Persist Security " & _
        "Info=False;Initial " & _
        "Catalog=pubs;Use Procedure for Prepare=1;Auto " & _
        "Translate=True;Packet Size=4096;Wo" & _
        "rkstation ID=STEVE;Use Encryption for Data=False;Tag " & _
        "with column collation when " & _
        "possible=False"

    End Sub

    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        'CODEGEN: This procedure is required by the Web Services Designer
        'Do not modify it using the code editor.
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

#End Region

    ' WEB SERVICE EXAMPLE
    ' The HelloWorld() example service returns the string Hello World.
    ' To build, uncomment the following lines then save and
    ' build the project.
    ' To test this web service, ensure that the .asmx file is
    ' the start page
    ' and press F5.
    '

    '<WebMethod()> Public Function HelloWorld() As String
    '      HelloWorld = "Hello World"
    ' End Function

    <WebMethod(Description:="Gets the authors")> Public _
    Function GetAuthors() As DataSet1
        Dim AuthorsTable As New DataSet1()
        OleDbDataAdapter1.Fill(AuthorsTable)
        Return AuthorsTable
    End Function

    <WebMethod(Description:="Updates the authors")> Public _
    Function UpdateAuthors(ByVal _
    Changes As DataSet1) As DataSet1
        If (Changes Is Nothing) Then
            Return Nothing
        Else
            OleDbDataAdapter1.Update(Changes)
            Return Changes
        End If
    End Function

End Class


Most Viewed Articles (in VB.net )

Latest Articles (in VB.net)

Comment on this tutorial