WCF Service
How to check if a User ID already exits or not in the database using a WCF Service.
In this article, I am going to discuss how to create a WCF Service and use in asp.net 3.5. You can also use this service at registration time to check duplicate data.
How to Create a WCF service
Step 1: To create new WCF Service Open new web site and select WCF service like as follows:
Step 2: By default you will get the following page.
Step 3: Now add the following code into Service class to get record from the database.
- //This function is used to get Userid from database.
- public DataSet GetUserId()
- {
- SqlConnection con = new SqlConnection("Data Source = Puru-SQLSERVER2005; Initial Catalog = Puru; User ID = sa; Password = wintellect;");
- string cmd = "select UserID from Registration";
- SqlDataAdapter da = new SqlDataAdapter(cmd, con);
- con.Open();
- DataSet ds = new DataSet();
- da.Fill(ds);
- con.Close();
- return ds;
- }
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Runtime.Serialization;
- using System.ServiceModel;
- using System.Text;
- using System.Data;
- using System.Data.SqlClient;
- public class Service: IService {
- public string GetData(int value)
- {
- return string.Format("You entered: {0}", value);
- }
- public CompositeType GetDataUsingDataContract(CompositeType composite)
- {
- if (composite.BoolValue)
- {
- composite.StringValue += "Suffix";
- }
- return composite;
- }
- //This function is used to get Userid from database.
- public DataSet GetUserId()
- {
- SqlConnection con = new SqlConnection("Data Source = Puru-SQLSERVER2005; Initial Catalog = Puru; User ID = sa; Password = wintellect;");
- string cmd = "select UserID from Registration";
- SqlDataAdapter da = new SqlDataAdapter(cmd, con);
- con.Open();
- DataSet ds = new DataSet();
- da.Fill(ds);
- con.Close();
- return ds;
- }
- }
- <%@ ServiceHost Language="C#" Debug="true" Service="Service" CodeBehind="~/App_Code/Service.cs" %>
Build the application and debug; you will get the following output.


olivermodePosted Apr 8, 2017, 8:40 AM
Thank you! a great tutorial
Niresh kumarPosted Jan 11, 2013, 4:53 AM
very nice example to learn basic of WCF....thank u...
Kapil GargPosted Feb 20, 2012, 6:54 AM
Nice examle..Keep it up Man
vinod singhPosted Aug 5, 2011, 7:39 AM
Nice example to learn the basic wcf
love sharmaPosted Feb 16, 2011, 9:46 AM
Very Good Example to understand the basic of WCF.