private void lvUsers_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            ShowStudentData studentSelected = new ShowStudentData();

            studentSelected = lvUsers.SelectedItem as ShowStudentData;

            idstudent.Text             = studentSelected.StuId.ToString();
            studentFirstName.Text      = studentSelected.FirstName;
            lastnamestudent.Text       = studentSelected.LastName;
            datestudent.DisplayDate    = studentSelected.BirthDate;
            emailstudent.Text          = studentSelected.StuEmail;
            intakestudent.SelectedItem = studentSelected.Intake;
            tracksstudent.SelectedItem = studentSelected.Track;
            branchstudent.SelectedItem = studentSelected.Branch;
        }
        private void editstudent_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                ShowStudentData studentSelected = new ShowStudentData();
                studentSelected = lvUsers.SelectedItem as ShowStudentData;

                studentSelected.StuId     = int.Parse(idstudent.Text.ToString());
                studentSelected.FirstName = studentFirstName.Text.ToString();
                studentSelected.LastName  = lastnamestudent.Text.ToString();
                studentSelected.BirthDate = datestudent.DisplayDate;
                studentSelected.StuEmail  = emailstudent.Text.ToString();
                studentSelected.Branch    = branchstudent.SelectedItem.ToString();

                studentSelected.Track  = tracksstudent.SelectedItem.ToString();
                studentSelected.Intake = int.Parse(intakestudent.SelectedItem.ToString());
                rel_branch_track_intake newaddedstudent = new rel_branch_track_intake();

                var getIntack =
                    (from Ik in context3.intakes
                     select Ik).ToList();
                var getTrack =
                    (from Ik in context3.tracks
                     select Ik).ToList();
                var getBracnh =
                    (from Ik in context3.branches
                     select Ik).ToList();
                // but intake id in table intak student

                foreach (var item2 in getIntack)
                {
                    if (studentSelected.Intake == item2.IntakeNumber)
                    {
                        studentSelected.IntakeID = item2.IntakeNo;
                    }
                }


                // put track name in class track


                foreach (var item2 in getTrack)
                {
                    if (studentSelected.Track == item2.TrackName)
                    {
                        studentSelected.TrackID = item2.TrackId;
                    }
                }

                // put branch name in class branch


                foreach (var item2 in getBracnh)
                {
                    if (studentSelected.Branch == item2.Name)
                    {
                        studentSelected.BranchID = item2.BranchId;
                    }
                }



                int findITB =
                    (
                        from TIB in context3.rel_branch_track_intake
                        where TIB.branchID == studentSelected.BranchID && TIB.trackID == studentSelected.TrackID &&
                        TIB.intack == studentSelected.IntakeID
                        select TIB.id
                    ).Count();


                int lastindexOfTIB;


                lastindexOfTIB =
                    (from lastindexes in context3.rel_branch_track_intake
                     select lastindexes).Count();



                student updateStudentInformation =
                    (from std in context3.students

                     where std.StuId == studentSelected.StuId
                     select std
                    ).FirstOrDefault();


                user newstudentuser = context3.users.Where(em => em.Email == studentSelected.StuEmail.ToString()).FirstOrDefault();
                newstudentuser.Email    = updateStudentInformation.StuEmail;
                newstudentuser.Password = passswordstudent.Text.ToString();

                context3.SaveChanges();

                int findITB3 =
                    (
                        from TIB in context3.rel_branch_track_intake
                        where TIB.branchID == studentSelected.BranchID && TIB.trackID == studentSelected.TrackID &&
                        TIB.intack == studentSelected.IntakeID
                        select TIB.id
                    ).FirstOrDefault();

                if (findITB > 0)
                {
                    updateStudentInformation.StuEmail      = studentSelected.StuEmail;
                    updateStudentInformation.StuId         = studentSelected.StuId;
                    updateStudentInformation.FirstName     = studentSelected.FirstName;
                    updateStudentInformation.LastName      = studentSelected.LastName;
                    updateStudentInformation.BirthDate     = DateTime.Now;
                    updateStudentInformation.JoinDate      = DateTime.Now;
                    updateStudentInformation.MatrailStatus = "single";

                    updateStudentInformation.ITBid = findITB3;
                    context3.SaveChanges();
                }
                else
                {
                    updateStudentInformation.StuEmail      = studentSelected.StuEmail;
                    updateStudentInformation.StuId         = studentSelected.StuId;
                    updateStudentInformation.FirstName     = studentSelected.FirstName;
                    updateStudentInformation.LastName      = studentSelected.LastName;
                    updateStudentInformation.BirthDate     = DateTime.Now;
                    updateStudentInformation.JoinDate      = DateTime.Now;
                    updateStudentInformation.MatrailStatus = "single";
                    newaddedstudent.trackID  = studentSelected.TrackID;
                    newaddedstudent.branchID = studentSelected.BranchID;
                    newaddedstudent.intack   = studentSelected.IntakeID;
                    context3.rel_branch_track_intake.Add(newaddedstudent);
                    updateStudentInformation.ITBid = newaddedstudent.id;
                    context3.SaveChanges();
                }
            }
            catch (Exception d)
            {
                MessageBox.Show("invalid inputs");
            }
        }
        private void lvUsers_Loaded(object sender, RoutedEventArgs e)
        {
            List <ShowStudentData> ShowStudentData = new List <ShowStudentData>();
            var query
                = (from std in context3.students
                   select std).ToList();

            foreach (var item in query)
            {
                ShowStudentData DataStudent = new ShowStudentData();
                DataStudent.BirthDate     = item.BirthDate;
                DataStudent.StuEmail      = item.StuEmail;
                DataStudent.JoinDate      = item.JoinDate;
                DataStudent.FirstName     = item.FirstName;
                DataStudent.LastName      = item.LastName;
                DataStudent.ITBid         = item.ITBid;
                DataStudent.MatrailStatus = item.MatrailStatus;
                DataStudent.StuId         = item.StuId;
                ShowStudentData.Add(DataStudent);
            }
            var query2 =
                (from ITBtable in context3.rel_branch_track_intake
                 select ITBtable).ToList();


            foreach (var item in ShowStudentData)
            {
                foreach (var item2 in query2)
                {
                    if (item.ITBid == item2.id)
                    {
                        item.BranchID = item2.branchID;
                        item.TrackID  = item2.trackID;
                        item.IntakeID = item2.intack;
                    }
                }
            }

            var getIntack =
                (from Ik in context3.intakes
                 select Ik).ToList();
            var getTrack =
                (from Ik in context3.tracks
                 select Ik).ToList();
            var getBracnh =
                (from Ik in context3.branches
                 select Ik).ToList();

            // but intake id in table intak student
            foreach (var item in ShowStudentData)
            {
                foreach (var item2 in getIntack)
                {
                    if (item.IntakeID == item2.IntakeNo)
                    {
                        item.Intake = item2.IntakeNumber;
                    }
                }
            }

            // put track name in class track

            foreach (var item in ShowStudentData)
            {
                foreach (var item2 in getTrack)
                {
                    if (item.TrackID == item2.TrackId)
                    {
                        item.Track = item2.TrackName;
                    }
                }
            }
            // put branch name in class branch

            foreach (var item in ShowStudentData)
            {
                foreach (var item2 in getBracnh)
                {
                    if (item.BranchID == item2.BranchId)
                    {
                        item.Branch = item2.Name;
                    }
                }
            }


            foreach (var item in ShowStudentData)
            {
                lvUsers.Items.Add(item);
            }
        }
        private void addstudent_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                track  addtrackstudent      = new track();
                intake addintakstudent      = new intake();
                branch addbranchstudent     = new branch();
                user   studentuser          = new user();
                rel_branch_track_intake ITb = new rel_branch_track_intake();

                student newstudent = new student();
                newstudent.StuId             = int.Parse(idstudent.Text.ToString());
                newstudent.FirstName         = studentFirstName.Text.ToString();
                newstudent.LastName          = lastnamestudent.Text.ToString();
                newstudent.BirthDate         = datestudent.DisplayDate;
                newstudent.StuEmail          = emailstudent.Text.ToString();
                newstudent.MatrailStatus     = "single";
                newstudent.JoinDate          = DateTime.Now;
                addbranchstudent.Year        = DateTime.Now;
                addbranchstudent.Name        = branchstudent.SelectedItem.ToString();
                addtrackstudent.Year         = DateTime.Now;
                addtrackstudent.TrackName    = tracksstudent.SelectedItem.ToString();
                addintakstudent.IntakeNumber = int.Parse(intakestudent.SelectedItem.ToString());
                addintakstudent.Year         = DateTime.Now;
                ShowStudentData newstudentwasadded = new ShowStudentData();
                newstudentwasadded.StuId     = int.Parse(idstudent.Text.ToString());
                newstudentwasadded.StuEmail  = emailstudent.Text.ToString();
                newstudentwasadded.FirstName = studentFirstName.Text.ToString();
                newstudentwasadded.LastName  = lastnamestudent.Text.ToString();
                newstudentwasadded.Track     = tracksstudent.SelectedItem.ToString();
                newstudentwasadded.Intake    = int.Parse(intakestudent.SelectedItem.ToString());
                newstudentwasadded.Branch    = branchstudent.SelectedItem.ToString();
                newstudentwasadded.BirthDate = DateTime.Now;

                studentuser.Email    = newstudent.StuEmail;
                studentuser.Password = passswordstudent.Text.ToString();
                studentuser.Type     = "student";


                int branchFindId
                    = (from branches in context3.branches
                       where branches.Name == addbranchstudent.Name
                       select branches.BranchId).FirstOrDefault();

                int trackFindId
                    =
                        (from trackID in context3.tracks
                         where trackID.TrackName == addtrackstudent.TrackName
                         select trackID.TrackId).FirstOrDefault();

                int intakeFindId
                    =
                        (from intakeId in context3.intakes
                         where intakeId.IntakeNumber == addintakstudent.IntakeNumber
                         select intakeId.IntakeNo
                        ).FirstOrDefault();

                ITb.intack   = intakeFindId;
                ITb.trackID  = trackFindId;
                ITb.branchID = branchFindId;


                int findITB =
                    (
                        from TIB in context3.rel_branch_track_intake
                        where TIB.branchID == branchFindId && TIB.trackID == trackFindId && TIB.intack == intakeFindId
                        select TIB.id
                    ).Count();
                int lastindexOfTIB;
                lastindexOfTIB =
                    (from lastindexes in context3.rel_branch_track_intake
                     select lastindexes).Count();
                MessageBox.Show(lastindexOfTIB.ToString());
                if (findITB > 0)
                {
                    int findITB2 =
                        (
                            from TIB in context3.rel_branch_track_intake
                            where TIB.branchID == branchFindId && TIB.trackID == trackFindId && TIB.intack == intakeFindId
                            select TIB.id
                        ).FirstOrDefault();
                    newstudent.ITBid = findITB2;
                    context3.users.Add(studentuser);
                    context3.students.Add(newstudent);
                    context3.SaveChanges();
                    lvUsers.Items.Add(newstudentwasadded);
                }
                else
                {
                    newstudent.ITBid = lastindexOfTIB;
                    ITb.id           = lastindexOfTIB;
                    context3.rel_branch_track_intake.Add(ITb);
                    context3.SaveChanges();
                    context3.users.Add(studentuser);
                    context3.students.Add(newstudent);
                    context3.SaveChanges();
                    lvUsers.Items.Add(newstudentwasadded);
                }
            }
            catch (Exception d)
            {
                MessageBox.Show("invalid inputs");
            }
        }