0

我试图让我的 web 应用程序在将数据插入数据库时​​发送电子邮件更新,就像我将在下面显示的代码一样。

这是一个 btnAssign,它将用数据更新相关的数据库表和列

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();
                    }
}

数据库部分的插入/更新工作正常。当我通过选择一列添加 smtp 代码以发送电子邮件时,它不起作用。

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();
                }

更糟糕的是,消息应该出现的标签没有出现。这意味着插入数据后,代码基本停止运行。如果我上面粘贴的代码令人困惑,我在此处的链接中添加了一个 txtFile 。

我仍然无法弄清楚为什么我的电子邮件在将数据插入数据库后无法运行。

问候。

4

1 回答 1

1

您正在阅读dr["email"].ToString()但仅assignto在您的 select sql 语句中选择列。您可以更改 select sql 语句以同时选择assigntoemail列。

于 2013-09-01T05:54:46.860 回答