ASP.NET
  Home arrow ASP.NET arrow Page 5 - Building XML Web Services Using C# and ASP...
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Dedicated Servers  
Moblin 
JMSL Numerical Library 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
ASP.NET

Building XML Web Services Using C# and ASP.NET
By: James Yang
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 42
    2002-03-29

    Table of Contents:
  • Building XML Web Services Using C# and ASP.NET
  • What is a web service?
  • A simple web service
  • Other web service features
  • Real world application
  • Conclusion

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Building XML Web Services Using C# and ASP.NET - Real world application


    (Page 5 of 6 )

    Well, we've learnt all of the fundamentals for building web services. It's time to put what we've learnt into practice by designing a real world example. The example application we're about to create will not contain properties, because Microsoft recommends a web service be stateless whenever possible.

    We are going to make a stripped-down version of Passport. Our version will contain seven methods:
    • bool Authenticate (string username, string password): This method will authenticate a user and return true if authenticated and false if not.
    • bool AddUser (string username, string password, string name, string email): This method will add a user to the database. If successful, the method will return true, if not the method will return false.
    • bool DeleteUser (string username): Will delete a user from the database. If successful the method will return true, if not the method will return false.
    • bool EditUser (string username, string name, string email): This method will edit the user information. If successful the method will return true, if not the method will return false.
    • bool ChangePassword (string username, string password): This method will change a user’s password. If successful the method will return true, if not the method will return false.
    • string ReturnName (string username): this method returns a users name.
    • string ReturnEmail (string username): this method returns a users email.
    Our example makes use of an SQL server database. Use the following TSQL in query analyzer to create our database (In our example I will assume that SQL Server is installed on the same machine as where the web service will reside):

    CREATE DATABASE minipassport

    GO

    CREATE TABLE Users (

    UserName varchar (10) Primary Key NOT NULL ,

    Name varchar (50) NOT NULL ,

    EMail varchar (100) NOT NULL ,

    Password varchar (10) NOT NULL

    ) ON PRIMARY

    GO


    The code for our web service looks like this:

    <%@ WebService class = "miniPassport" Language="C#" Debug = "true"%>

    using System;

    using System.Data;

    using System.Data.SqlClient;

    using System.Web.Services;

    [WebService(Name ="Mini Passport", Description="Web Service to Authenticate and Manage Users", Namespace = "devArticles")]

    public class miniPassport

    {

    const string connStr = "server=127.0.0.1;uid=sa;pwd=;database=minipassport";

    [WebMethod(Description = "Method to Authenticate Users")]

    public bool Authenticate(string username, string password)

    {

    SqlConnection dbConn = new SqlConnection(connStr);

    string sqlStr = "Select password from users where username = '" + username + "';";

    dbConn.Open();

    SqlCommand dbCommand = new SqlCommand(sqlStr,dbConn);

    SqlDataReader dbReader = dbCommand.ExecuteReader();



    bool returnBool;

    if (dbReader.Read())

    {

    if (dbReader[0].ToString()==password)

    {

    returnBool = true;

    }

    else

    {

    returnBool = false;

    }

    }

    else

    {

    returnBool=false;

    }

    dbReader.Close();

    dbConn.Close();

    return returnBool;

    }

    [WebMethod(Description = "Method to Add User")]

    public bool AddUser(string username, string password, string name, string email)

    {

    bool returnBool = false;

    SqlConnection dbConn = new SqlConnection(connStr);

    string sqlStr = "INSERT INTO users(username,password,name,email) values('" + username + "', '" + password + "', '" + name + "', '" + email + "');";

    SqlCommand dbCommand = new SqlCommand(sqlStr,dbConn);

    try

    {

    dbConn.Open();

    if (dbCommand.ExecuteNonQuery()!=0)

    {

    returnBool=true;

    }

    returnBool=true;

    }

    catch

    {

    returnBool=false;

    }

    dbConn.Close();

    return returnBool;

    }

    [WebMethod(Description = "Method to Delete User")]

    public bool DeleteUser(string username)

    {

    bool returnBool = false;

    SqlConnection dbConn = new SqlConnection(connStr);

    string sqlStr = "DELETE FROM users where username = '" + username +"';";

    SqlCommand dbCommand = new SqlCommand(sqlStr,dbConn);

    try

    {

    dbConn.Open();

    if (dbCommand.ExecuteNonQuery()!=0)

    {

    returnBool=true;

    }

    }

    catch

    {

    returnBool=false;

    }

    dbConn.Close();

    return returnBool;

    }

    [WebMethod(Description = "Method to Edit User Information")]

    public bool EditUser(string username, string name, string email)

    {

    bool returnBool = false;

    SqlConnection dbConn = new SqlConnection(connStr);

    string sqlStr = "UPDATE users SET username = '" + username +"',name = '"+name+"',email= '"+email+"';";

    SqlCommand dbCommand = new SqlCommand(sqlStr,dbConn);

    try

    {

    dbConn.Open();

    if (dbCommand.ExecuteNonQuery()!=0)

    {

    returnBool=true;

    }

    }

    catch

    {

    returnBool=false;

    }

    dbConn.Close();

    return returnBool;

    }

    [WebMethod(Description = "Method to Change User Password")]

    public bool ChangePassword(string username, string password)

    {

    bool returnBool = false;

    SqlConnection dbConn = new SqlConnection(connStr);

    string sqlStr = "UPDATE users SET password = '"+password+"';";

    SqlCommand dbCommand = new SqlCommand(sqlStr,dbConn);

    try

    {

    dbConn.Open();

    if (dbCommand.ExecuteNonQuery()!=0)

    {

    returnBool=true;

    }

    }

    catch

    {

    returnBool=false;

    }

    dbConn.Close();

    return returnBool;

    }

    [WebMethod(Description = "Method to Obtain User Name")]

    public string ReturnName(string username)

    {

    SqlConnection dbConn = new SqlConnection(connStr);

    string sqlStr = "Select Name from users where username = '" + username + "';";

    dbConn.Open();

    SqlCommand dbCommand = new SqlCommand(sqlStr,dbConn);

    SqlDataReader dbReader = dbCommand.ExecuteReader();

    dbReader.Read();

    string _name = dbReader[0].ToString();

    dbReader.Close();

    dbConn.Close();

    return _name;

    }

    [WebMethod(Description = "Method to obtain User Email Address")]

    public string ReturnEmail(string username)

    {

    SqlConnection dbConn = new SqlConnection(connStr);

    string sqlStr = "Select email from users where username = '" + username + "';";

    dbConn.Open();

    SqlCommand dbCommand = new SqlCommand(sqlStr,dbConn);

    SqlDataReader dbReader = dbCommand.ExecuteReader();

    dbReader.Read();

    string _name = dbReader[0].ToString();

    dbReader.Close();

    dbConn.Close();

    return _name;

    }

    }


    As you can see, there's nothing difficult about our code. It's composed from what we've covered throughout this article. If you add your own functionality and make it available on the web (or even register it on UDDI), then it's a complete authentication web service. This will allow other sites to incorporate our demo login system and centralize user information.

    Our mini passport web service

    More ASP.NET Articles
    More By James Yang


       · Perfect Instruction given for the web services
     

    ASP.NET ARTICLES

    - How Caching Means More Ca-ching, Part 2
    - How Caching Means More Ca-ching, Part 1
    - Reading a Delimited File Using ASP.Net and V...
    - What is .Net and Where is ASP.NET?
    - An Object Driven Interface with .Net
    - Create Your Own Guestbook In ASP.NET
    - HTTP File Download Without User Interaction ...
    - Dynamically Using Methods in ASP.NET
    - Changing the Page Size Interactively in a Da...
    - XML Serialization in ASP.NET
    - Using Objects in ASP.NET: Part 1/2
    - IE Web Controls in VB.NET
    - Class Frameworks in VB .NET
    - Cryptographic Objects in C#: Part 1
    - Sample Chapter: Pure ASP.Net







    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway