WCF Insert Update Delete Edit Select Operation using Web Application in Asp.Net With SQL Server Databse
Introduction :
This Artical Explain how to create WCF service and How to use it here Describe Insert update delete Edit Select operation usin the WCF Service With SQL Server Database,
Step 1 :open Visual Studio 2010 or 2012 then, Create WCF Service Application
Step 3: Then create database with table name
Step 4 : Now, We can need to define DataContract,
The data contract is a formal agreement between service and client that abstractly describes the data to be exchanged or Communicate Between Client And Server. In Commonly DataContract Describe for each parameter or return type, what data is serialized ( XML) to be exchanged. In DataMember We cab Define Perameter or Return type. WCF work on client and server for Security purpose it can be use Serialization Thus We need to DataMember for Serialization and message or Data Security.
Create Class : Employee.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Web;
namespace Crude_4_WCF
{
[DataContract]
public class Employee
{
string Name;
string Address;
[DataMember]
public string name
{
get { return Name; }
set { Name = value; }
}
[DataMember]
public string address
{
get { return Address; }
set { Address = value; }
}
}
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Web;
namespace Crude_4_WCF
{
[DataContract]
public class Employee
{
string Name;
string Address;
[DataMember]
public string name
{
get { return Name; }
set { Name = value; }
}
[DataMember]
public string address
{
get { return Address; }
set { Address = value; }
}
}
Step 5 : IService1.cs , Now In this IService1 Interface we can declare the service interface of Service1.cs file and implement in Service1.cs
{
[ServiceContract]
public interface IService1
{
[OperationContract]
void Create(Employee emp);
[OperationContract]
DataSet Edit(Employee emp);
[OperationContract]
DataSet Read(Employee emp);
[OperationContract]
void Delete(Employee emp);
[OperationContract]
void Update(Employee emp);
}
}
SqlConnection con = new SqlConnection(@"Data Source=(LocalDB)\v11.0; TO SEE Video Click HereAttachDbFilename =D:\program\Crude_4_WCF\App_Data\DB1.mdf;Integrated Security=True");
{
con.Open();
SqlCommand cmd = new SqlCommand("insert into tbl_reg values('"+emp.name+"',
'"+emp.address+"')",con);
cmd.ExecuteNonQuery();
con.Close();
}
SqlCommand cmd = new SqlCommand("delete from tbl_reg where name='"+emp.name+"'", con);
cmd.ExecuteNonQuery();
con.Close();
}
public DataSet Read(Employee emp)
{
DataSet ds = new DataSet();
SqlDataAdapter adp = new SqlDataAdapter("select * from tbl_reg", con);
adp.Fill(ds);
return ds;
}
{
con.Open();
DataSet ds = new DataSet();
SqlDataAdapter adp = new SqlDataAdapter("select * from tbl_reg where name='"+emp.name+"'",con);
adp.Fill(ds);
return ds;
}
public void Update(Employee emp)
{
con.Open();
SqlCommand cmd = new SqlCommand();
cmd = new SqlCommand("Update tbl_reg set address='" + emp.address + "' Where name='" + emp.name + "'", con);
cmd.ExecuteNonQuery();
con.Close();
}
}
}
Step 8 : Add Service Refference to WebApplication, Right Click on Referance and click on Add Service Reference
Step 9 :After Creating Service Reference we can call the method using following
{
ServiceReference1.Service1Client ss = new Service1Client();
ServiceReference1.Employee em = new Employee();
protected void Page_Load(object sender, EventArgs e)
{
grid();
}
public void grid()
{
DataSet ds = new DataSet();
ds = ss.Read(em);
GridView1.DataSource = ds;
GridView1.DataBind();
}
protected void Submit_Click(object sender, EventArgs e)
{
em.name = TextBox1.Text;
em.address = TextBox2.Text;
ss.Create(em);
string msg = TextBox1.Text + "Data Entered";
Label1.Text = msg;
grid();
}
protected void Delete_Click(object sender, EventArgs e)
{
em.name = TextBox1.Text;
ss.Delete(em);
string msg = "your data deleted";
Label1.Text = msg;
grid();
}
protected void Edit_Click(object sender, EventArgs e)
{
em.name = Convert.ToString(TextBox1.Text);
DataSet ds = new DataSet();
ds = ss.Edit(em);
if (ds.Tables[0].Rows.Count > 0)
{
TextBox1.Text = ds.Tables[0].Rows[0]["name"].ToString();
TextBox2.Text = ds.Tables[0].Rows[0]["address"].ToString();
}
}
protected void Update_Click(object sender, EventArgs e)
{
em.name = TextBox1.Text;
em.address = TextBox2.Text;
ss.Update(em);
string msg = "your data Updated";
Label1.Text = msg;
grid();
}
}
See Other Tutorial :
See Other Tutorial :
Sign up here with your email
1 comments:
Write commentsबहुत बढ़िया पोस्ट है chetan पटेल को थैंक्स ये ब्लॉग बनाने के लिए॰
Replyfrom
http://computerhindiknowledge.blogspot.in/
ConversionConversion EmoticonEmoticon