Using WebService In ASPX

R

rn5a

I have created the following WebService named NConnect.asmx using which
I want an ASPX page to first authenticate a user & after successful
user validation, the ASPX page should display a few TextBoxes for users
to enter some data in those TextBoxes which will finally be inserted in
a SQL Server 2005 DB table:

<%@ WebService Language="VB" Class="NConnect" %>

Imports System
Imports System.Data
Imports System.Data.SqlClient
Imports System.Web
Imports System.Web.Services
Imports System.Web.Services.Protocols

Public Class Authenticator : Inherits SoapHeader
Public UserName As String
Public Password As String
End Class

Public Class NConnect : Inherits WebService
Public sHeader As Authenticator
Private sqlConn As New SqlConnection(".......")
<WebMethod(), SoapHeader("sHeader")> Public Sub UpdateData(ByVal
UserID As Integer, ByVal Mileage As Double, ByVal MaxSpeed As Double,
ByVal Throttle As Double)
If (sHeader Is Nothing) Then
Throw New ArgumentNullException
End If

If (Authenticate(sHeader.UserName, sHeader.Password)) Then
Dim sqlCmd As SqlCommand

sqlCmd = New SqlCommand("ConnectInfo", sqlConn)
sqlCmd.CommandType = CommandType.StoredProcedure

Try
With sqlCmd
.Parameters.Add("@UserID", SqlDbType.Int).Value =
UserID
'passing other parameters to the
'SP to be inserted in the DB table
End With

sqlConn.Open()
sqlCmd.ExecuteNonQuery()
Catch ex As SqlException
Throw ex
End Try
End If
End Sub

Private Function Authenticate(ByVal UserName As String, ByVal
Password As String) As Boolean
Dim iUID As Integer = 0
Dim sqlCmd As SqlCommand

sqlCmd = New SqlCommand("ValidateUser", sqlConn)
sqlCmd.CommandType = CommandType.StoredProcedure

With sqlCmd
.Parameters.Add("@UserName", SqlDbType.VarChar, 50).Value =
UserName
.Parameters.Add("@Password", SqlDbType.VarChar, 50).Value =
Password
End With

sqlConn.Open()
iUID = CType(sqlCmd.ExecuteScalar, Integer)

If (iUID = 0) Then
Return False
Else
Return True
End If
End Function
End Class

Now how do I make use of the above ASMX Web Service in an ASPX page so
that ASP.NET first validates the user, then renders a few TextBoxes for
the user to enter some data which will finally be inserted a DB table
when the user clicks a Button?

How do I pass the UserName & Password as the SOAP headers so as to send
the SOAP headers to validate the user?

If I type the URL http://myserver/ASPX/NConnect.asmx?op=UpdateData in
the browser, then the browser displays the 4 TextBoxes which are the 4
parameters the WebMethod UpdataData expects but how do I validate the
user prior to that?

Apart from the 4 TextBoxes, the above URL also displays info about SOAP
& all those things. How do I avoid that?

The bottomline is whatever tasks the above ASMX code does, I want do
them in an ASPX page using the ASMX code.
 
L

Laurent Bugnion

Hi,

I have created the following WebService named NConnect.asmx using which
I want an ASPX page to first authenticate a user & after successful
user validation, the ASPX page should display a few TextBoxes for users
to enter some data in those TextBoxes which will finally be inserted in
a SQL Server 2005 DB table:

Now how do I make use of the above ASMX Web Service in an ASPX page so
that ASP.NET first validates the user, then renders a few TextBoxes for
the user to enter some data which will finally be inserted a DB table
when the user clicks a Button?

How do I pass the UserName & Password as the SOAP headers so as to send
the SOAP headers to validate the user?

If I type the URL http://myserver/ASPX/NConnect.asmx?op=UpdateData in
the browser, then the browser displays the 4 TextBoxes which are the 4
parameters the WebMethod UpdataData expects but how do I validate the
user prior to that?

This is only a service provided by ASP.NET, to allow you testing your
web service code without implementing a client. When you deploy the web
service to the production server, the ASP.NET won't deliver this feature
anymore, this is only in localhost configuration.
Apart from the 4 TextBoxes, the above URL also displays info about SOAP
& all those things. How do I avoid that?

The bottomline is whatever tasks the above ASMX code does, I want do
them in an ASPX page using the ASMX code.

If you want the client to call the ASMX web service, you must implement
a client in JavaScript. Implementing the SOAP client yourself is
possible but difficult. There are frameworks for that. Check atlas.asp.net.

If you want to call the web service from your server-side code (i.e.
when the page is rendered, it connects to the web service, gets
information and then use this information to render the HTML code), then
you can use Web References in Studio 2005. Simply right click on your
project, choose "Add web reference", and then enter the web service's
URL and follow the instructions. This will create a proxy class, which
you can use in your code behind in synchronous or asynchronous way.

Finally, as usual, you might want to ask yourself if a SOAP based web
service is really what you need, and might be interested in reading this:
http://www.galasoft-lb.ch/mydotnet/articles/article-2006100601.aspx

HTH,
Laurent
 
C

Cowboy \(Gregory A. Beamer\)

First thing I see is a real design problem.

In general, you should not mix metaphors. In other words, authentication and
putting data in the database are two separate operations. While you can
combine them into one message, you have to return a message to let hte user
know precisely what happened. For example, did I fail putting information
into hte database, fail to authenticate, or raise an exception? Which
happened? The way you have things designed, you will never know.

Now, you can get around this with logging, but it still misses the point.

My suggestion. Two paths:

Path 1: Make your service look more like a typical library. Separate the
authentication method from the insert method and roll in that direction.

Path 2: If it makes sense to combine the information, use a message based
approach and send all of the information and return a message to indicate
what happened when the person called the method. As a sub, you return void.

As for calling the service, you can create a web reference. In ASP.NET 2.0,
where you can easily rename services, you can point to an ASMX in the same
project (although that would be unwise performance wise and I would,
instead, encapsulate the logic in a library and call from both the ASPX and
the ASMX, depending on how the user is using the system).

EIther way, I would move the actual funcationality out of the UI, whether
the "UI" is ASPX or ASMX. This gives you much better reuse.

Good luck!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top