Ishfaq Ahmad
Ishfaq Ahmad

Reputation: 88

How i will manage Data in Grid View coming from Service through WCF

I want to create a service which fetch data from database and showing them on a client page in a GridView, but i have problem that how i will manage the data coming from service through data table in Grid View Plz help me out.

The following is my code:

Service Page with name "Service.svc"

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Activation;
using System.ServiceModel.Web;
using System.Text;
sing System.Data;
using System.Data.SqlClient;
using System.Web;
using System.Web.Configuration;
[ServiceContract(Namespace = "myService")]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class Service
{
   static private string sqlConString
   {
       get { return WebConfigurationManager.ConnectionStrings["SQLConnectionString"].ConnectionString; }
    }
    SqlConnection conn = new SqlConnection(sqlConString);

    [OperationContract]
    public DataTable FatchJobProporties()
    {
        conn.Open();
        try
        {
            string selQuery = "SELECT [job_title],[job_company],[job_vacancies],[job_description] FROM [dbo].[tb_job]";

         SqlCommand cmd = new SqlCommand(selQuery, conn);
         SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataSet ds = new DataSet();
            da.Fill(ds, "tb_Job");
            DataTable dt = ds.Tables["tb_Job"];
            return dt;
        }
        catch (Exception ex)
        {
            throw ex;
        }

       finally
        {
            conn.Close();
            conn.Dispose();
        }
}

[DataContract]
public class JobProporties
{
    string jobTitle = "my";
    string jobCompany = "my";
    string jobDec = "my";
    int jobVacant = 0;

    [DataMember]
    public string JobTitle
   {
        get { return jobTitle; }
        set { jobTitle = value; }
    }

    [DataMember]
    public string JobCompany
    {
        get { return jobCompany; }
        set { jobCompany = value; }
    }

   [DataMember]
    public int JobVacant
    {
        get { return jobVacant; }
        set { jobVacant = value; }
    }

    [DataMember]
    public string JobDec
    {
        get { return jobDec; }
        set { jobDec = value; }
    }
}

This Service Receiving Page with name "default.aspx"

![<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head runat="server">

    <title></title>

<script>

    function myFunc() {

        var proxy = new myService.Service();

        proxy.FatchJobProporties(onSuccess, onFail, null);


    }


    function onSuccess(result) {

        document.getElementById("tbDiv").innerHTML = result;

    }

    // This function is called if the service call fails

    function onFail() {

        alert("Fail to Fetch");
    }

</script>

</head>

<body>

    <form id="form1" runat="server">

    <div>

        <asp:ScriptManager ID="ScriptManager1" runat="server">

            <Services>

                <asp:ServiceReference Path="Service.svc" />

            </Services>

        </asp:ScriptManager>

        <input id="Button1" type="button" value="button" onclick="return myFunc();" />

        <div id="tbDiv">

        </div>


    </div>

    </form>

</body>

</html>][1]

Output: whole data come in a single line; for output see the image

Upvotes: 1

Views: 1432

Answers (2)

Ck.Nitin
Ck.Nitin

Reputation: 181

When will u add the service reference of WCF. You can do like

IList<Customer> customers = WCFServiceClientObject.GetAllCustomers()
myDataGrid.DataSource = customers;
MyDataGrid.DataBind();

Thanks

Ck Nitin (TinTin)

Upvotes: 1

Ck.Nitin
Ck.Nitin

Reputation: 181

Use the List class in return type to return multiple records.

try this code.

[DataContract]
public class Customer
{
    [DataMember]
    public int CustomerID{ get; set; }    }

    [DataMember]
    public string CustomerName{ get; set; }    
}


public interface ICustomerService
{

   [OperationContract]
   List<Customer> GetAllCustomer();
}


public class CustomerService:ICustomerService
{

   List<Customer> GetAllCustomer()
   {
       List<Customer> customers = new List<Customer>();

       using(SqlConnection con = new SqlConnection("Database Connection String"))
       {
        con.Open();     
        using(SqlCommand cmd = new SqlCommand("Select * from Customer",con))
        {
            SqlDataReader dr = cmd.ExecuteReader();

            while(dr.Read())
            {
                Customer customer = new Customer();
                customer.CustomerID =Parse.Int(dr[0].ToString());
                customer.CustomerName =dr[1].ToString(); 
                customers.Add(customer);
            }
        }
       }
    return customers;
  }
}

Enjoy!!!!

Thanks Ck Nitin (TinTin)

Upvotes: 1

Related Questions