wcf services create and get data

File-àNew-àwebsite-àselect WCFService-àok
Iservice.cs:-
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using System.Data;
// NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "IproductService" in both code and config file together.
[ServiceContract]
public interface IproductService
{
    [OperationContract]
    int InsertRecord(Product p);
    [OperationContract]
    int DeleteRecord(Product p);
    [OperationContract]
    DataSet GetProducts();
 }
[DataContract]
public class Product
{
    int productid;
    [DataMember]
    public int Productid
    {
        get { return productid; }
        set { productid = value; }
    }
    string pname;
    [DataMember]
    public string Pname
    {
        get { return pname; }
        set { pname = value; }
    }
    int qty;
    [DataMember]
    public int Qty
    {
        get { return qty; }
        set { qty = value; }
    }
    double price;
    [DataMember]
    public double Price
    {
        get { return price; }
        set { price = value; }
    }
}
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 "productService" in code, svc and config file together.
public class productService : IproductService
{
    //create the connection
    SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["constr"].ToString());
   public int InsertRecord(Product p)
    {
    //open the connection
        con.Open();
       //pass the procedure
        SqlCommand cmd = new SqlCommand("proc_addproduct", con);
       //mention that we are working with SP
        cmd.CommandType = CommandType.StoredProcedure;
       //pass the values to parameters
        cmd.Parameters.AddWithValue("@productid", p.Productid);
        cmd.Parameters.AddWithValue("@pname", p.Pname);
        cmd.Parameters.AddWithValue("@qty", p.Qty);
        cmd.Parameters.AddWithValue("@price", p.Price);
       //execute the procedure
       int i=cmd.ExecuteNonQuery();
       con.Close();
       return i;
    }
  public int DeleteRecord(Product p)
   {
       con.Open();
       SqlCommand cmd = new SqlCommand("proc_deleteproduct",con);
       cmd.CommandType = CommandType.StoredProcedure;
       cmd.Parameters.AddWithValue("@productid", p.Productid);
      int i=cmd.ExecuteNonQuery();
      con.Close();
      return i;
   }
 public DataSet GetProducts()
  {
      con.Open();
      SqlCommand cmd = new SqlCommand("proc_displaydata",con);
      cmd.CommandType = CommandType.StoredProcedure;
      SqlDataAdapter da = new SqlDataAdapter(cmd);
      con.Close();
      DataSet ds = new DataSet();
      da.Fill(ds, "product");
      return ds;
  }
}
Press f5 and copy  the service:-
File-àNew-àWebsite--àSelect ASP.net website-àAdd
Goto Default.aspx[Design]

Gotoàsolutionexplorer-àrc on project Add serviceRefernce
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ServiceReference1;
using System.Data;
public partial class _Default : System.Web.UI.Page
{
    ServiceReference1.IproductServiceClient obj = new IproductServiceClient();
    Product p = new Product();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            FillData();
        }
    }
    private void FillData()
    {
        DataSet ds = obj.GetProducts();
        GridView1.DataSource = ds;
        GridView1.DataBind();
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        p.Productid = int.Parse(TextBox1.Text);
        p.Pname = TextBox2.Text;
        p.Qty = int.Parse(TextBox3.Text);
        p.Price = double.Parse(TextBox4.Text);
       int i=obj.InsertRecord(p);
       if (i==1)
       {
           FillData();
           Page.RegisterStartupScript("aaa", "<script>alert('Product is added successfully')</script>");
       }
       else
       {
           Page.RegisterStartupScript("aaa", "<script>alert('Insertion failed')</script>");
       }
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        p.Productid = int.Parse(TextBox5.Text);
       int i= obj.DeleteRecord(p);
       if (i==1)
       {
           FillData();
           Page.RegisterStartupScript("aaa", "<script>alert('Product is deleted successfully')</script>");
       }
       else
       {
           Page.RegisterStartupScript("aaa", "<script>alert('Deletion failed')</script>");
       }
    }
}
Storedprocedure:-
create procedure proc_addproduct(@productid int,
@pname varchar(20),@qty int,@price money)
as begin
insert into product values(@productid,
@pname,@qty,@price)
end
create procedure proc_deleteproduct(
@productid int)
as begin
delete from product where productid=@productid
end
create procedure proc_displaydata
as begin
select * from product
end



Comments

Popular posts from this blog

How to write Pure java script Program?