2013-09-01 72 views
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("[email protected]", dr["email"].ToString(), "Case Pending", strBody.ToString()); 
        //pasing the Gmail credentials to send the email 
        System.Net.NetworkCredential mailAuthenticaion = new System.Net.NetworkCredential("[email protected]", "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(); 
       } 

更糟糕的是,消息應該出現的標籤沒有出現。這意味着在插入數據後,代碼基本停止運行。我在link這裏添加了一個txtFile,如果我上面粘貼的代碼很混亂。

我真的不知道爲什麼我的電子郵件沒有運行後,將數據插入到數據庫中。

問候。

+0

您是否試過單步執行代碼以確定它爲什麼會停止? –

+0

嘗試步進以確切知道哪一行發生錯誤。 –

+0

嗯,因爲這個代碼工作時,我嘗試使用它爲我的忘記密碼功能。但當然,我必須根據我的要求做出相應的改變。所以基本上代碼的邏輯是好的,但只是添加到另一個頁面後,它不起作用 –

回答

1

您正在閱讀dr["email"].ToString(),但只在您的select sql語句中選擇assignto列。您可以更改select sql語句以選擇assigntoemail列。