Hi
This is the following steps to fill gridview using WCF
Step 1: Create one table i.e tblEmp which contain Id,EmpName,EmpSal field.
Step2. Add the “WCF Service File”.
Step3.Write the following code in Iservice.cs file
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using System.Data.SqlClient;
using System.Data;// NOTE: If you change the interface name “IService” here, you must also update the reference to “IService” in Web.config.
[ServiceContract]
public interface IService
{[OperationContract]
Emp[] getAllEmpName();}
[DataContract]
public class Emp
{
[DataMember]
public int EmpId { get; set; }
[DataMember]
public string EmpName { get; set; }
[DataMember]
public string EmpSal { get; set; }
}
Step4: Write the following Code in “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;// NOTE: If you change the class name “Service” here, you must also update the reference to “Service” in Web.config.
public class Service : IService
{
public void DoWork()
{
}#region IService Members
Emp[] IService.getAllEmpName()
{
SqlConnection con = new SqlConnection(“Data Source=.\\SQLEXPRESS;AttachDbFilename=|DataDirectory|\\Database.mdf;Integrated Security=True;User Instance=True”);
SqlCommand cmd = new SqlCommand(“Select *from tblEmp”, con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
int RCount=dt.Rows.Count;
Emp[] arrEmp=new Emp[RCount];
int i = 0;
foreach (DataRow dr in dt.Rows)
{
arrEmp[i] = new Emp();
arrEmp[i].EmpId = Convert.ToInt32(dr[“Id”]);
arrEmp[i].EmpName = dr[“EmpName”].ToString();
arrEmp[i].EmpSal = dr[“EmpSal”].ToString();
i++;}
return arrEmp;}
#endregion
}
Step 5: Click on “Add service reference” then Click on “Discover” then
write the NameSpace Name Like this img
Step 6:Click on advance and set “asynchronous Communication” like this
Step6: Take one gridview control in default page. write the following code in code behind file
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
ShowEmpData.ServiceClient ws = new ShowEmpData.ServiceClient();
GridView1.DataSource = ws.getAllEmpName();
GridView1.DataBind();}
}
Now compile the code. You will get output like this
This article is really great..:) I implemented it..:) Step by step along with screenshot helped me..
I m glad to know that it helped you.
Can i have the complete code for this in a zip format, can you mail me to dora.meka@gmail.com
Sure, I will make it public in my share folder.
Hi
Recently i have uploaded the simplified code in my share folder. You can download from here
https://skydrive.live.com/#cid=4B1F6C3E92F6522C&id=4B1F6C3E92F6522C!125
great ! it works!
Nice to hear.
Hi chandra can you post some more examples on WCF with 3 tier architecture?
Sure.I will post in my free time. I have plan to post artical on 3 tier archicture using WCF and EF.