Example #1
0
        private void SerializeEmpProfile(EmployeeProfile empProfile)
        {
            string           existingJsonString;
            EmployeeProfiles empProfiles = new EmployeeProfiles();

            empProfiles.listOfEmployeeProfiles = new List <EmployeeProfile>();
            if (!File.Exists(serializedFileName))
            {
                FileStream fs = File.Create(serializedFileName);
                fs.Close();
            }
            else
            {
                existingJsonString = File.ReadAllText(serializedFileName);
                if (!existingJsonString.Equals(""))
                {
                    empProfiles = JsonConvert.DeserializeObject <EmployeeProfiles>(existingJsonString);
                }
            }

            if (empProfiles.listOfEmployeeProfiles == null)
            {
                empProfiles.listOfEmployeeProfiles = new List <EmployeeProfile>();
            }

            empProfiles.listOfEmployeeProfiles.Add(empProfile);

            string jsonString = JsonConvert.SerializeObject(empProfiles, Formatting.Indented);

            File.WriteAllText(serializedFileName, jsonString);
        }
Example #2
0
        private void button1_Click(object sender, EventArgs e)
        {
            string   alias = tbAlias.Text.ToString().Trim().ToLower();
            string   name  = tbName.Text.ToString().Trim().ToUpper();
            DateTime dob   = dtpDOB.Value.Date;
            DateTime doj   = dtpDOJ.Value.Date;

            empProfile                = new EmployeeProfile();
            empProfile.Alias          = alias;
            empProfile.EmpName        = name;
            empProfile.DateOfBirthday = dob;
            empProfile.DateOfJoining  = doj;

            if (empProfile.Alias != "" && empProfile.EmpName != "")
            {
                SerializeEmpProfile(empProfile);
                ClearAllFields();
            }
            else
            {
                MessageBox.Show("Employee alias and name cannot be null!", "Error");
            }
        }