I'm trying to allow my webapp to send an email update whenever a data is being inserted into the database like the codes i'll show below.
This is a btnAssign where it will update the relevant database table and column with data
protected void btnAssign_Click1(object sender, EventArgs e) { using (var connAdd = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString)) { String assign = ddlpid1.SelectedValue; connAdd.Open(); var sql = "Update MemberReport Set assignto ='" + assign + "', caseprogress = 'ongoing' where memberreportID='" + lbmemberreportid.Text + "'"; using (var cmdAdd = new SqlCommand(sql, connAdd)) { cmdAdd.ExecuteNonQuery(); } sql = "Insert into PoliceReport(memberreportid) values('" + lbmemberreportid.Text + "')"; // sql = "Update PoliceAccount Set handle ='" + assign + "' where policeid ='" + ddlpid1.SelectedValue + "' OR '" + ddlpid2.SelectedValue + "'"; using (var cmdAdd = new SqlCommand(sql, connAdd)) { cmdAdd.ExecuteNonQuery(); } sql = "Update PoliceAccount Set handle ='" + lbmemberreportid.Text + "' where policeid ='" + ddlpid1.SelectedValue + "'"; // sql = "Update PoliceAccount Set handle ='" + assign + "' where policeid ='" + ddlpid1.SelectedValue + "' OR '" + ddlpid2.SelectedValue + "'"; using (var cmdAdd = new SqlCommand(sql, connAdd)) { cmdAdd.ExecuteNonQuery(); } }
The insertion / updating of database part is working fine. When i addthe smtp codes to send email by selecting a column, it didnt work.
SqlCommand cmd = new SqlCommand(); SqlDataReader dr; //SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString); //con.Open(); // get the records matching the supplied username or email id. cmd = new SqlCommand("select * from PoliceAccount where handle='" + lbmemberreportid.Text + "'", connAdd); dr = cmd.ExecuteReader(); cmd.Dispose(); if (dr.HasRows) { dr.Read(); StringBuilder strBody = new StringBuilder(); //Passing emailid,username and generated unique code via querystring. For testing pass your localhost number and while making online pass your domain name instead of localhost path. strBody.Append("<a>Please be notified that you've been assigned a case to handle. Please proceed to the scene with immediate effect.</a>"); // sbody.Append("&uCode=" + uniqueCode + "&uName=" + txtUserName.Text + ">Click here to change your password</a>"); System.Net.Mail.MailMessage mail = new System.Net.Mail.MailMessage("apr13mpsip@gmail.com", dr["email"].ToString(), "Case Pending", strBody.ToString()); //pasing the Gmail credentials to send the email System.Net.NetworkCredential mailAuthenticaion = new System.Net.NetworkCredential("apr13mpsip@gmail.com", "Temasekpoly13"); System.Net.Mail.SmtpClient mailclient = new System.Net.Mail.SmtpClient("smtp.gmail.com", 587); mailclient.EnableSsl = true; mailclient.Credentials = mailAuthenticaion; mail.IsBodyHtml = true; mailclient.Send(mail); dr.Close(); dr.Dispose(); cmd.ExecuteReader(); cmd.Dispose(); //con.Close(); lbmemberreportid.Text = ""; ddllocation.SelectedIndex = 0; ddlnumber.SelectedIndex = 0; ddlpid1.SelectedIndex = 0; tbdetails.Text = ""; tbproperty.Text = ""; tbsuspect.Text = ""; ddlpid1.Visible = false; LoadGrid(); lblmsg.ForeColor = System.Drawing.Color.Green; lblmsg.Text = "MemberReportID" + Session["memberreportid"] + "has been successfully assigned"; } connAdd.Close(); }
To make matter worse, the label where the message is suppose to appear did not appear. Which means after inserting the data, the code basically stop running. I added a txtFile here if the code i pasted above is confusing.
http://www.filedropper.com/admintask
I really still cant figure out why does my email not run after inserting the data into the database.
Regards.
http://www.filedropper.com/admintask