In this article I’m going to explain how to insert and display records to SQL Server database using WCF.

About WCF:

Windows Communication Foundation (WCF) is an extension of the .NET framework to buildand run connected systems. Windows Communication provides a unified framework for building secure and reliable transacted Web services. Windows Communication Foundation  combines and extends the capabilities of Distributed Systems, Microsoft .NET Remoting, Web Services, and Web Services Enhancements (WSE), to develop and deliver unified secured systems.

Table Design:

Column Name

Data Type

empid

varchar(50)

name

varchar(100)

designation

varchar(50)

city

varchar(50)

country

varchar(50)

 

Create table script:

CREATE TABLE [dbo].[EmployeeDetails](

          [empid] [varchar](50) NULL,

          [name] [varchar](100) NULL,

          [designation] [varchar](100) NULL,

          [city] [varchar](50) NULL,

          [country] [varchar](50) NULL

) ON [PRIMARY]


Step-1:
          Open visual studio and start new Project

     
Step-2:
           Select WCF Service Application

                
Step-3:
           Now click on the Ok . Then you will get 3 files in your project folder
  1. IService.cs
  2. Service.svc
  3. Service.svc.cs


here you should know about following Contracts

Service Contract:

              Service contract is a contract that specifies the direction and type of the messages in a conversation. It is an interface or a class that defines the service contract in a Windows Communication Foundation (WCF)  application.

            A service contract is the gateway to a service for external applications to make use of the service  functions, and at least one service contract should be available in a service.

Operation Contract:

              An operation contract defines the methods of the service that are accessible by external systems. The  OperationContract attribute needs to be applied for all these methods, these are also like web methods in a  web service. 

Data Contract:

             A data contract defines a type with a set of data members or fields that will be used as the composite  type in a service contract. It is a loosely-coupled model that is defined outside the implementation of the  service and accessible by services in other platforms. To define a data contract, apply the DataContract  attribute to the class to serialize the class by a serializer, and apply the DataMember attribute to the fields in  the class that must be serialized.

Data Member:

            A data member specifies the type which is part of a data contract used as a composite type member of  the contract. To define a data member, apply the DataMember attribute to the fields that must be serialized.  The DataMember attribute can be applied to private properties, but they will be serialized and deserialized,  and will be accessible to the user or process. 


Step-4:
          Add following code to IService1.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.Data; 

[ServiceContract]

public interface IService

{

    [OperationContract]

    string InsertEmloyeeDetails(Employee_Details objempdetails); 

    [OperationContract]

    DataSet BindEmployeeDetails();

} 

[DataContract]

public class Employee_Details

{

    private string _empid, _name, _designation, _city, _country; 

    [DataMember]

    public string empid

    {

        get { return _empid; }

        set { _empid = value; }

    }

    [DataMember]

    public string name

    {

        get { return _name; }

        set { _name = value; }

    }

    [DataMember]

    public string designation

    {

        get { return _designation; }

        set { _designation = value; }

    }

    [DataMember]

    public string city

    {

        get { return _city; }

        set { _city = value; }

    }

    [DataMember]

    public string country

    {

        get { return _country; }

        set { _country = value; }

    }

}

 

 Step-5:

          Add following code to Service1.svc.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.Data.SqlClient;

using System.Data;

public class Service : IService

{

     SqlConnection conn = new SqlConnection("Data Source=SPIDER;Initial Catalog=Demo;Integrated Security=True");   

    public string InsertEmloyeeDetails(Employee_Details objempdetails)

    {

        string MessageOutput;

        conn.Open();

        string cmdstr="insert into EmployeeDetails(empid,name,designation,city,country) values(@empid,@name,@designation,@city,@country)";

        SqlCommand cmd = new SqlCommand(cmdstr,conn);

        cmd.Parameters.AddWithValue("@empid", objempdetails.empid);

        cmd.Parameters.AddWithValue("@name", objempdetails.name);

        cmd.Parameters.AddWithValue("@designation", objempdetails.designation);

        cmd.Parameters.AddWithValue("@city", objempdetails.city);

        cmd.Parameters.AddWithValue("@country", objempdetails.country);

        int result = cmd.ExecuteNonQuery();

        if (result == 1)

        {

            MessageOutput = "Records Inserted";

        }

        else

        {

            MessageOutput = " Records not Inserted";

        }

        conn.Close();

        return MessageOutput;

    }

    public DataSet BindEmployeeDetails()

    {

        conn.Open();

        string cmdstr = "Select * from EmployeeDetails";

        SqlCommand cmd = new SqlCommand(cmdstr, conn);

        SqlDataAdapter adp = new SqlDataAdapter(cmd);

        DataSet ds = new DataSet();

        adp.Fill(ds);

        cmd.ExecuteNonQuery();

        conn.Close();

        return ds;

    }

}

Step-6:
          Press F5 to run the application. Now WCF Test Client will be displayed and it will be
 load your service.
Step-7:
          Now you just double click the function which is you have created. There you will be
 able to see the  function's details which is include Name, Value and Type 
    Now your service will be created successfully. Now you can open your service in the
 browser by right click to view in browser option in the Service1.svc file. 
    
Step-8:
           Now you have to create new website by right click to the solution file 
Step-9:
          Now you just add new App_WebReferences folder by right click your website 
Step-10:
          Now we have to add service reference by right click the App_WebReferences folder. 
Step-11:
      Now you just click Discover button, it will be find service reference. here you can
 change the "namespace"  name 
Step-12:
      Now your service reference will be added successfully 
    After created your service reference, the end point will be created automatically in your
 web config. 
Step-13:
      After created the service reference if you did any changes to the service then you should
 update the service  reference
Step-14:
      Now you can create your aspx page      

Designer Source Code:

Ex: Default.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" 

Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD

XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head runat="server">

    <title>Demo</title></head>

<body>

    <form id="form1" runat="server">

    <div>

        <table width="600px" align="center">

            <tr>

              <td colspan="2" align="center"><b>Employee Details</b></td>

            </tr>

            <tr>

                <td>Employee ID</td>

              <td><asp:TextBox ID="txtEmpID" runat="server"></asp:TextBox></td>

            </tr>

            <tr>

                <td> Name </td>

                <td> <asp:TextBox ID="txtName" runat="server"></asp:TextBox> </td>

            </tr>

            <tr>

                <td> Designation </td>

                <td> <asp:TextBox ID="txtDesignation" runat="server"></asp:TextBox> </td>

            </tr>

            <tr>

                <td> City </td>

                <td> <asp:TextBox ID="txtCity" runat="server"></asp:TextBox></td>

            </tr>

            <tr>

                <td> Country </td>

                <td><asp:TextBox ID="txtCountry" runat="server"></asp:TextBox>

                </td>

            </tr>

            <tr> <td colspan="2" align="center"> <asp:Button ID="btnSubmit" runat="server" OnClick="btnSubmit_Click" Text="Submit" /> </td>

            </tr>

            <tr>

            <td colspan="2"><asp:Label ID="lblMsg" runat="server" ></asp:Label> </td>

            </tr>

            <tr>

            <td colspan="2">

            <asp:GridView ID="gvEmployeeDetails" runat="server" Width="100%">

</asp:GridView>

            </td>

            </tr>

        </table>

    </div>

    </form>

</body>

</html>

Code Behind:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web; 

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Xml;

using System.Data;

using Service;

public partial class _Default : System.Web.UI.Page

{

    protected void Page_Load(object sender, EventArgs e)

    {

        if (!IsPostBack)

        {

           BindData();

        }

    }

    protected void BindData()

    {

        ServiceClient client = new ServiceClient();

        DataSet ds = new DataSet();

        DataTable FromTable = new DataTable();

        try

        {

           client.Open();

            ds = client.BindEmployeeDetails();

            FromTable = ds.Tables[0];

            if (FromTable.Rows.Count > 0)

            {

                gvEmployeeDetails.DataSource =FromTable;

                gvEmployeeDetails.DataBind();

            }

            else

            {

                FromTable.Rows.Add(FromTable.NewRow());

                gvEmployeeDetails.DataSource =FromTable;

               gvEmployeeDetails.DataBind();

                int TotalColumns = gvEmployeeDetails.Rows[0].Cells.Count;

                gvEmployeeDetails.Rows[0].Cells.Clear();               

gvEmployeeDetails.Rows[0].Cells.Add(new TableCell());               

gvEmployeeDetails.Rows[0].Cells[0].ColumnSpan = TotalColumns;               

gvEmployeeDetails.Rows[0].Cells[0].Text = "No records Found";

            }

        }

        catch (Exception ex)

        {

        }

        finally

       {

            ds.Dispose();

            client.Close();

        }

    }

    protected void btnSubmit_Click(object sender, EventArgs e)

    {

        ServiceClient client = new ServiceClient();

        Employee_Details objemp = new Employee_Details();

      objemp.empid = txtEmpID.Text;

        objemp.name = txtName.Text;

        objemp.designation = txtDesignation.Text;

        objemp.city = txtCity.Text;

        objemp.country = txtCountry.Text;

        string result = client.InsertEmloyeeDetails(objemp);

        lblMsg.Text = result;

        BindData();

    }

}

cytotec abortion

http://www.extrageek.com/template/default.aspx?an-abortion-pill buy abortion pills online
medical abortion pill online cytotec abortion abortion pill


View demo Download
http://www.dotnetfox.com/Document/1004/1004Demo2.gif

If you enjoyed this article, get email updates (it's free).


Related Articles
  • How to insert and display data using WCFIn this article i'm going to explain how to insert records using WCF. Windows Communication Foundation (WCF) is an extension of the .NET framework to build and run connected systems. Windows Communication Foundation provides a unified framework for building secure and reliable transacted Web services. Windows Communication Foundation combines and extends the capabilities of Distributed Systems, M...
  • How to insert edit delete update records in Grid view using WCF In this article i'm going to explain how to insert records using WCF. Windows Communication Foundation (WCF) is an extension of the .NET framework to build and run connected systems. Windows Communication Foundation provides a unified framework for building secure and reliable transacted Web services. Windows Communication Foundation combines and extends the capabilities of Distributed System...
  • What is endpoint in WCF ?In this article I’m going to explain what is endpoint in WCF (Windows Communication Foundation). Endpoints provide clients access to the functionality offered by a WCF service. Each endpoint consists of four properties: An address that indicates where the endpoint can be found. A binding that specifies how a client can communicate with the endpoint. A contract that identifies the operations availa...

Comments
comments powered by Disqus

Thanks for downloading..!!

×
Subscribe via Email
    
message to display in fancybox