Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Returning Two Values from One exposed WebService Help!! 1

Status
Not open for further replies.

richfield

IS-IT--Management
Jan 6, 2005
23
GB
Hi,

I'm creating a webservice to expose, now I've done the code, the thing is I have a function which calculates how many times my web service has been accessed and I need to combine it with a function which displays a table from a SQL Server, here's the code as my explanation is a bit confusing!

<code>
<%@ WebService language="VB" class="Sales" %>

Imports System
Imports System.Web.Services
Imports System.Xml.Serialization
Imports System.Data
Imports System.Data.SqlClient
Imports System.Web
Imports System.Configuration

<WebService(Description:= _
"My Web Service ", _
Namespace:= _
" _
)> _
Public Class Sales
Inherits WebService

<WebMethod(Description:= _
"Display DataGrid")> _
Public Function DisplayData() As DataSet
Dim dst As New DataSet
Dim dad as sqlDataAdapter
Dim conn as sqlConnection
'provide connection string to connection object
conn=new sqlConnection("My Server details")
'sql select instruction to data adapter
dad=new sqlDataAdapter("select * from mytable",conn)
'use dataadapter to fill dataset
dad.fill(dst,"mytable")
'return dataset
Return dst
End Function


<WebMethod(Description := "Number of times this service has been accessed.")> _
Public Function Counter() As Integer
' If the XML Web service method hasn't been accessed, initialize
' it to 1.
If Application("appMyServiceUsage") Is Nothing Then
Application("appMyServiceUsage") = 1
Else
' Increment the usage count.
Application("appMyServiceUsage") = _
CInt(Application("appMyServiceUsage")) + 1
End If
Return CInt(Application("appMyServiceUsage"))
End Function
End Class
</code>

So I want to combine the Counter Function with the DisplayData Function so the result will be a dataset and a integer both being returned from the same function?

Thanks in Advance

Rich
 
You could return the counter as the first row of your dataset. e.g.

Code:
dad=new sqlDataAdapter("SELECT TOP 1 '" & Counter() & "' AS id FROM MyTable union select id from mytable",conn)

but remember that the first statement must have the same columns as the second so if you have more than id being returned in your second statement, this must be reflected in the first (even if you just return blank fields).

Another way to do it would be to pass a variable to DisplayData but using ByRef so it actually updates it. e.g.

Code:
   Public Function DisplayData(ByRef MyCounter As Integer) As DataSet
         Dim dst As New DataSet
         Dim dad as sqlDataAdapter
         Dim conn as sqlConnection
         'provide connection string to connection object
         conn=new sqlConnection("My Server details")
         'sql select instruction to data adapter
         dad=new sqlDataAdapter("select * from mytable",conn)
         'use dataadapter to fill dataset
         dad.fill(dst,"mytable")
	 'Get Counter Value
	 Counter(MyCounter)
         'return dataset
         Return dst
      End Function

    Public Function Counter(ByRef MyCounter As Integer) As Integer
        ' If the XML Web service method hasn't been accessed, initialize
        ' it to 1.
        If Application("appMyServiceUsage") Is Nothing Then
            Application("appMyServiceUsage") = 1
        Else
            ' Increment the usage count.
            Application("appMyServiceUsage") = _
               CInt(Application("appMyServiceUsage")) + 1
        End If
        MyCounter = CInt(Application("appMyServiceUsage"))
    End Function

When you call your function you would just pass it a variable for MyCounter. e.g.

Code:
Dim MyCounter as Integer = 0
Dim dst as DataSet = DisplayData(MyCounter)

Hope this helps.

----------------------------------------------------------------------

Need help finding an answer?

Try the search facilty ( or read FAQ222-2244 on how to get better results.
 
I've tried your second method, but when you go to view the web service it does not allow you to invoke either method.??
 
Oh ok - I just tried it from a class file rather than a web service as it was easier to test. Maybe you can't pass a variable by reference to a web service (can anyone confirm this?).

Have you tried the first method then?

----------------------------------------------------------------------

Need help finding an answer?

Try the search facilty ( or read FAQ222-2244 on how to get better results.
 

"but remember that the first statement must have the same columns as the second so if you have more than id being returned in your second statement, this must be reflected in the first (even if you just return blank fields)."

how do I do that I have 4 items being returned in my second statement?
 
You can just add a column with a blank value. e.g.

Code:
select '1' as Column1, '' As Column2, '' As Column3 etc...

----------------------------------------------------------------------

Need help finding an answer?

Try the search facilty ( or read FAQ222-2244 on how to get better results.
 
I get the following error:

error converting varChar to numeric

heres the code I put in

dad=new sqlDataAdapter("SELECT TOP 1 '" & ServiceUsage() & "' AS HitCount, '1' as Column1, '' As Column2, '' As Column3 FROM MyTable union select SalesID As SalesID, Sales As Sales, Date As Date, EmployeeID As EmployeeID from MyTable",conn)


Is there an error in my code??
 
Firstly, You should make sure that the column names are the same in both statements, however, this isn't the cause of you error. e.g. The first column should be called SalesID rather than HitCount and Column1 should be Sales etc.

Secondly, the error you are receiving is because one of your field types is probably numeric (is it Sales?). If so return:

Code:
... , 0 as Sales ...

----------------------------------------------------------------------

Need help finding an answer?

Try the search facilty ( or read FAQ222-2244 on how to get better results.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top