I have create a small WebService on VB.NET that is giving me problems trying
to call it. It tests fine running it directly from VS.NET 2003 but when I
try to call it from another vb.net app it gives me an http 401 unauthorized
message. The webservice and the vb.net windows app are both on my
development box. Here is the code that is having the problem: This line
gives the error: Dim results() As Object = Me.Invoke("GetCustomers", New
Object() {sCountryCode})
This code is in the Reference.vb file for the webservice
<System.Web.Services.Protocols.SoapDocumentMethodA ttribute("http://localhost
/Northwind/Customer/GetCustomers",
RequestNamespace:="http://localhost/Northwind/Customer",
ResponseNamespace:="http://localhost/Northwind/Customer",
Use:=System.Web.Services.Description.SoapBindingUs e.Literal,
ParameterStyle:=System.Web.Services.Protocols.Soap ParameterStyle.Wrapped)> _
Public Function GetCustomers(ByVal sCountryCode As String) As
System.Data.DataSet
Dim results() As Object = Me.Invoke("GetCustomers", New Object()
{sCountryCode})
Return CType(results(0),System.Data.DataSet)
End Function
The webservice code is this:
Imports System.Web.Services
Imports System.Data
Imports System.Data.SqlClient
<System.Web.Services.WebService(Namespace:="http ://localhost/Northwind/Custo
mer")> _
Public Class Customer
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.
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
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
<WebMethod()> _
Public Function GetCustomers(ByVal sCountryCode As String) As DataSet
Dim cnn As SqlConnection = New SqlConnection("Data Source=(local);Initial
Catalog=Northwind;Integrated Security=SSPI")
Dim cmd As SqlCommand = cnn.CreateCommand
cmd.CommandType = CommandType.Text
cmd.CommandText = "Select * from Customers where Country = '" & sCountryCode
& "'"
Dim da As SqlDataAdapter = New SqlDataAdapter(cmd)
Dim dsCustomerData As DataSet = New DataSet
da.Fill(dsCustomerData, "Customers")
GetCustomers = dsCustomerData
End Function
End Class
Here is the code to call it.
Private Sub btnGetCustomerData_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles btnGetCustomerData.Click
Dim dsCustomers As DataSet
Dim cust As localhost.Customer = New localhost.Customer
dsCustomers = cust.GetCustomers("France")
dgCustomers.DataSource = dsCustomers
dgCustomers.DataMember = "Customers"
End Sub