Wcf services display data in gridview

///////////////////////////////////////////////////////////
//////Consuming.Aspx.cs
/////////////////////////////////////////////////////
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
//using ConsumingWcfService.ServiceReference1;
//using ConsumingWcfService.MyService;
using ConsumingWcfService.ServiceReference1;
using System.Data;
using System.Data.SqlClient;
using System.Text;
using System.IO;
using System.Web.Script.Serialization;
//using ConsumingWcfService.ServiceReference2;

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

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            //ServiceReference1.Service1Client obj = new ServiceReference1.Service1Client();
            //ImagePath p = new ImagePath();
            //p.Name = TextBox2.Text;
            //obj.DeleteImagePath(p);

            //ServiceReference1.Service1Client obj = new ServiceReference1.Service1Client();
            //ImagePath p = new ImagePath();
            //p.Imagepaath = TextBox2.Text;
            //p.Name = TextBox1.Text;
            //obj.AddImagePath(p);
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            //ServiceReference2.Service1Client obj = new ServiceReference2.Service1Client();
            //ImagaDetailsClass a = new ImagaDetailsClass();
            ServiceReference1.ImyinterfaceClient obj = new ServiceReference1.ImyinterfaceClient();
            string strdata = obj.ShowImageDetails();
            var serializer = new JavaScriptSerializer();
            //var data= serializer.DeserializeObject(strdata);
            DataTable dtdetails = new DataTable();
            List<Dictionary<string, object>> rows = serializer.Deserialize<List<Dictionary<string, object>>>(strdata);
            DataTable dtDetailsss = GetDataTableFromDictionaries(rows);
            if (dtDetailsss.Rows.Count > 0)
            {
                GridView1.DataSource = dtDetailsss;
                GridView1.DataBind();
            }        
        }
        private DataTable GetDataTableFromDictionaries<T>(List<Dictionary<string, T>> list)
        {
            DataTable dataTable = new DataTable();

            if (list == null || !list.Any()) return dataTable;

            foreach (var column in list.First().Select(c => new DataColumn(c.Key, typeof(T))))
            {
                dataTable.Columns.Add(column);
            }

            foreach (var row in list.Select(
                r =>
                {
                    var dataRow = dataTable.NewRow();
                    r.ToList().ForEach(c => dataRow.SetField(c.Key, c.Value));
                    return dataRow;
                }))
            {
                dataTable.Rows.Add(row);
            }

            return dataTable;
        }
     
     

    }
}
///////////////////////////////////////////////////////////
//////Consuming.Aspx
/////////////////////////////////////////////////////
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="InsertAndDelete.aspx.cs" Inherits="ConsumingWcfService.InsertAndDelete" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox><asp:TextBox ID="TextBox2" runat="server"></asp:TextBox><asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
    &nbsp;<asp:Button ID="Button2" runat="server" Text="Button" OnClick="Button2_Click" />
    </div>
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False">
            <Columns>
                <asp:TemplateField HeaderText="Id">
                    <EditItemTemplate>
                        <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("Id") %>'></asp:TextBox>
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="Label1" runat="server" Text='<%# Bind("Id") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Name">
                    <EditItemTemplate>
                        <asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("Name") %>'></asp:TextBox>
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="Label2" runat="server" Text='<%# Bind("Name") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="ImagePath">
                    <EditItemTemplate>
                        <asp:TextBox ID="TextBox3" runat="server" Text='<%# Bind("ImagePath") %>'></asp:TextBox>
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="Label3" runat="server" Text='<%# Bind("ImagePath") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>
    </form>
</body>
</html>

