Creating a Web Service in VB.net

By: Steven Holzner Emailed: 1696 times Printed: 2202 times    

Latest comments
By: rohit kumar - how this program is work
By: Kirti - Hi..thx for the hadoop in
By: Spijker - I have altered the code a
By: ali mohammed - why we use the java in ne
By: ali mohammed - why we use the java in ne
By: mizhelle - when I exported the data
By: raul - no output as well, i'm ge
By: Rajesh - thanx very much...
By: Suindu De - Suppose we are executing

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

VB.net Home | All VB.net Tutorials | Latest VB.net Tutorials

Sponsored Links

If this tutorial doesn't answer your question, or you have a specific question, just ask an expert here. Post your question to get a direct answer.



Bookmark and Share

Comments(0)


Be the first one to add a comment

Your name (required):


Your email(required, will not be shown to the public):


Your sites URL (optional):


Your comments:



More Tutorials by Steven Holzner
Changes in Controls from VB6 to VB.net
Throwing a Custom Exception in VB.net
Throwing an Exception in VB.net
Using Finally in VB.net
Using Multiple Catch Statements in VB.net
Exception Filtering in the Catch Block in VB.net
Raising an Exception Intentionally in VB.net
Getting an Exception's Number and Description in VB.net
Using On Error GoTo 0 in VB.net
Using Resume Next and Resume Line in VB.net
Understanding Scope in VB.net
Properties in VB.net
Procedure Delegates in VB.net
Preserving a Variable's Values between Procedure Calls in VB.net
Specifying Optional Arguments with default values in Procedures in VB.net

More Tutorials in VB.net
Scope in VB.net
Unstructured Exception Handling in VB.net
Structured Exception Handling in VB.net
Creating Sub Procedures in VB.net
Creating Functions in VB.net
Passing a Variable Number of Arguments to Procedures in VB.net
Specifying Optional Arguments with default values in Procedures in VB.net
Preserving a Variable's Values between Procedure Calls in VB.net
Procedure Delegates in VB.net
Properties in VB.net
Understanding Scope in VB.net
Using Resume Next and Resume Line in VB.net
Using On Error GoTo 0 in VB.net
Getting an Exception's Number and Description in VB.net
Raising an Exception Intentionally in VB.net

More Latest News
Most Viewed Articles (in VB.net )
Your first VB.NET Crystal Reports - A step by step guide
File operations in VB.net
How to export from database to excel using VB.net
What is .NET Framework and the Common Language Runtime?
The text writer in VB.net
Vb.net binary reader and writer
How to Open and Edit Cells in an Excel 2007 file in VB.NET
Insert Background Pictures in Excel 2007 from VB.NET
Update cell data in an Excel file using OLEDB in VB.net
How to send email using VB.NET code
Multi threaded Socket Programming in VB.net
Using the EventLog Class in VB.net
Creating a Windows Service Installer in VB.net
Creating a Web Service in VB.net
Passing a Variable Number of Arguments to Procedures in VB.net
Most Emailed Articles (in VB.net)
How to export from database to excel using VB.net
Changes in Controls from VB6 to VB.net
What is .NET Framework and the Common Language Runtime?
While Loop in VB.net
Substring in Vb.Net String Class
Create an Excel 2007 file in VB.NET 2005
Insert Pictures in Excel 2007 from VB.NET 2005
Socket Programming in VB.net
Chat Server in VB.net
Using the ServiceBase Class in VB.net
String Insert, index off & format in VB.net
String Split function in VB.net
Vb.net text reader
Using Excel 2007 Chart VB.net Picture Box
Using Excel 2007 DataBar in VB.net