【发布时间】:2022-01-09 12:43:23
【问题描述】:
当我的 Datagrid 中的项目更新时,一封电子邮件会发送到我的 gmail。这很好用!
我想做出改变。仅当 [Stock].Amount 更新为 0 时才会发送电子邮件。希望任何人都可以帮助我。
这是我的查询:
private void BtnSend_Click(object sender, RoutedEventArgs e)
{
DataRowView o = (DataRowView)g2.SelectedItem;
int id = Convert.ToInt32(o.Row.ItemArray[0]);
int Total = Convert.ToInt32(o.Row.ItemArray[5]);
try
{
const string query = @"UPDATE [Stock] SET [Stock].Amount = @Total WHERE [Stock].Id = @Id;";
using (SqlConnection con = new SqlConnection(connectionstring))
using (SqlCommand cmd = new SqlCommand(query, con))
{
cmd.Parameters.Add("@Id", SqlDbType.Int).Value = id;
cmd.Parameters.Add("@Total", SqlDbType.Int).Value = Total;
con.Open();
cmd.ExecuteNonQuery();
}
MessageBox.Show("update completed successfully");
binddatagrid();
{
SqlCommand second = new SqlCommand($"SELECT COUNT(*) FROM [Stock] WHERE [Stock].Amount = 0;'");
var fromAddress = new MailAddress("FROM-Email", "From Name");
var toAddress = new MailAddress("To-Email", "To Name");
const string fromPassword = "*****";
const string subject = "Test";
const string body = "text";
var smtp = new SmtpClient
{
Host = "smtp.gmail.com",
Port = 587,
EnableSsl = true,
DeliveryMethod = SmtpDeliveryMethod.Network,
UseDefaultCredentials = false,
Credentials = new NetworkCredential(fromAddress.Address, fromPassword),
Timeout = 20000
};
using (var message = new MailMessage(fromAddress, toAddress)
{
Subject = subject,
Body = body
})
{
smtp.Send(message);
MessageBox.Show("E-mail has been sent.", "Error", MessageBoxButton.OK, MessageBoxImage.Information);
}
}
}
catch
{
MessageBox.Show("A field was entered incorrectly. Correct this and try adding the information again.", "Error", MessageBoxButton.OK, MessageBoxImage.Information);
}
【问题讨论】:
-
您是否尝试过检查 0 的
if语句? -
你的意思是:if (Total == 0) {mail} 我用了很多 if 的但没用……但我用了这个,它奏效了……有时你必须清醒一下哈哈。
标签: c# wpf email if-statement data-binding