I was using an ASP.NET Wizard control for adding new users to the web-based application that I developed. Everything works well and fine. Now, I got a new requirement from the Admin of the system which is sending an email notification to the new user telling him that he has been added to the system. I added the Mail function to my code and it works. Now, after clicking on the Finished button on the Wizard, the user will be added to the database and the system will send an email notification to the user, then the wizard will show me the Success message. However, sometimes it takes a long time until the system sending that email to the user and the wizard showing me the Success message.
So How I can display a (Please Wait ) screen/PopUp during the time that the system is working on sending that email to the user?
Code-Behind with Mail function:
protected void Wizard1_FinishButtonClick(object sender, WizardNavigationEventArgs e)
{
string username = TextBox1.Text;
if (!String.IsNullOrEmpty(radio1.SelectedValue) && !String.IsNullOrEmpty(username))
{
string connString = "Data Source=localhost;Initial Catalog=TestDB;Integrated Security=True";
string insertUserCommand = "INSERT INTO employee (Name, Username, JobTitle, BadgeNo, EmpOrgType, DivisionCode) values (@Name, @Username, @JobTitle, @BadgeNo, @EmpOrgType, @DivisionCode)";
string cmdText = "SELECT Count(*) FROM employee WHERE Username = '" + username + "'";
using (SqlConnection conn = new SqlConnection(connString))
{
conn.Open();
// Open DB connection.
using (SqlCommand cmd = new SqlCommand(cmdText, conn))
{
if ((int)cmd.ExecuteScalar() == 0){
SqlCommand cmd2 = new SqlCommand(insertUserCommand, conn);
cmd2.Parameters.AddWithValue("@Name", user.Name);
cmd2.Parameters.AddWithValue("@Username", username);
cmd2.Parameters.AddWithValue("@JobTitle", jobTitle);
cmd2.Parameters.AddWithValue("@BadgeNo", EmpNo.));
cmd2.Parameters.AddWithValue("@EmpOrgType", orgType);
cmd2.Parameters.AddWithValue("@DivisionCode", orgCode);
cmd2.ExecuteNonQuery();
}
}
}
//For updating the role of the user
string deleteCommand = "DELETE FROM UserRole where Username=@Username";
string insertCommand = "INSERT INTO UserRole (RoleID,Username) values(@RoleID,@Username)";
using (SqlConnection conn = new SqlConnection(connString))
{
conn.Open();
//using (SqlCommand cmd = new SqlCommand(cmdText, conn))
using (SqlCommand cmd = new SqlCommand(deleteCommand, conn))
{
cmd.Parameters.AddWithValue("@Username", username);
cmd.ExecuteNonQuery();
//Now the insert
cmd.CommandText = insertCommand;
cmd.Parameters.Clear();
cmd.Parameters.AddWithValue("@RoleID", radio1.SelectedValue);
cmd.Parameters.AddWithValue("@Username", username);
cmd.ExecuteNonQuery();
//infoSpan.InnerText = String.Format("The users role has been updated to - {0}", radio1.SelectedValue);
//cmd.ExecuteScalar();
//infoSpan.InnerText = String.Format("The users role has been updated to - {0}", radio1.SelectedValue);
}
}
Wizard1.Visible = false;
wizard.InnerHtml = @"<p><b>The task has been done successfully.</b> <br /> <a href='UserManagement.aspx'>Edit Another User</a></p>";
}
SendEmailToUser(username);
}
/*****************************************************/
protected void SendNotificationByEmail(string toAddresses, string fromAddress, string MailSubject, string MessageBody, bool isBodyHtml)
{
SmtpClient sc = new SmtpClient("MailAddress");
try
{
MailMessage msg = new MailMessage();
msg.From = new MailAddress("test@mailAddress.com", "Test Sys");
msg.Bcc.Add(toAddresses);
msg.Subject = MailSubject;
msg.Body = MessageBody;
msg.IsBodyHtml = isBodyHtml;
sc.Send(msg);
}
catch (Exception ex)
{
throw ex;
}
}
protected void Send(string username)
{
string connString = "Data Source=localhost;Initial Catalog=TestDB;Integrated Security=True";
string networkID = username.ToString();
using (SqlConnection conn = new SqlConnection(connString))
{
var sbEmailAddresses = new System.Text.StringBuilder(2000);
//initiate the varibles that will be retreived from the database
string name = null;
// Open DB connection.
conn.Open();
string cmdText2 = @"SELECT Name
FROM dbo.employee
WHERE (Username = @networkID)";
using (SqlCommand cmd = new SqlCommand(cmdText2, conn))
{
cmd.Parameters.AddWithValue("@networkID", networkID);
SqlDataReader reader = cmd.ExecuteReader();
if (reader != null)
{
if (reader.Read())
{
name = reader["Name"].ToString();
sbEmailAddresses.Append(username).Append("@mailAddress.com");
}
}
//var sEMailAddresses = sbEmailAddresses.ToString();
string body = @"...........................";
SendNotificationByEmail(sbEmailAddresses.ToString(), "", "Welcome...", body, true);
sbEmailAddresses.Clear();
reader.Close();
}
conn.Close();
}
}
If your application uses an
UpdatePanel, you can use theUpdateProgresscontrol to show whatever you want while the request is processing.