Monday, 11 June 2012

Insert,Update and Delete through the WCF.

1.First add a webService .
2.Two page is add in our application
      a.IService.cs
      b.Service.cs
3.Write This Coading in Service.cs.
----------------------------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;

// 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
{
    SqlConnection con = new SqlConnection(@"server=SatyaaH-DBS\gshhhjs;database=ssss;uid=ssss;pwd=aa@sdsd");

    public string InsertData(int ID, string Name)
    {
        con.Open();
        SqlCommand cmd = new SqlCommand("Insert into DemoTBL(ID,Name) values('"+ID+"','"+Name+"') ",con);
        cmd.ExecuteNonQuery();
        con.Close();
        return "Success";
    }
 
    public string GetData(int value)
    {
        return string.Format("You entered: {0}", value);
    }

    public UserDetails GetDataUsingDataContract(UserDetails composite)
    {
        return composite;
    }
    public List<UserDetails> GetUserDetails1(string Username)
    {
        List<UserDetails> userdetails = new List<UserDetails>();
        {
            con.Open();
            SqlCommand cmd = new SqlCommand("select ID,Name from DemoTBL where Name Like '%'+@Name+'%'", con);
            cmd.Parameters.AddWithValue("@Name", Username);
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataTable dt = new DataTable();
            da.Fill(dt);
            if (dt.Rows.Count > 0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    UserDetails userInfo = new UserDetails();
                    userInfo.ID = Convert.ToInt32(dt.Rows[i]["ID"].ToString());
                    userInfo.Name = dt.Rows[i]["Name"].ToString(); 
                    userdetails.Add(userInfo);
                }
            }
            con.Close();
        }
        return userdetails;
    }

    public string DeleteData(int ID)
    {
        con.Open();
        SqlCommand cmd = new SqlCommand("Delete from DemoTBL where ID='" + ID + "' ", con);
        cmd.ExecuteNonQuery();
        con.Close();
        return "Deleted";
    }

    public string UpdateData(int ID,string Name)
    {
        con.Open();
        SqlCommand cmd = new SqlCommand("Update  DemoTBL set Name='"+Name+"' where ID='" + ID + "' ", con);
        cmd.ExecuteNonQuery();
        con.Close();
        return "Updated";
    }
    public object Name
    {
        get;
        set;
    }
}
--
--------------------------------------------------------------------------------------
4.Write this coading IService.cs
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 DeleteData(int ID);
    [OperationContract]
    string
InsertData(int  ID,string Name);
    [OperationContract]
    List<UserDetails> GetUserDetails1(string Username);
    [OperationContract]
    string UpdateData(int ID, string Name);
}

// Use a data contract as illustrated in the sample below to add composite types to service operations.
[DataContract]
public class UserDetails
{
    [DataMember]
    public int ID
    {
        get;
        set;
    }
    [DataMember]
    public string Name
    {
        get;
        set;
    }
  
}

5.Next Add a page in your WebApplication.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ServiceReference1;

public partial class _Default : System.Web.UI.Page
{
    ServiceReference1.ServiceClient obj = new ServiceReference1.ServiceClient();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindUserDetails();
        }
    }
    protected void BindUserDetails()
    {
        IList<UserDetails> objUserDetails = new List<UserDetails>();
        objUserDetails = obj.GetUserDetails1("");

        GridView1.DataSource = objUserDetails;
        GridView1.DataBind();
    }
  
    protected void btn_update_Click(object sender, EventArgs e)
    {
        Response.Write(obj.UpdateData(Convert.ToInt32(TextBox1.Text), TextBox2.Text));
        BindUserDetails();
    }
    protected void btn_delete_Click(object sender, EventArgs e)
    {
        Response.Write(obj.DeleteData(Convert.ToInt32(TextBox1.Text)));
        BindUserDetails();
    }
    protected void btn_save_Click(object sender, EventArgs e)
    {
        Response.Write(obj.
InsertData(Convert.ToInt32(TextBox1.Text), TextBox2.Text));
        BindUserDetails();
    }
}

6.Design Page


<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true"
    CodeFile="Default.aspx.cs" Inherits="_Default" %>

<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
</asp:Content>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
    <asp:GridView ID="GridView1" runat="server">
</asp:GridView>
    <br />
    <br />
    <br />
    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
    <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
    <asp:Button ID="btn_save" runat="server"  Text="Save"
        onclick="btn_save_Click" />
    <asp:Button ID="btn_update" runat="server" Text="Update"
        onclick="btn_update_Click"/>
    <asp:Button ID="btn_delete" runat="server" Text="Delete"
        onclick="btn_delete_Click" />
</asp:Content>
 7.Source Code Design


8.Database Desin


No comments:

Post a Comment