@Veer.Activities is a Model where I have defined all the properties Like activityname,checkboxes. Here is the code:Activities.cs
public class Activities
{
public Activities(string name, CheckBox chb, bool sel)
{
ActivityName = name;
AcivityCheckBox = chb;
Selected = sel;
}
private string _activityName;
private CheckBox _activityCheckBox;
private bool _selected;
public string ActivityName
{
get { return _activityName; }
set { _activityName = value; }
}
public CheckBox AcivityCheckBox
{
get { return _activityCheckBox; }
set { _activityCheckBox = value; }
}
public bool Selected
{
get { return _selected; }
set { _selected = value; }
}
}
Here is my DaoDailyActivities.cs
public class DaoDailyActivities
{
string activityName = "";
string userActivityName = "";
bool IsSelected;
int activityID;
SqlConnection con = new SqlConnection("server=172.16.32.68;database=ParentalHealth;uid=sa;pwd=Emids123");
public ObservableCollection<Activities> GetActivities()
{
SqlCommand cmd = new SqlCommand("SP_GetActivities", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Connection = con;
con.Open();
ObservableCollection<Activities> activitiesList = new ObservableCollection<Activities>();
SqlDataReader readerActivities;
readerActivities = cmd.ExecuteReader();
while (readerActivities.Read())
{
activityID = Convert.ToInt32(readerActivities["ActivityID"]);
activityName = readerActivities["ActivityName"].ToString();
CheckBox chb = new CheckBox();
activitiesList.Add(new Activities(activityName, chb, IsSelected = (activityID % 2 == 0)));
}
return activitiesList;
}
public List<UserActivity> GetUserActivity()
{
string sqlQuery = "SELECT ActivityName FROM Activities A ";
sqlQuery+="INNER JOIN UserActivity UA ON A.ActivityID = UA.ActivityID WHERE UA.UserID = 1";
SqlCommand cmd = new SqlCommand(sqlQuery, con);
cmd.Connection = con;
con.Open();
List<UserActivity> userActivityList = new List<UserActivity>();
SqlDataReader readerUserActivities;
readerUserActivities = cmd.ExecuteReader();
while (readerUserActivities.Read())
{
userActivityName = readerUserActivities["ActivityName"].ToString();
userActivityList.Add(new UserActivity(userActivityName));
}
return userActivityList;
}
}
Here is my MainViewModel.cs
public class MainViewModel : ViewModelBase
{
public string Welcome
{
get
{
return "Welcome to MVVM Light";
}
}
public SeniorActivities.ViewModel.BusinessImplementation.SeniorActivities sn = new BusinessImplementation.SeniorActivities();
/// <summary>
/// Initializes a new instance of the MainViewModel class.
/// </summary>
public MainViewModel()
{
string abc="hello";
OpenPopUp = new RelayCommand(() =>
{
Activities pop = new Activities(ref abc);
WindowInteropHelper helper = new WindowInteropHelper(pop);
pop.ShowDialog();
});
ClosePopUp = new RelayCommand(() =>
{
Activities close = new Activities();
WindowInteropHelper closePopUp = new WindowInteropHelper(close);
close.Close();
});
}
public RelayCommand OpenPopUp
{
get;
private set;
}
public RelayCommand ClosePopUp
{
get;
private set;
}
}
Here is my Activities.xaml.cs
public partial class Activities : Window
{
/// <summary>
/// Initializes a new instance of the SeniorActivities class.
/// </summary>
public Activities()
{
DaoDailyActivities act = new DaoDailyActivities();
InitializeComponent();
listBox1.ItemsSource = act.GetActivities();
}
public Activities(ref string xyz)
{
DaoDailyActivities act = new DaoDailyActivities();
InitializeComponent();
listBox1.ItemsSource = act.GetActivities();
MessageBox.Show(xyz);
}
}
Im my DaoDailyActivities.cs
GetUserActivity() is returning the activities per specific user. I want this list with preselected checkboxes along with the list returning by GetActivities() in my second window so that user can check/uncheck the activities. All this I am storing/retrieving in sql server 2008
Kindly Suggest?
Thanks