Link to home
Start Free TrialLog in
Avatar of maqskywalker
maqskywalker

asked on

passing parameter to stored proced inside a web service

Hi experts,

I'm using Visual Studio 2010 and VB in a Web Application and SQL Server 2008.
I'm using the Employees table of the Northwind Database.

I have a stored procedure called GetEmployeeDetailsByID that uses 1 parameter called @EmployeeID.  

The code for the stored procedure is this:

CREATE PROCEDURE [dbo].[GetEmployeeDetailsByID] 
	@EmployeeID int = 0
AS
BEGIN
	SET NOCOUNT ON;
	SELECT FirstName, LastName, BirthDate, City, Country 
	FROM Employees WHERE EmployeeID=@EmployeeID
END

Open in new window


When I execute the Stored Procedure and enter @EmployeeID = 1 then the stored procedure returns the following:

User generated image
I read this article, in this example their stored procedure performs an insert statement.
http://www.c-sharpcorner.com/UploadFile/718fc8/using-stored-procedure-in-web-service/

I'm trying do them same type of thing as in the example but in my stored procedure it is doing a select statement.

See I want to write a .asmx Web Service that will call this stored procedure and pass the parameter into it and execute it.

I have a Web Service called EmployeeByIDWebService.asmx

My code looks like this:

Imports System
Imports System.Collections.Generic
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 EmployeeByIDWebService
    Inherits System.Web.Services.WebService

    <WebMethod()> _
    Public Function HelloWorld() As String
       Return "Hello World"
    End Function

    Dim constring As String = "Data Source=MYCOMPUTER\SQLEXPRESS;Initial Catalog=Northwind;Integrated Security=True"

    Dim conn As SqlConnection
    Dim comm As SqlCommand

    <WebMethod(Description:="Simple Example")> _
    Public Function data(EmployeeID As Integer) As String
        conn = New SqlConnection(constring)
        conn.Open()

        comm = New SqlCommand()
        comm.Connection = conn
        comm.CommandType = System.Data.CommandType.StoredProcedure
        comm.CommandText = "GetEmployeeDetailsByID"
        comm.Parameters.AddWithValue("@EmployeeID", EmployeeID)

        Try
            'Executes a Transact-SQL statement against the connection and returns the number of rows affected.
            comm.ExecuteNonQuery()

        Catch ex As Exception
            Throw ex
        Finally
            conn.Close()
            conn.Dispose()
        End Try

        Return

    End Function


End Class

Open in new window


I'm not sure if the syntax is correct,? Is this correct?  
In the Return statement what do I put?

When I run this Web Service in the browser I should get a text box similar to this one (this pic is from the example in the link above) where I enter the EmployeeID parameter and then when I click the Invoke parameter my web service should return my result.

User generated image
Avatar of maqskywalker
maqskywalker

ASKER

I revised my Web Service called EmployeeByIDWebService.asmx this is my code:

Imports System
Imports System.Collections.Generic
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 EmployeeByIDWebService
    Inherits System.Web.Services.WebService

    <WebMethod()> _
    Public Function HelloWorld() As String
       Return "Hello World"
    End Function

    Dim constring As String = "Data Source=FCOE-DPN1TR1;Initial Catalog=Northwind;Integrated Security=True"

    Dim conn As SqlConnection
    Dim comm As SqlCommand

    <WebMethod(Description:="Simple Example")> _
    Public Function data(EmployeeID As Integer) As String
        conn = New SqlConnection(constring)
        conn.Open()

        comm = New SqlCommand()
        comm.Connection = conn
        comm.CommandType = System.Data.CommandType.StoredProcedure
        comm.CommandText = "GetEmployeeDetailsByID"
        comm.Parameters.AddWithValue("@EmployeeID", EmployeeID)

        Dim dtTable As New DataTable

        Try
            'Executes a Transact-SQL statement against the connection and returns the number of rows affected.
            comm.ExecuteNonQuery()

            dtTable.Load(comm.ExecuteReader())

        Catch ex As Exception
            Throw ex
        Finally
            conn.Close()
            conn.Dispose()
        End Try

        Return dtTable(0)(0)

    End Function

End Class

Open in new window


When I run it I get this:
User generated image
Then if i click on the link for my method called data, i get this:

User generated image
When I type in an EmployeeID value of 1 and click invoke I get this:

User generated image
So Nancy is the FirstName column of the result returned by my stored procedure.

Is there a better way to write this VB code that I wrote in this example to return also the LastName, BirthDate, City, Country in the xml.
What I mean is, can I just return the table that is returned by my stored procedure as a DataTable or DataSet? How would I change my syntax of my Web Service to do that?
ASKER CERTIFIED SOLUTION
Avatar of kaufmed
kaufmed
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial