Friday, August 3, 2012

Create service in wcf use SQl server for data fetching and inserting

create new service use SQl server database with sample code in wcf use c#


Step 1: copy this code to  Iservice 



using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using System.ServiceModel.Web;


[ServiceContract]
public interface IService
{
    [OperationContract]
    List GetCustomer(string Custname);


    [OperationContract]
    string InsertCustomer(Customerdetail CustInfo);


}




[DataContract]
public class Customerdetail
{
    string CustName = string.Empty;
    string  DOB = string .Empty ;
    string Profession = string.Empty;
    int CustId;


    [DataMember]
    public int CustomerId
    {
        get
        {
            return CustId;
        }
        set
        {
            CustId = value;
        }
    }




    [DataMember]
    public string  DateOfBirth
    {
        get
        {
            return DOB;
        }
        set
        {
            DOB = value;
        }
    }


    [DataMember]
    public string Customer
    {
        get
        {
            return CustName ;
        }
        set
        {
            CustName = value;
        }
    }


    [DataMember]
    public string Professional
    {
        get
        {
            return Profession;
        }
        set
        {
            Profession = value;
        }
    }
}






 Then add this code to Secrvice.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;


public class Service : IService
{


    string strCon = System.Configuration.ConfigurationManager.ConnectionStrings["ConStr"].ConnectionString;


    SqlConnection con = new SqlConnection(@"Data Source=192.168.0.21\SQLSERVER2008;Initial Catalog=Payroll;User ID=sa;Password=@rajkumar");


    public List GetCustomer(string Custname)
    {
        
        List custdetail = new List();
        {
            con.Open();
            SqlCommand cmd = new SqlCommand("select * from Customer where Customer Like '%'+@CustName+'%'", con);
            cmd.Parameters.AddWithValue("@CustName", Custname);
            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++)
                {
                    Customerdetail CustInfo = new Customerdetail();
                    CustInfo.CustomerId  = Convert .ToInt32(dt.Rows[i]["CustomerId"].ToString());
                    CustInfo.Customer = dt.Rows[i]["Customer"].ToString();
                    CustInfo.DateOfBirth  = dt.Rows[i]["DOB"].ToString();
                    CustInfo.Professional  = dt.Rows[i]["Professional"].ToString();
                    custdetail.Add(CustInfo);
                }
            }
            con.Close();
        }
        return custdetail;
    }


    public string InsertCustomer(Customerdetail CustInfo)
    {
        string strMessage = string.Empty;
        con.Open();
        SqlCommand cmd = new SqlCommand("insert into Customer(Customer,DOB,Professional) values(@Customer,@DOB,@Profession)", con);
        cmd.Parameters.AddWithValue("@Customer", CustInfo.Customer );
        cmd.Parameters.AddWithValue("@DOB", CustInfo.DateOfBirth);
        cmd.Parameters.AddWithValue("@Profession", CustInfo.Professional );
        int result = cmd.ExecuteNonQuery();
        if (result == 1)
        {
            strMessage = CustInfo.Customer  + " Details inserted successfully";
        }
        else
        {
            strMessage = CustInfo.Customer + " Details not inserted successfully";
        }
        con.Close();
        return strMessage;
    }
}




Then add the service  reference to  your web application  After then 


create  aspx page 





<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication1._Default" %>





    Test Anlon Service

   

   

       

           
               
                   
               
           
           
           
               
                    Name
               
               
                   
               
           
           
               
                    DOB
               
               
                   
               
           
           
               
                    Professional
               
               
                   
               
           
           
           
               
                   
                   
               
           
           
               
                   
               
               
                   
               
           
       
   
   





Then  copy and paste the below  code to aspx.cs


using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.ServiceModel;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using WebApplication1.ServiceReference1;


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

        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void btnData_Click(object sender, EventArgs e)
        {


            gvData.DataSource = Custdetail.GetCustomer(txtName.Text);
            gvData.DataBind();
        }

        protected void btnInsert_Click(object sender, EventArgs e)
        {

            Customerdetail CustInfo = new Customerdetail();
            CustInfo.Customer  = txtName.Text;
            CustInfo.DateOfBirth = txtDOB.Text;
            CustInfo.Professional  = txtProfession .Text;

            string result = Custdetail.InsertCustomer(CustInfo);
            lblResult.Text = result;

            
            gvData.DataSource = Custdetail.GetCustomer(string.Empty);
            gvData.DataBind();

            txtName.Text = string.Empty;
            txtDOB.Text = string.Empty;
            txtProfession.Text = string.Empty;
            
        }

    }
}
















Tuesday, July 7, 2009