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:
.