Click here to Skip to main content
15,907,906 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,
I am currently struggling with ideas on how to save the value from a method into the database.My method is this:
foreach (var item in Courses)
                 {

                     if (item.IsChecked)
                     {
                         SelectedCourses.Add(item);

                     }


Now here i'm getting the value from the checkbox into a collection called "SelectedCourses".This has one to many courses,so the lecturer can select 3 courses for e.g but the actual value stored is "courseName".I have a method that saves all the values,and it works except for the courseName because I have no idea how to save multiple "courseName" into database since I have no idea how the sintax may look like.This is the save method:
<pre>  public void SaveTeacher(object param)
        {

            int num = 0;
            using (DatabaseStudentsEntitiesLastStand db = new DatabaseStudentsEntitiesLastStand())
            {
                using (var transaction = db.Database.BeginTransaction())
                {
                    try
                    {
                        RegisterTeacher c = new RegisterTeacher();
                        c.SNTeacher = SNTeacher;
                        c.UserName = _UserName;
                        c.pwd = pwd;
                        c.fullName = fullName;
                        c.education = education;
                        db.RegisterTeachers.Add(c);
                        SelectedCourses = new ObservableCollection<Cours>();
                        foreach (var item in Courses)
                        {

                            if (item.IsChecked)
                            {
                               courseName=item.courseName;
                                SelectedCourses.Add(item);
                                db.Courses.AddRange(SelectedCourses);

                            }
                        }

                        num = db.SaveChanges();
                        transaction.Commit();



                    }
                     


                    catch (Exception e)
                    {
                        transaction.Rollback();
                    }
                }
               
            }
        }

And this is the delegatecommand along with the binding in the view:
saveCommand = new DelegateCommand(SaveTeacher);
 private DelegateCommand saveCommand;
        public DelegateCommand SaveCommand
        {
            get { return saveCommand; }
            set
            {
                if (saveCommand != value)
                {
                    saveCommand = value;
                    NotifyOnPropertyChange("SaveCommand");
                }
            }
        }

<pre> <Button Content="Submit" Command="{Binding Path=SaveCommand,Mode=TwoWay}" CommandParameter="{Binding ElementName=coursesList}"  HorizontalAlignment="Left" Margin="517,98.4,0,0" Grid.Row="3" VerticalAlignment="Top" Width="110" Height="40"/>


What I have tried:

.
Posted
Updated 19-Apr-18 2:29am
v2
Comments
F-ES Sitecore 18-Apr-18 9:07am    
You need a separate table to store the courses. If your parent table (RegisterTeacher) is like

ID, SNTeacher, ...
1, John, ....
2, Dave, ...

then you need a table called "RegisteredTeacherCourse" that will look like

TeacherID, CourseID
1, 1
1, 2
1, 4
2, 6
2, 2

That shows teacher 1 (John) has courses with the ids 1, 2 and 4 and teacher 2 (Dave) has courses 6 and 2.

That's roughly where you're trying to get to. If you google "ado.net implement one to many data relationship" you'll find examples of how to code it as that is what this type of data is called. You have "one" teacher that can have "many" courses.
Daniel Andrei Popescu 19-Apr-18 8:23am    
Hello,I have manage to do it and take the value :).I have instead a problem with saving the data into the database.I'm using entity framework and I have a DelegateCommand SaveCommand that saves all the data inserted by the lecturer.I have pasted the method that i'm using in order to save it and i'm using EF with database first.When I click the button,nothing happens,but if i put breakpoints to see if the data was passed from one value to another,it does,even the courseName.What should I do in order to make it work?I have tried to put a messagebox at the end that could confirm the registration,but I don't know if what i did was efective at all.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900