0

以下に示すコードのように、データがデータベースに挿入されるたびに、webapp が電子メールの更新を送信できるようにしようとしています。

これは、関連するデータベース テーブルと列をデータで更新する 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