Introduction
In Part 1, we had completed the following tasks:
- Creating and designing of Main page and Child window page using XAML
- Creating WCF service
- Consuming WCF service in Silverlight application
- Fetching data from database using WCF service
- Show data in the
datagrid
of popup child window of main page
Tasks for Part 2
In this part, the main working areas are as follows:
- Check Box Problems
- Adding the selected contacts of child window to Read only Contact Text Box
- Saving the Meeting Schedule into the database
- Sending email to all the selected contacts for meeting information
Check Box Problem
The main problem with check boxes is that of datagrid
header check box. Providing they are all checked and all unchecked from header check box to the end user was a very challenging job initially, but later it becomes easy stuff. Below is the coding for this problem. When header check box is checked, all checkboxes should check and corresponding data will be added to the list generics and when unchecked none of the corresponding data is added to the list generics.
Add the following namespace for including observableCollection
.
using System.Collections.ObjectModel;
Next two observableCollection
type variables are defined in globally, having class type as their type.
public class newcontacts
{
public string Fname { get; set; }
public string Lname { get; set; }
public string Address { get; set; }
public string Email { get; set; }
}
ObservableCollection<newcontacts> allcontacts = new ObservableCollection<newcontacts>();
ObservableCollection<newcontacts> contactlist = new ObservableCollection<newcontacts>();
The below method is used to check all if header checkbox is checked.
private void CheckBX_Click(object sender, RoutedEventArgs e)
{
IsLoopNeeded = false;
chk_header = sender as CheckBox;
bool check = chk_header.IsChecked.Value;
if (check)
{
foreach (newcontacts selectContacts in allcontacts)
{
chk_header = (CheckBox)dataGrid1.Columns[4].GetCellContent(selectContacts);
if (chk_header.IsChecked != true)
{
chk_header.IsChecked = true;
}
}
IsContactPresent = true;
}
else
{
foreach (newcontacts selectContacts in allcontacts)
{
chk_header = (CheckBox)dataGrid1.Columns[4].GetCellContent(selectContacts);
if (chk_header.IsChecked != false)
{
chk_header.IsChecked = false;
}
}
IsContactPresent = false;
}
}
If individual check boxes are checked, then the following method is used in which recorded is added to list when check box is checked or removed when checkbox is unchecked.
void cell_chk_Click(object sender, RoutedEventArgs e)
{
CheckBox chk = sender as CheckBox;
bool check = chk.IsChecked.Value;
IsLoopNeeded = true;
newcontacts contacts = chk.DataContext as newcontacts;
if (check)
{
if (!contactlist.Contains(contacts))
{
contactlist.Add(contacts);
}
}
else
{
contactlist.Remove(contacts);
}
}
Adding Selected Contacts from Child Window to Main Page Text Box
This is the most tricky part of this application and consumes most of the time. I had no idea how to add records to other pages which are checked. I had tried so many ways and logic, but those are not as good as this one. I think this is very good and fast as compared to others by using Delegate
and EventHandler
. Records from child window will be added only when clicked on ok button, not cancel or closing windows.
But to this task, I have used the paramertized consturctor. Using main class constructor for doing this is much faster than the above mentioned two methods. So below is the coding how this can be achieved.
Main Page Code Lines
Create private string
type static
variable for storing contacts which we get from child window.
private static String Contacts = "";
Now create constructor of main class which gets the contacts from child window and is saved in static
variable. All the methods and variables defined in the main page default constructor should be included in this construct. This is also important.
public MainPage(string contactlist)
{
InitializeComponent();
Contacts = contactlist;
this.Loaded += new RoutedEventHandler(MainPage_Loaded);
timeStart.ValueChanged +=
new RoutedPropertyChangedEventHandler<DateTime?>(timeStart_ValueChanged);
}
public MainPage()
{
InitializeComponent();
this.Loaded += new RoutedEventHandler(MainPage_Loaded);
timeStart.ValueChanged +=
new RoutedPropertyChangedEventHandler<DateTime?>(timeStart_ValueChanged);
}
Now on closing of popup window(child window), add all those contacts to the read only text box for contacts.
private void button1_Click(object sender, RoutedEventArgs e)
{
Popup myPopUp = new Popup();
myPopUp.Show();
myPopUp.Closed += new EventHandler(myPopUp_Closed);
}
void myPopUp_Closed(object sender, EventArgs e)
{
txtContacts.Text = Contacts;
}
Child Window Code
Now in the child window CS code file, we have to send all selected contacts to the main page when ok button is clicked.
bool IsLoopNeeded = false;
bool IsContactPresent = true;
CheckBox chk_header;
private String _returnContactList = "";
public String returnContactList
{
get
{
return _returnContactList;
}
set
{
_returnContactList = value;
}
}
On Child window close event, the following code is executed:
void Popup_Closed(object sender, EventArgs e)
{
if (this.DialogResult.Value == true)
{
if (IsLoopNeeded == false && IsContactPresent == true)
{
for (int i = 0;i $ allcontacts.Count; i++)
{
_returnContactList += allcontacts[i].Email.ToString() + ";";
}
}
else if (IsLoopNeeded == false && IsContactPresent == false)
{
_returnContactList = "";
}
else if (IsLoopNeeded == true)
{
for(int i=0;i $ contactlist.Count;i++)
_returnContactList += contactlist[i].Fname.ToString() + ";";
}
MainPage obj = new MainPage(returnContactList);
}
}
The following screen shot shows data retrieved from child window shows in read only text box.
Saving Meeting Information into Database and Sending Mail
Now we can used WCF service again to save the data in to database. Below is the WCF code for saving records and main page code to call WCF methods.
[OperationContract]
public string saveSchedule(string agenda, string location,
DateTime StartTime, DateTime EndTime, string contacts, string details)
{
SqlConnection con = new SqlConnection();
con.ConnectionString = WebConfigurationManager.ConnectionStrings
["ConnectionString"].ConnectionString;
SqlCommand cmd = new SqlCommand();
con.Open();
cmd.Connection = con;
cmd.CommandText = "insert into Meeting values
(@Title,@Location,@StartTime,@EndTime,@Contact,@Details,@CreatedDate)";
cmd.Parameters.AddWithValue("@Title", agenda);
cmd.Parameters.AddWithValue("@Location", location);
cmd.Parameters.AddWithValue("@StartTime", StartTime);
cmd.Parameters.AddWithValue("@EndTime", EndTime);
cmd.Parameters.AddWithValue("@Contact", contacts);
cmd.Parameters.AddWithValue("@Details", details);
cmd.Parameters.AddWithValue("@CreatedDate", System.DateTime.Now);
cmd.ExecuteNonQuery();
return "Record is saved successfully, Thanks for submitting schedule for meeting.";
}
[OperationContract]
public string sendmail(string agenda, string Location, string contacts, string details)
{
try
{
SmtpClient smtpClient = new SmtpClient();
MailMessage objMail = new MailMessage();
MailAddress objMail_fromAddress= new MailAddress("bablu909@gmail.com");
MailAddress objMail_toAddress= new MailAddress(contacts);
objMail.IsBodyHtml=true;
objMail.From=objMail_fromAddress;
objMail.To.Add(objMail_toAddress);
objMail.Subject=agenda+"<br>";
objMail.Body=" "+ details + " Location:-" +Location+ "";
objMail.Priority=MailPriority.High;
smtpClient.Credentials= new System.Net.NetworkCredential
("bablu909@gmail.com","123balwant");
smtpClient.EnableSsl=true;
smtpClient.Send(objMail);
return "An Email is send to all recipients for the meeting schedule";
}
catch (Exception ex)
{
return "Error Sending Email Please Try again" +ex;
}
}
The main page code for calling WCF methods in main page and passing parameters.
private void btSchedule_Click(object sender, RoutedEventArgs e)
{
DateTime start = Convert.ToDateTime(dateStart.Text +" " +
timeStart.Value.Value.TimeOfDay);
DateTime end = Convert.ToDateTime(dateEnd.Text +" " + timeEnd.Value.Value.TimeOfDay);
ServiceReference1.Service1Client client = new ServiceReference1.Service1Client();
client.saveScheduleAsync
(txtAgenda.Text,txtLocation.Text,start,end,txtContacts.Text,txtDetails.Text);
client.saveScheduleCompleted +=
new EventHandler<saveScheduleCompletedEventArgs>(client_saveScheduleCompleted);
client.sendmailAsync(txtAgenda.Text,txtLocation.Text,
txtContacts.Text,txtDetails.Text);
client.sendmailCompleted +=
new EventHandler<sendmailCompletedEventArgs>(client_sendmailCompleted);
}
void client_saveScheduleCompleted(object sender, saveScheduleCompletedEventArgs e)
{
lblMessage.Content = e.Result;
}
void client_sendmailCompleted(object sender, sendmailCompletedEventArgs e)
{
lblMessage.Content += e.Result;
}
Note
The complete source code and database script for this application can be found in the download link. Please inform me if you find that changing or any type of updating is required for this article or for the previous article. This article is complete only if you read Part 1.
Thanks and please provide feedback so that I can improve the quality and post more articles.