Step 1: Create an application in Iron Speed Designer using the Application Wizard. Be sure to use the table(s) for which you wish to add a web service. This example uses the Shippers table in Northwind database.
Step 2: Build the application.
Step 3: Add a reference to System.Web.Services.dll in the application’s CompileApplication.rsp file. This is optional if you are using Visual Studio .NET to compile the application, but required if you are using vbc.exe to compile the application
…\MyApp\CompileApplication.rsp
Step 4: Open application in Visual Studio .NET.
Open Visual Studio and click on Open -> Web Site and point to your application's directory.
Step 5: Add a Web Service to your application. (Add New Item, Web Service).
…\MyApp\MyWebServiceDirectory\MyWebServiceForTableAccess.asmx
Step 6: Switch the Web Service to code view, and add functions similar to the following to the Visual Basic.NET class for the Web Service (MyWebServiceForTableAccess.asmx.vb):
' Web service function to Add a Shippers record
' Input: company name, phone number
' Output: id of created record
<WebMethod()> Public Function AddShippersRecord(ByVal companyName As String, ByVal phone As String) As Integer
' Set up object for the primary key of new record to be inserted.
Dim createdShipperId As Integer
' Create and obtain a transaction object.
Dim t As BaseClasses.Data.SqlProvider.SqlTransaction
t = BaseClasses.Data.SqlProvider.SqlTransaction.GetOrCreateTransaction("ShippersAccess")
Try
' Create a new record and populate it.
Dim shipperRecord As New ShippersRecord
shipperRecord.SetCompanyNameFieldValue(companyName)
shipperRecord.SetPhoneFieldValue(phone)
' Save the new record
shipperRecord.Save()
' Get the primary key of the new record inserted.
Dim primaryKey As BaseClasses.Data.KeyValue
primaryKey = shipperRecord.GetID()
createdShipperId = CInt(primaryKey.ColumnValueByName("ShipperID"))
' Commit the transaction.
t.Commit()
Catch
' On exception, rollback the transaction.
t.RollBack()
Finally
' Finally release the transaction object.
t.Release(True)
End Try
Return createdShipperId
End Function
' Web service function to Delete a Shippers record
' Input: shipper id
' Output: boolean indicating success/failure to delete record
<WebMethod()> Public Function DeleteShippersRecord(ByVal shipperId As Integer) As Boolean
Dim isSuccessfullyDeleted As Boolean = False
' Create and obtain a transaction object.
Dim t As BaseClasses.Data.SqlProvider.SqlTransaction
t = BaseClasses.Data.SqlProvider.SqlTransaction.GetOrCreateTransaction("ShippersAccess")
Try
' Set the Shippers table data access class.
Dim shipperAccess As ShippersTable = ShippersRecord.TableUtils
' Set up primary key for the record to delete.
Dim primaryKey As New BaseClasses.Data.KeyValue
primaryKey.AddElement(shipperAccess.ShipperIDColumn.InternalName, shipperId.ToString())
' Delete the record using the above primary key.
ShippersTable.DeleteRecord(primaryKey)
' Commit the transaction.
t.Commit()
' Flag a successful delete.
isSuccessfullyDeleted = True
Catch e As Exception
' Rollback the transaction.
t.RollBack()
Finally
' Release the transaction.
t.Release(True)
End Try
Return isSuccessfullyDeleted
End Function
' Web service function to Update a Shippers record
' Input: shipper id
' Output: boolean indicating success/failure to update record
<WebMethod()> Public Function UpdateShippersRecord(ByVal shipperId As Integer, ByVal companyName As String, ByVal phoneNumber As String) As Boolean
Dim isSuccessfullyUpdated As Boolean = False
' Create and obtain a transaction object.
Dim t As BaseClasses.Data.SqlProvider.SqlTransaction
t = BaseClasses.Data.SqlProvider.SqlTransaction.GetOrCreateTransaction("ShippersAccess")
Try
' Set the Shippers table data access class.
Dim shipperAccess As ShippersTable = ShippersRecord.TableUtils
' Set up primary key for the record to update.
Dim primaryKey As New BaseClasses.Data.KeyValue
primaryKey.AddElement(shipperAccess.ShipperIDColumn.InternalName, shipperId.ToString())
' Update the record using the above primary key.
Dim shipperRec As ShippersRecord = ShippersTable.GetRecord(primaryKey, True)
shipperRec.SetCompanyNameFieldValue(companyName)
shipperRec.SetPhoneFieldValue(phoneNumber)
' Save the record
shipperRec.Save()
' Commit the transaction.
t.Commit()
' Flag a successful update.
isSuccessfullyUpdated = True
Catch e As Exception
' Rollback the transaction.
t.RollBack()
Finally
' Release the transaction.
t.Release(True)
End Try
Return isSuccessfullyUpdated
End Function
Step 7: Add the following imports statement at the top of your webservices.asmx.vb file
imports <Application Name>.Business
Replace <Application Name> with your application name.
Step 8: Compile the application in Visual Studio .NET.
Step 9: The web service now has three supported operations: Add, Delete, and Update.
It can be accessed via http://localhost/<App Name>/<Folder>/<Web Service Name>
|