private void btnSsave_Click(object sender, EventArgs e)
        {
            Employee emp = new Employee();

            emp.EmployeeAddress = txtAddress.Text;
            emp.EmployeeEmail   = txtEmail.Text;
            emp.EmployeeName    = txtName.Text;
            emp.EmployeePhoneNo = Convert.ToInt64(txtPhoneNo.Text);
            emp.IsActive        = Convert.ToBoolean(chkIsAcctive.Checked);
            string      jsonContent = JsonConvert.SerializeObject(emp);
            HttpContent content     = new StringContent(jsonContent);
            HttpClient  client      = new HttpClient();

            client.BaseAddress = new Uri("http://localhost:1301/");
            content.Headers.Clear();
            content.Headers.Add("content-type", "application/json");
            HttpResponseMessage responsemployee = client.PostAsync("Employee/SaveEmployee", content).Result;
            string result = responsemployee.Content.ReadAsStringAsync().Result;

            long empIdGet = 0;
            bool isParse  = long.TryParse(result, out empIdGet);

            if (isParse)
            {
                ModelSalary sal = new ModelSalary();
                sal.EmployeeId = empIdGet;
                sal.Salary     = Convert.ToSingle(txtSalary.Text);
                string salJsonConvert = JsonConvert.SerializeObject(sal);
                content = new StringContent(salJsonConvert);
                content.Headers.Clear();
                content.Headers.Add("content-type", "application/json");

                HttpResponseMessage responseSalary = client.PostAsync("Salary/SaveModelSalary", content).Result;
                string resSalary = responseSalary.Content.ReadAsStringAsync().Result;


                ModelPhoto photo = new ModelPhoto();
                photo.EmployeeId = empIdGet;
                FileStream fs = new FileStream(openFileDialog1.FileName, FileMode.Open, FileAccess.Read);

                byte[] buf = new byte[fs.Length];
                fs.Read(buf, 0, buf.Length);
                photo.Photo = buf;
                string phoJsonConvert = JsonConvert.SerializeObject(photo);
                content = new StringContent(phoJsonConvert);
                content.Headers.Clear();
                content.Headers.Add("content-type", "application/json");

                HttpResponseMessage responsePhoto = client.PostAsync("Photo/SaveModelPhoto", content).Result;
                string resPhoto = responsePhoto.Content.ReadAsStringAsync().Result;
                MessageBox.Show(resPhoto);
                Form1 frmForm1 = new Form1(txtName.Text, empIdGet);
                frmForm1.ShowDialog();
            }
            else
            {
                MessageBox.Show(result);
            }
        }
예제 #2
0
        private void Form1_Load(object sender, EventArgs e)
        {
            HttpClient client = new HttpClient();

            client.BaseAddress = new Uri("http://localhost:1301/");
            client.DefaultRequestHeaders.Accept.Clear();
            client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
            HttpResponseMessage responsemployee = client.GetAsync("Employee/GetEmployee?employeeName=" + _empName).Result;
            string result = responsemployee.Content.ReadAsStringAsync().Result;

            Employee emp = JsonConvert.DeserializeObject <Employee>(result);

            label1.Text = emp.EmployeeName;
            label2.Text = emp.EmployeeAddress;
            label3.Text = emp.EmployeeEmail;
            label4.Text = emp.EmployeePhoneNo.ToString();

            HttpResponseMessage responsPhoto = client.GetAsync("Photo/GetModelPhoto?employeeId=" + _empId).Result;
            string     resultPhoto           = responsPhoto.Content.ReadAsStringAsync().Result;
            ModelPhoto phot = JsonConvert.DeserializeObject <ModelPhoto>(resultPhoto);

            pictureBox1.Image = Image.FromStream(new MemoryStream(phot.Photo));
        }