Using WebService In ASPX

Discussion in 'ASP .Net' started by rn5a@rediffmail.com, Oct 10, 2006.

  1. Guest

    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.
    , Oct 10, 2006
    #1
    1. Advertising

  2. Hi,

    wrote:
    > 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:


    <snip>


    > 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
    --
    Laurent Bugnion, GalaSoft
    Software engineering: http://www.galasoft-LB.ch
    Private/Malaysia: http://mypage.bluewin.ch/lbugnion
    Support children in Calcutta: http://www.calcutta-espoir.ch
    Laurent Bugnion, Oct 10, 2006
    #2
    1. Advertising

  3. 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!

    --
    Gregory A. Beamer
    MVP; MCP: +I, SE, SD, DBA
    http://gregorybeamer.spaces.live.com

    *************************************************
    Think outside of the box!
    *************************************************
    <> wrote in message
    news:...
    >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.
    >
    Cowboy \(Gregory A. Beamer\), Oct 10, 2006
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. John M
    Replies:
    1
    Views:
    1,093
    Kumar Reddi
    May 29, 2005
  2. batista
    Replies:
    1
    Views:
    885
    Laurent Bugnion
    Jan 26, 2006
  3. batista
    Replies:
    0
    Views:
    544
    batista
    Jan 26, 2006
  4. Mike
    Replies:
    2
    Views:
    662
  5. Roberto Giorgetti

    Error connection using webservice locally (3 webservice in balanci

    Roberto Giorgetti, Jun 13, 2006, in forum: ASP .Net Web Services
    Replies:
    0
    Views:
    185
    Roberto Giorgetti
    Jun 13, 2006
Loading...

Share This Page