Developing Your First Silverlight Application Using Visual Studio 2008 - Creating an ASP.NET 3.5 Web Service using Visual Studio 2008 beta: source code
(Page 2 of 5 )
Once the project is created (as shown in the previous section), modify “ProductService.vb” to match the following:
Imports System.Web.Services
Imports System.Web.Services.Protocols
Imports System.ComponentModel
Imports System.Data.SqlClient
' To allow this Web Service to be called from script, using ASP.NET AJAX,
uncomment the following line.
<System.Web.Script.Services.ScriptService()> _
<System.Web.Services.WebService(Namespace:="http://tempuri.org/")> _
<System.Web.Services.WebServiceBinding
(ConformsTo:=WsiProfiles.BasicProfile1_1)> _
<ToolboxItem(False)> _
Public Class ProductService
Inherits System.Web.Services.WebService
<WebMethod()> _
Public Function GetProductInfo(ByVal ProductID As Integer) As Product
Using cn As New SqlConnection(ConfigurationManager.ConnectionStrings
("cnNorthwind").ConnectionString)
Using cmd As New SqlCommand("SELECT ProductID, ProductName, UnitPrice,
UnitsInStock, UnitsOnOrder,(SELECT sum(UnitPrice * Quantity) FROM dbo.
[Order Details] WHERE ProductID = a.ProductID) as SaleValue FROM
dbo.Products as a WHERE ProductID=" & ProductID, cn)
cmd.Connection.Open()
Using rdr As SqlDataReader = cmd.ExecuteReader
If rdr.Read() Then
Return New Product With {.ProductID = rdr(rdr.GetOrdinal("ProductID")),
.ProductName = rdr(rdr.GetOrdinal("ProductName")) & "", .UnitPrice = rdr
(rdr.GetOrdinal("UnitPrice")) & "", .UnitsInStock = rdr(rdr.GetOrdinal
("UnitsInStock")) & "", .UnitsOnOrder = rdr(rdr.GetOrdinal
("UnitsOnOrder")) & "", .SaleTillToDate = rdr(rdr.GetOrdinal("SaleValue"))
& ""}
Else
Return Nothing
End If
End Using
cmd.Connection.Close()
End Using
End Using
End Function
End Class
Make sure that the following attribute is added to the class (otherwise the “Error invoking Service” may be raised):
<System.Web.Script.Services.ScriptService()>
Modify “Web.Config” to include the following:
<connectionStrings>
<add name="cnNorthwind" connectionString="Data Source=.sql2k5;initial
catalog=Northwind;user id=sa;password=eXpress2005"/>
</connectionStrings>
Add a new class named “Product.vb” to the web service project (right click on project and select “Add new Item”). Modify it to match the following:
Public Class Product
Private _ProductID As Integer
Private _ProductName As String
Private _UnitPrice As Double
Private _UnitsInStock As Integer
Private _UnitsOnOrder As Integer
Private _SaleTillToDate As Integer
Public Property ProductID() As Integer
Get
Return _ProductID
End Get
Set(ByVal value As Integer)
_ProductID = value
End Set
End Property
Public Property ProductName() As String
Get
Return _ProductName
End Get
Set(ByVal value As String)
_ProductName = value
End Set
End Property
Public Property UnitPrice() As Double
Get
Return _UnitPrice
End Get
Set(ByVal value As Double)
_UnitPrice = value
End Set
End Property
Public Property UnitsInStock() As Integer
Get
Return _UnitsInStock
End Get
Set(ByVal value As Integer)
_UnitsInStock = value
End Set
End Property
Public Property UnitsOnOrder() As Integer
Get
Return _UnitsOnOrder
End Get
Set(ByVal value As Integer)
_UnitsOnOrder = value
End Set
End Property
Public Property SaleTillToDate() As Integer
Get
Return _SaleTillToDate
End Get
Set(ByVal value As Integer)
_SaleTillToDate = value
End Set
End Property
End Class
Next: Adding a Silverlight Project to the Visual Studio 2008 solution >>
More ASP.NET Articles
More By Jagadish Chaterjee