Friday 17 January 2014

wcf service in windows application

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.IO;
using System.Runtime.Serialization.Json;
using ClassLibrary1;

namespace WindowsFormsApplication6
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            fill();
        }
        void fill()
        {
            List<EMP> lst = new List<EMP>();
            HttpWebRequest hwr = (HttpWebRequest)WebRequest.Create("http://localhost:6928/Service1.svc/Reset/GetData");
            HttpWebResponse hws = (HttpWebResponse)hwr.GetResponse();
            Stream st = hws.GetResponseStream();
            using (StreamReader sr = new StreamReader(st))
            {
                string s = sr.ReadToEnd();
                DataContractJsonSerializer djs = new DataContractJsonSerializer(typeof(List<EMP>));
                MemoryStream ms = new MemoryStream(Encoding.Default.GetBytes(s));
                lst = (List<EMP>)djs.ReadObject(ms);
            }
            dataGridView1.DataSource = lst;
        }
        void clr()
        {
            textBox1.Clear();
            textBox2.Clear();
            textBox1.Focus();
        }
        private void button1_Click(object sender, EventArgs e)
        {
            HttpWebRequest hwr = (HttpWebRequest)WebRequest.Create("http://localhost:6928/Service1.svc/Reset/AddData/" + textBox1.Text + "/" + textBox2.Text);
            hwr.Method = "POST";
            HttpWebResponse hws = (HttpWebResponse)hwr.GetResponse();
            clr();
            MessageBox.Show("Data saved.");
            fill();
        }

     

        private void button2_Click(object sender, EventArgs e)
        {
            HttpWebRequest hwr = (HttpWebRequest)WebRequest.Create("http://localhost:6928/Service1.svc/Reset/EditData/" + textBox1.Text + "/" + textBox2.Text);
            hwr.Method = "PUT";
            HttpWebResponse hws = (HttpWebResponse)hwr.GetResponse();
            clr();
            MessageBox.Show("Data Updated.");
            fill();
        }

        private void button4_Click(object sender, EventArgs e)
        {
            DialogResult=MessageBox.Show("Do you want to delete it ?", "Message", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if(DialogResult.ToString()=="Yes")
            {
                HttpWebRequest hwr = (HttpWebRequest)WebRequest.Create("http://localhost:6928/Service1.svc/Reset/DeleteData/" + textBox1.Text);
                hwr.Method = "DELETE";
                HttpWebResponse hws = (HttpWebResponse)hwr.GetResponse();
                clr();
                MessageBox.Show("Data Deleted.");
                fill();
            }
         
        }

        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex == 0)
            {
                textBox1.Text = dataGridView1.CurrentRow.Cells[2].Value.ToString();
                textBox2.Text = dataGridView1.CurrentRow.Cells[3].Value.ToString();
            }

            else if (e.ColumnIndex == 1)
            {
                DialogResult = MessageBox.Show("Do you want to delete it ?", "Message", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                if (DialogResult.ToString() == "Yes")
                {
                    HttpWebRequest hwr = (HttpWebRequest)WebRequest.Create("http://localhost:6928/Service1.svc/Reset/DeleteData/" + dataGridView1.CurrentRow.Cells[2].Value.ToString());
                    hwr.Method = "DELETE";
                    HttpWebResponse hws = (HttpWebResponse)hwr.GetResponse();
                    clr();
                    MessageBox.Show("Data Deleted.");
                    fill();
                }
            }
        }
    }
}