如果值为 0,则发送电子邮件 C# WPF

If value 0 send e-mail C# WPF

当我的 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);
        }

如果 Total 不是 0:

,您可以在发送任何电子邮件之前从该方法 return
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]);
    if (Total != 0) //< --
        return;

    try
    {
        //same code as before...
    }
    catch
    {
        MessageBox.Show("A field was entered incorrectly. Correct this and try adding the information again.", "Error", MessageBoxButton.OK, MessageBoxImage.Information);
    }
}