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

Advertisements
Posted in WCF. 9 Comments »

9 Responses to “How to fill gridview using WCF ?”

  1. Sanjay Says:

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

    • Chandra Dev Says:

      I m glad to know that it helped you.

  2. Dorababu Says:

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

    • Chandra Dev Says:

      Sure, I will make it public in my share folder.

      • Chandra Dev Says:

        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

  3. Rajini Says:

    great ! it works!

    • Chandra Dev Says:

      Nice to hear.

  4. Dorababu Says:

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

    • Chandra Dev Says:

      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 )

Google+ photo

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

Connecting to %s

%d bloggers like this: