1
Reply

how to retrive the data from sqlserver using wcfservices

Phani Raj

Phani Raj

Dec 28 2012 10:20 AM
1.4k
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;

// NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "IEmpService" in both code and config file together.
[ServiceContract]
public interface IEmpService
{
  [OperationContract]
  void FindRec(EmpData obj);
}
[DataContract]
public class EmpData
{
  int EmpId, Deptno;
  string EName, Designation;
  DateTime DOJ; double Salary;
  [DataMember]
  public int PEmpId { get { return EmpId; } set { EmpId = value; } }
  [DataMember]
  public string PEName { get { return EName; } set { EName = value; } }
  [DataMember]
  public string PDesignation { get { return Designation; } set { Designation = value; } }
  [DataMember]
  public DateTime PDOJ { get { return DOJ; } set { DOJ = value; } }
  [DataMember]
  public double PSalary { get { return Salary; } set { Salary = value; } }
  [DataMember]
  public int PDeptno { get { return Deptno; } set { Deptno = value; } }
}
.cs file..
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using System.Data.SqlClient;

// NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "EmpService" in code, svc and config file together.
public class EmpService : IEmpService
{
  public void FindRec(EmpData obj)
  {
  SqlConnection Con = new SqlConnection("Server=.;User Id=sa;Password=abc;Database=Phani");
  string S = "select * from EmpDetails where EmpId=" + obj.PEmpId;
  SqlCommand cmd = new SqlCommand(S, Con);
  Con.Open();
  SqlDataReader Dr = cmd.ExecuteReader();
  if (Dr.Read())
  {
  obj.PEName = Dr[1].ToString();
  obj.PDesignation = Dr[2].ToString();
  obj.PDOJ = Convert.ToDateTime(Dr[3]);
  obj.PSalary = Convert.ToDouble(Dr[4]);
  obj.PDeptno = Convert.ToInt32(Dr[5]);
  }
  Con.Close();
 
  }
}
client application code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SR1;

public partial class _Default : System.Web.UI.Page
{
  protected void Page_Load(object sender, EventArgs e)
  {

  }
  EmpServiceClient objs = new EmpServiceClient();
  EmpData objd = new EmpData();
  protected void Button1_Click(object sender, EventArgs e)
  {
  objd.PEmpId = Convert.ToInt32(TextBox1.Text);
  objs.FindRec(objd);
  TextBox2.Text = objd.PEName;
  TextBox3.Text = objd.PDesignation;
  TextBox4.Text = objd.PDOJ.ToString();
  TextBox5.Text = objd.PSalary.ToString();
  TextBox6.Text = objd.PDeptno.ToString();
  }
}

but in textboxes im getting null values

Answers (1)