///////////////////////////////////////////////////////////
//////I services.cs
/////////////////////////////////////////////////////
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
using System.Data;
namespace Imagestore_WcfServices
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "IService1" in both code and config file together.
    [ServiceContract]
    public interface IService1
    {

        [OperationContract]
        string GetData(int value);

        [OperationContract]
        CompositeType GetDataUsingDataContract(CompositeType composite);
        [OperationContract]
        int AddImagePath(ImagePath p);
        [OperationContract]
        int DeleteImagePath(ImagePath p);
        // TODO: Add your service operations here
    }


    // Use a data contract as illustrated in the sample below to add composite types to service operations.
    [DataContract]
    public class CompositeType
    {
        bool boolValue = true;
        string stringValue = "Hello ";

        [DataMember]
        public bool BoolValue
        {
            get { return boolValue; }
            set { boolValue = value; }
        }

        [DataMember]
        public string StringValue
        {
            get { return stringValue; }
            set { stringValue = value; }
        }
    }
    [DataContract]
    public class ImagePath
    {
        [DataMember]
        public string Name { get; set; }
        [DataMember]
        public string Imagepaath { get; set; }
    }


    [ServiceContract]
    public interface Imyinterface : IService1
    {
        [OperationContract]
        string ShowImageDetails();  
    }
    //[DataContract]
    //public class ImagaDetailsClass
    //{
    //    [DataMember]
    //    public int MyProperty { get; set; }
    //}
}


///////////////////////////////////////////////////////////
//////I services.svc.cs
/////////////////////////////////////////////////////


using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
using System.Data;
using System.Data.SqlClient;


namespace Imagestore_WcfServices
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Service1" in code, svc and config file together.
    // NOTE: In order to launch WCF Test Client for testing this service, please select Service1.svc or Service1.svc.cs at the Solution Explorer and start debugging.
    public class Service1 : Imyinterface
    {
        public string GetData(int value)
        {
            return string.Format("You entered: {0}", value);
        }

        public CompositeType GetDataUsingDataContract(CompositeType composite)
        {
            if (composite == null)
            {
                throw new ArgumentNullException("composite");
            }
            if (composite.BoolValue)
            {
                composite.StringValue += "Suffix";
            }
            return composite;
        }
        public int AddImagePath(ImagePath p)
        {
            SqlConnection con = new SqlConnection("database=pluto;data source=192.168.200.77;user id=sa;password=indi123");
            con.Open();
            SqlCommand cmd = new SqlCommand("Insert into tbl_ImageStore (name,imagePath)values(@name,@Imagepath)", con);
            cmd.Parameters.AddWithValue("@name", p.Name);
            cmd.Parameters.AddWithValue("@Imagepath", p.Imagepaath);
            int i = cmd.ExecuteNonQuery();
            con.Close();
            return i;
        }
        public int DeleteImagePath(ImagePath p)
        {
            SqlConnection con = new SqlConnection("database=pluto;data source=192.168.200.77;user id=sa;password=indi123");
            con.Open();
            SqlCommand cmd = new SqlCommand("Delete from tbl_ImageStore where name=@name", con);
            cmd.Parameters.AddWithValue("@name", p.Name);
            int i = cmd.ExecuteNonQuery();
            con.Close();
            return i;
        }
        public DataTable ImageDetailsMethod()
        {
            DataTable dt = new DataTable();
            SqlConnection con = new SqlConnection("database=pluto;data source=192.168.200.77;user id=sa;password=indi123");
            SqlDataAdapter da = new SqlDataAdapter("Select * from tbl_imageStore", con);
            da.Fill(dt);
            return dt;
        }
        public string ShowImageDetails()
        {          
            DataTable dt = new DataTable();
            SqlConnection con = new SqlConnection("database=pluto;data source=192.168.200.77;user id=sa;password=indi123");
            SqlDataAdapter da = new SqlDataAdapter("Select * from tbl_imageStore", con);
            da.Fill(dt);
            System.Web.Script.Serialization.JavaScriptSerializer serializer = new System.Web.Script.Serialization.JavaScriptSerializer();
            List<Dictionary<string, object>> rows = new List<Dictionary<string, object>>();
            Dictionary<string, object> row;
            foreach (DataRow dr in dt.Rows)
            {
                row = new Dictionary<string, object>();
                foreach (DataColumn col in dt.Columns)
                {
                    row.Add(col.ColumnName, dr[col]);
                }
                rows.Add(row);
            }
            return serializer.Serialize(rows);          
        }
    }
}

Comments

Popular posts from this blog

How to write Pure java script Program?