2013-09-01 32 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列。