Click here to Skip to main content
15,886,518 members
Articles / Web Development / ASP.NET

Web Services, a simple approach

Rate me:
Please Sign up or sign in to vote.
3.58/5 (32 votes)
24 Mar 2006CPOL5 min read 122.7K   1.5K   79  
This articles is an introduction to web services, with a simple illustration.
using System;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Runtime.InteropServices;
using System.Data.SqlClient;
using System.Configuration;
using System.Diagnostics;
using System.Data;
using System.Xml;

[WebService(Namespace = "http://localhost:2034/WebServices/", Description="This is my First WebServices Demo - By Vivek")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class Service : System.Web.Services.WebService
{
    public Service () {

        //Uncomment the following line if using designed components 
        //InitializeComponent(); 
    }

    [WebMethod]
    public string DisplayTextMsg() 
    {
        return "My First Service For Testing";
    }

    [WebMethod]
    public string Vivek()
    {
        return "My Name is Vivek.T";
    }

    [WebMethod]
    public XmlDocument GetDataFromDB()
    {
        string errorMessage = "";
        XmlDocument myDatas = new XmlDocument();
        //Connection string is stored in the web.config file as an appSetting
        string connectionString = ConfigurationSettings.AppSettings["DatabaseConnectionString"];
        SqlConnection dbConnection = null;
        // Open a connection to the database
        try
        {
            dbConnection = new SqlConnection(connectionString);
            dbConnection.Open();
        }
        catch (Exception ex)
        {
            errorMessage = ex.Message;
        }
        if (errorMessage == "")
        {
            // Build an insert command
            string SQL = "select * From ProfileContact";
            SqlCommand GetCustomerCmd = new SqlCommand(SQL, dbConnection);

            try
            {
                SqlDataAdapter custDA = new SqlDataAdapter();
                custDA.SelectCommand = GetCustomerCmd;
                DataSet custDS = new DataSet();
                custDA.Fill(custDS, "ProfileContact");
                myDatas.LoadXml(custDS.GetXml());
                dbConnection.Close();
               
            }
            catch (System.Exception ex)
            {
                errorMessage = ex.Message;

            }
            finally
            {
                dbConnection.Dispose();
            }
        }
        return myDatas;
    } 
    //end 
 		
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Chief Technology Officer at Zealots
Australia Australia
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions