How to fill gridview using WCF ?


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

Advertisement

9 thoughts on “How to fill gridview using WCF ?

  1. Sanjay August 17, 2011 / 2:48 pm

    This article is really great..:) I implemented it..:) Step by step along with screenshot helped me..

    • Chandra Dev August 22, 2011 / 10:31 am

      I m glad to know that it helped you.

  2. Dorababu March 10, 2012 / 10:46 am

    Can i have the complete code for this in a zip format, can you mail me to dora.meka@gmail.com

  3. Rajini May 30, 2012 / 9:58 pm

    great ! it works!

  4. Dorababu May 31, 2012 / 2:43 pm

    Hi chandra can you post some more examples on WCF with 3 tier architecture?

    • Chandra Dev June 5, 2012 / 5:53 pm

      Sure.I will post in my free time. I have plan to post artical on 3 tier archicture using WCF and EF.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.