protected void btnUpload_Click(object sender, EventArgs e)
        {
            string theFile  = txtFile.PostedFile.FileName;
            string filepath = MapPath("images/") + theFile;

            //Response.Write(filepath);
            try{
                txtFile.PostedFile.SaveAs(filepath);
            }catch (Exception) {
                throw new Exception("File was not saved!");
            }
            staffRecord st     = new staffRecord();
            string      sId    = Request.QueryString["staffId"].ToString();
            string      name   = Request.QueryString["name"].ToString();
            var         result = db.staffRecords.Where(est => est.staffId == sId).First();

            result.passport = "~/PrivateWeb/images/" + theFile.ToString();
            // st.passport = filepath;
            //db.staffRecords.Add(result);
            db.SaveChanges();
            Response.Redirect("../PrivateWeb/enterEmploymentRecord.aspx?staffId=" + sId + "&name=" + name + "next");
        }
        protected void create_Click(object sender, EventArgs e)
        {
            if (sid.Text == string.Empty)
            {
                sidError.Text = "*";
            }
            else if (fname.Text == string.Empty)
            {
                fnameError.Text = "*";
            }
            else if (mname.Text == string.Empty)
            {
                mError.Text = "*";
            }
            else if (lname.Text == string.Empty)
            {
                lError.Text = "*";
            }
            else if (dob.Text == string.Empty)
            {
                dobError.Text = "*";
            }
            else if (gender.Text.Equals("-select-"))
            {
                genderError.Text = "*";
            }
            else if (ms.Text.Equals("-select-"))
            {
                msError.Text = "*";
            }
            else if (mobile.Text == string.Empty)
            {
                mobileError.Text = "*";
            }
            else if (addr.Text == string.Empty)
            {
                addrError.Text = "*";
            }
            else if (email.Text == string.Empty)
            {
                eError.Text = "*";
            }
            else if (foid.Text == string.Empty)
            {
                msg.Text = "*";
            }
            else if (basicQ.Text == string.Empty)
            {
                bqError.Text = "*";
            }
            else if (inst.Text == string.Empty)
            {
                iError.Text = "*";
            }
            else if (degree.Text == string.Empty)
            {
                dgError.Text = "*";
            }
            else if (ddLga.Text.Equals("-select-"))
            {
                lgaError.Text = "*";
            }
            else if (ddState.Text.Equals("-select-"))
            {
                lgaError.Text = "*";
            }
            else if (rel.Text == string.Empty)
            {
                relError.Text = "*";
            }
            else if (phone.Text == string.Empty)
            {
                gsmError.Text = "*";
            }
            else if (kaddr.Text == string.Empty)
            {
                kaddrError.Text = "*";
            }
            else if (sex.Text.Equals("-select-"))
            {
                sexError.Text = "*";
            }
            else if (klname.Text == string.Empty)
            {
                klnError.Text = "*";
            }
            else if (userName.Text == string.Empty)
            {
                userError.Text = "*";
            }
            // else if (dop.Text == string.Empty) { msg.Text = "*"; }
            else if (txtFile.FileName == string.Empty)
            {
                pp.Text = "*";
            }
            else if (kfname.Text == string.Empty)
            {
                kfnError.Text = "*";
            }
            else
            {
                string   dateOB      = dob.Text;
                string   dateO       = dop.Text;
                DateTime dateOfBirth = DateTime.Parse(dateOB);

                DateTime dateOptained = DateTime.Parse(dateO);
                int      i            = dateOptained.Year - dateOfBirth.Year;
                if (i < 14)
                {
                    dateError.Text = "invalid date";
                }

                staffRecord sr = new staffRecord();
                try
                {
                    sr.staffId = sid.Text;
                    sr.fName   = fname.Text;
                    sr.lName   = lname.Text;
                    sr.gender  = gender.SelectedValue;
                    string   dateString = dob.Text;
                    DateTime result;
                    result         = DateTime.Parse(dateString);
                    sr.dateOfBirth = result;
                    string staffAge = ((DateTime.Now.Year) - (result.Year)).ToString();
                    sr.email                = email.Text;
                    sr.mobile               = mobile.Text;
                    sr.address              = addr.Text;
                    sr.state                = ddState.SelectedValue.ToString();
                    sr.Lga                  = ddLga.SelectedValue.ToString();
                    sr.middleName           = mname.Text;
                    sr.marritalStatus       = ms.Text;
                    sr.formOfIdentification = foid.Text;
                    string theFile  = txtFile.PostedFile.FileName;
                    string filepath = MapPath("images/") + theFile;
                    try
                    {
                        txtFile.PostedFile.SaveAs(filepath);
                    }
                    catch (Exception)
                    {
                        msg.Text = "Record was not saved!";
                        // msg.Text += fx.Message;
                    }

                    sr.passport = "~/PrivateWeb/images/" + theFile.ToString();
                    sr.age      = Int32.Parse(staffAge);
                    sr.idNumber = pid.Text;
                    sr.status   = "active";
                    db.staffRecords.Add(sr);
                    db.SaveChanges();

                    //populate user table with staff login parameters
                    user use = new user();

                    use.email    = email.Text;
                    use.staffId  = sid.Text;
                    use.role     = "staff";
                    use.username = userName.Text;
                    string temPsw = randomPsw();
                    use.password = CreateHash(temPsw);
                    use.status   = "active";
                    db.users.Add(use);
                    db.SaveChanges();


                    qualification qu = new qualification();

                    string bq = basicQ.Text;
                    string dg = degree.Text;
                    Session["basicqualification"] = bq;
                    Session["degree"]             = dg;
                    qu.staffId = sid.Text;
                    qu.name    = basicQ.Text;
                    if (dop.Text != string.Empty)
                    {
                        string   result2 = dop.Text;
                        DateTime dateo   = DateTime.Parse(result2);
                        qu.dateObtained = dateo;
                    }
                    else
                    {
                        dateError.Text = "*";
                    }

                    qu.typeOfQualification = degree.Text;
                    qu.institution         = inst.Text;
                    db.qualifications.Add(qu);
                    db.SaveChanges();
                    //  Response.Redirect("enterEmploymentRecord.aspx?staffId=" + sid.Text);



                    staffNextOfKin st = new staffNextOfKin();

                    st.staffId       = sid.Text;
                    st.fName         = kfname.Text;
                    st.lname         = klname.Text;
                    st.gender        = sex.Text;
                    st.address       = kaddr.Text;
                    st.contactMobile = phone.Text;
                    st.relationship  = rel.Text;
                    db.staffNextOfKins.Add(st);
                    db.SaveChanges();


                    pendingLeave pl = new pendingLeave();
                    pl.staffId           = sid.Text;
                    pl.staffPendingLeave = 0;
                    db.pendingLeaves.Add(pl);
                    db.SaveChanges();
                    try
                    {
                        System.Net.Mail.MailMessage mail = new System.Net.Mail.MailMessage();
                        mail.To.Add(email.Text);
                        mail.From            = new MailAddress("*****@*****.**", "NIAPS", System.Text.Encoding.UTF8);
                        mail.Subject         = "Your NIAPS Logon Detail";
                        mail.SubjectEncoding = System.Text.Encoding.UTF8;
                        mail.Body            = "Your account details on NIA Personnel System (NIAPS) are: <b />user name =" + userName.Text + " <b />password ="******"*****@*****.**", "niaps123");
                        client.Port        = 587;
                        client.Host        = "smtp.gmail.com";
                        client.EnableSsl   = true;
                        client.Send(mail);
                        Response.Redirect("enterEmploymentRecord.aspx?staffId=" + sid.Text + "&lname=" + lname.Text + "&fname=" + fname.Text + "&mname=" + mname.Text + "&bq=" + bq + "&degree=" + dg);
                    }
                    catch (Exception) {
                        Response.Redirect("enterEmploymentRecord.aspx?staffId=" + sid.Text + "&lname=" + lname.Text + "&fname=" + fname.Text + "&mname=" + mname.Text + "&bq=" + bq + "&degree=" + dg);
                    }
                }
                catch (FormatException)
                {
                    msg.Text = "error, Could not create staff";
                }
                catch (Exception)
                {
                    msg.Text = "Could not create staff.. Staff already exist";
                }
            }
        }