- Back to Home »
- ASP.NET »
- wcf service to insert data into database from asp.net webform
Posted by :
Sudhir Chekuri
Thursday, 26 September 2013
Create a new webservice which creates two files in appcode folder. they are service and iservice.
operationcontract is the method that is shared.
code in iservice.cs
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;
// NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "IConnectService" in both code and config file together.
[ServiceContract]
public interface IConnectService
{
[OperationContract]
string InsertUserRegDetails(string emailid,string pwd,string gender);
}
code inside service.cs
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;
using System.Configuration;
// NOTE: You can use the "Rename" command on the "Refactor" menu to change the
class name "ConnectService" in code, svc and config file together.
public class ConnectService : IConnectService
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings
["ConStr"].ConnectionString);
public string InsertUserRegDetails(string emailid, string pwd, string gender)
{
string Status;
if (con.State == ConnectionState.Closed)
{
con.Open();
}
SqlCommand cmd = new SqlCommand("insert into tbl_Register values
(@emailid,@pwd,@gender)", con);
cmd.Parameters.AddWithValue("@emailid", emailid);
cmd.Parameters.AddWithValue("@pwd", pwd);
cmd.Parameters.AddWithValue("@gender", gender);
int result = cmd.ExecuteNonQuery();
if (result == 1)
{
Status = emailid + " registered successfully";
}
else
{
Status = emailid + " could not be registered";
}
con.Close();
return Status;
}
}
code in asp.net web form class to use webservice
operationcontract is the method that is shared.
code in iservice.cs
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;
// NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "IConnectService" in both code and config file together.
[ServiceContract]
public interface IConnectService
{
[OperationContract]
string InsertUserRegDetails(string emailid,string pwd,string gender);
}
code inside service.cs
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;
using System.Configuration;
// NOTE: You can use the "Rename" command on the "Refactor" menu to change the
class name "ConnectService" in code, svc and config file together.
public class ConnectService : IConnectService
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings
["ConStr"].ConnectionString);
public string InsertUserRegDetails(string emailid, string pwd, string gender)
{
string Status;
if (con.State == ConnectionState.Closed)
{
con.Open();
}
SqlCommand cmd = new SqlCommand("insert into tbl_Register values
(@emailid,@pwd,@gender)", con);
cmd.Parameters.AddWithValue("@emailid", emailid);
cmd.Parameters.AddWithValue("@pwd", pwd);
cmd.Parameters.AddWithValue("@gender", gender);
int result = cmd.ExecuteNonQuery();
if (result == 1)
{
Status = emailid + " registered successfully";
}
else
{
Status = emailid + " could not be registered";
}
con.Close();
return Status;
}
}
code in asp.net web form class to use webservice
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using ConnectServiceNamespace;
/// <summary>
/// Summary description for ConnectClass
/// </summary>
public class ConnectClass
{
ConnectServiceClient c = new ConnectServiceClient();
public ConnectClass()
{
//
// TODO: Add constructor logic here
//
}
public string C_Register(string emailid, string pwd, string gender)
{
string status = c.InsertUserRegDetails(emailid,pwd,gender);
return status ;
}
}
sql queries
create database db_connect
use db_connect
create table tbl_Register(sno bigint identity primary key,emailid varchar(50) unique,pwd varchar(15),gender varchar(10))
insert into tbl_Register(emailid,pwd,gender) values('sudhir@gmail.com','sudhir','Male')
select * from tbl_Register