Step 1: Create a new WCF Service
Step 2: Add A Commom.Cs file in
App_Code folder.
And write these code.
using
System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Web;
using
System.Configuration;
using
System.Data;
using
System.Data.SqlClient;
///
<summary>
///
Summary description for Common
///
</summary>
public
class
Common
{
public static
string str = ConfigurationManager.ConnectionStrings["conn"].ToString();
SqlConnection conn =
new SqlConnection(str);
public Common()
{
}
#region
Check Login
public bool Check_Login(string
User_Name, string Password)
{
bool result = false;
SqlCommand cmd = new
SqlCommand("usp_check_login",
conn);
cmd.CommandTimeout =
0;
cmd.CommandType =
CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@username",
User_Name);
cmd.Parameters.AddWithValue("@pass",
Password);
SqlDataReader dr;
conn.Open();
dr =
cmd.ExecuteReader();
dr.Read();
if (dr[0].ToString() ==
"True")
{
result =
true;
}
else
{
result =
false;
}
conn.Close();
return result;
}
#endregion
# region
Register
public string Register_Login(string
Username, string User_Pass,
string F_Name, string
L_Name, string Address1,
string mobile, string
City)
{
string result =
"Error";
SqlCommand cmd = new
SqlCommand("Usp_Register_User",
conn);
cmd.CommandType =
CommandType.StoredProcedure;
cmd.CommandTimeout =
0;
cmd.Parameters.AddWithValue("@Username1",
Username);
cmd.Parameters.AddWithValue("@User_Pass",
User_Pass);
cmd.Parameters.AddWithValue("@F_Name", F_Name);
cmd.Parameters.AddWithValue("@L_Name", L_Name);
cmd.Parameters.AddWithValue("@Address1",
Address1);
cmd.Parameters.AddWithValue("@mobile",
mobile);
cmd.Parameters.AddWithValue("@City", City);
conn.Open();
SqlDataReader dr;
dr =
cmd.ExecuteReader();
dr.Read();
if (dr[0].ToString().Contains("Error"))
{
result =
"Error !!!";
}
else
{
result =
"Succsess";
}
return result;
}
#endregion
}
Step 3: On IService.cs page write this
code
using
System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Runtime.Serialization;
using
System.ServiceModel;
using
System.ServiceModel.Web;
using
System.Text;
// NOTE: You
can use the "Rename" command on the "Refactor" menu to change the interface name
"IService" in both code and config file together.
[ServiceContract]
public
interface
IService
{
[OperationContract]
string GetData(int value);
[OperationContract]
bool Check_Login(String U_Id,
String U_Pass);
[OperationContract]
[FaultContract(typeof(string))]
string Register_Login(string Username,
string User_Pass,
string F_Name, string L_Name,
string Address1, string
mobile, string City);
}
// Use a data
contract as illustrated in the sample below to add composite types to service
operations.
[DataContract()]
public
class
ServiceData
{
[DataMember]
public bool Result {
get; set; }
[DataMember]
public string ErrorMessage {
get; set; }
[DataMember]
public string ErrorDetails {
get; set; }
}
Step 4: On Service.cs page wrote the
below code.
using
System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Runtime.Serialization;
using
System.ServiceModel;
using
System.ServiceModel.Web;
using
System.Text;
// NOTE: You
can use the "Rename" command on the "Refactor" menu to change the class name
"Service" in code, svc and config file together.
public
class Service
: IService
{
Common cm =
new Common();
public string GetData(int
value)
{
return string.Format("You
entered: {0}", value);
}
public bool Check_Login(String
U_Id, String U_Pass)
{
bool result = false;
result =
cm.Check_Login(U_Id, U_Pass);
return result;
}
public string Register_Login(string
Username, string User_Pass,
string F_Name, string
L_Name, string Address1,
string mobile, string
City)
{
string result =
"Error";
ServiceData
myServiceData = new ServiceData();
try
{
result =
cm.Register_Login(Username, User_Pass, F_Name, L_Name, Address1, mobile, City);
}
catch (Exception
sqlEx)
{
//myServiceData.Result = true;
//myServiceData.ErrorMessage =
"Connection can not open this time either connection string is wrong or Sever is
down. Try later";
//myServiceData.ErrorDetails
= sqlEx.ToString();
string str = "Error
in Service";
throw new
FaultException<string>(str,
"Error In SQL Stored Procedure!!!");
}
return result;
}
}
Step 5: In web.config file..white the
below code
<system.serviceModel>
<services>
<service
name="Service"
behaviorConfiguration="ServiceBehavior">
<!--
Service Endpoints
-->
<endpoint
address=""
binding="wsHttpBinding"
contract="IService">
<!--
Upon
deployment, the following identity element should be removed or replaced to
reflect the
identity under
which the deployed service runs. If removed, WCF will infer an appropriate
identity
automatically.
-->
<identity>
<dns
value="localhost"/>
</identity>
</endpoint>
<endpoint
address="mex"
binding="mexHttpBinding"
contract="IMetadataExchange"/>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior
name="ServiceBehavior">
<!--
To avoid disclosing metadata information, set the value below to false and
remove the metadata endpoint above before deployment
-->
<serviceMetadata
httpGetEnabled="true"/>
<!--
To receive exception details in faults for debugging purposes, set the value
below to true. Set to false before deployment to avoid disclosing exception
information
-->
<serviceDebug
includeExceptionDetailInFaults="false"/>
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
Step 6: Add Connection string in config
file.
<connectionStrings>
<add
name="conn"
connectionString="server=.\SQL;User
Id=sa;Password=abc@123;Initial Catalog=test"/>
</connectionStrings>
Step 7: Run and save file for
Client application ithat you can see in my next blog. For more please find the
attachment.