Creating a Web Service in VB.net
By: Steven Holzner in VB.net Tutorials on 2010-10-23
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
Add Comment
This policy contains information about your privacy. By posting, you are declaring that you understand this policy:
- Your name, rating, website address, town, country, state and comment will be publicly displayed if entered.
- Aside from the data entered into these form fields, other stored data about your comment will include:
- Your IP address (not displayed)
- The time/date of your submission (displayed)
- Your email address will not be shared. It is collected for only two reasons:
- Administrative purposes, should a need to contact you arise.
- To inform you of new comments, should you subscribe to receive notifications.
- A cookie may be set on your computer. This is used to remember your inputs. It will expire by itself.
This policy is subject to change at any time and without notice.
These terms and conditions contain rules about posting comments. By submitting a comment, you are declaring that you agree with these rules:
- Although the administrator will attempt to moderate comments, it is impossible for every comment to have been moderated at any given time.
- You acknowledge that all comments express the views and opinions of the original author and not those of the administrator.
- You agree not to post any material which is knowingly false, obscene, hateful, threatening, harassing or invasive of a person's privacy.
- The administrator has the right to edit, move or remove any comment for any reason and without notice.
Failure to comply with these rules may result in being banned from submitting further comments.
These terms and conditions are subject to change at any time and without notice.
- Data Science
- Android
- React Native
- AJAX
- ASP.net
- C
- C++
- C#
- Cocoa
- Cloud Computing
- HTML5
- Java
- Javascript
- JSF
- JSP
- J2ME
- Java Beans
- EJB
- JDBC
- Linux
- Mac OS X
- iPhone
- MySQL
- Office 365
- Perl
- PHP
- Python
- Ruby
- VB.net
- Hibernate
- Struts
- SAP
- Trends
- Tech Reviews
- WebServices
- XML
- Certification
- Interview
categories
Related Tutorials
Changes in Controls from VB6 to VB.net
Unstructured Exception Handling in VB.net
Structured Exception Handling in VB.net
Creating Sub Procedures 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
Throwing an Exception in VB.net
Comments