简体   繁体   中英

TextChangedEvent does not fire when textbox changes or not

I am having a difficult time pinpointing what am I doing wrong here. My logic is to run the stored procedure ( UpdateRate ) only if a textbox changes. If there is no change in the textbox, just skip the stored procedure for that row of information and move to the next one.

Can someone please help me figure this out? I have tried everything. Keep in mind that I am new at this and might not fully understand complicated answers.

C#:

public partial class MainWindow : 
{
    internal static string oldAvgRate;
    internal static string oldOTRate;
    internal static string ratetype;
    internal static string rtOT;

    public MainWindow()
    {
        InitializeComponent();

        string connectionString = "datasource=;port=;username=;password=";
        string sDate = DateTime.Now.ToString("yyyy-MM-dd");
        MySqlConnection connection = new MySqlConnection(connectionString);

        MySqlCommand avgRate = new MySqlCommand("Select ID, DateFrom, DateTo, RateType, Amount, Description from Daily.Rates where RateType = 'Average Hourly Wages' and DateTo >= @sDate", connection);
        avgRate.Parameters.Add(new MySqlParameter("sDate", sDate));

        MySqlCommand otRate = new MySqlCommand("Select ID, DateFrom, DateTo, RateType, Amount, Description from Daily.Rates where RateType = 'Average OT Hourly Wages' and DateTo >= @sDate", connection);
        otRate.Parameters.Add(new MySqlParameter("sDate", sDate));

        try
        {
            connection.Open();

            MySqlDataReader AvgR = avgRate.ExecuteReader();

            while (AvgR.Read())
            {
                txtAHW.Text = AvgR["Amount"].ToString(); 
                dfAHW.Text = AvgR["DateFrom"].ToString();
                dtAHW.Text = AvgR["DateTo"].ToString();
                txtcommAHW.Text = AvgR["Description"].ToString();

                oldAvgRate = txtAHW.Text = AvgR["Amount"].ToString();
                ratetype = AvgR["RateType"].ToString();
            }

            AvgR.Close();
            AvgR.Dispose();

            MySqlDataReader OtR = otRate.ExecuteReader();

            while (OtR.Read())
            {
                txtOTHW.Text = OtR["Amount"].ToString();
                dfOTHW.Text = OtR["DateFrom"].ToString();
                dtOTHW.Text = OtR["DateTo"].ToString();
                txtcommOTHW.Text = OtR["Description"].ToString();

                oldOTRate = txtOTHW.Text = OtR["Amount"].ToString();
                rtOT = OtR["RateType"].ToString();
            }   

            OtR.Close();
            OtR.Dispose();
        }

        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }

        connection.Close();
    }

        private string UpdateRate(string dateFrom, string newRate, string oldRate, string ratetype, string description)
        {
            string connectionString = "datasource=;port=;Initial Catalog='';username=;password=";
            MySqlConnection connection = new MySqlConnection(connectionString);

            try
            {
                connection.Open();

                MySqlCommand cmd = new MySqlCommand("UpdateRate", connection);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add("@p_DateFrom", MySqlDbType.Date).Value = dateFrom;
                cmd.Parameters.Add("@p_NewAmount", MySqlDbType.Decimal).Value = newRate;
                cmd.Parameters.Add("@p_OldAmount", MySqlDbType.Decimal).Value = oldRate;
                cmd.Parameters.Add("@p_RateType", MySqlDbType.VarChar).Value = ratetype;
                cmd.Parameters.Add("@p_Description", MySqlDbType.VarChar).Value = description;

                cmd.ExecuteNonQuery();              
                connection.Close();
                return newRate;            
            }

            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            return null;
        }

        private bool txtAHWHasChangedFlag;
        private bool txtOTHWHasChangedFlag;

        private void textChangedEventHandler(object sender, TextChangedEventArgs args)
        {        
            var control = sender as TextBox;

            if (control.Name == "txtAHW" )
                txtAHWHasChangedFlag = true;
            else if (control.Name == "txtOTHW")
                txtOTHWHasChangedFlag = true;
        }

        private void btnSave_Click(object sender, RoutedEventArgs e)
        {
            if (txtAHWHasChangedFlag) //True regardless if changes are made in the textbox or not :(
            {
            oldAvgRate = UpdateRate(dfAHW.SelectedDate.Value.ToString("yyyy-MM-dd"), txtAHW.Text, oldAvgRate, ratetype, txtcommAHW.Text);
            MessageBox.Show("Done", "Test", MessageBoxButton.OK);
            }

            if (txtOTHWHasChangedFlag) //True regardless if changes are made in the textbox or not :(
            {
            oldOTRate = UpdateRate(dfOTHW.SelectedDate.Value.ToString("yyyy-MM-dd"), txtOTHW.Text, oldOTRate, rtOT, txtcommOTHW.Text);
            MessageBox.Show("Done", "Test", MessageBoxButton.OK);
            }

            if (!txtAHWHasChangedFlag && !txtOTHWHasChangedFlag)
            {    
            MessageBox.Show("Nothing has changed", "Test", MessageBoxButton.OK);
            return;
            }
        }
}

XAML:

<TextBox x:Name="txtAHW" TextChanged="textChangedEventHandler"/>
<TextBox x:Name="txtOTHW" TextChanged="textChangedEventHandler"/>

I have set 2 breakpoints inside inside btnSave_Click on the if statements, started the solution, changed one of the textboxes and notice that whatever I do, both statements result in True . Even if I disable my textboxes and click on the save button, I still get True instead of False . When I attempt to debug I notice the following error on the TextChangedEvent, for one of the textboxes that I change:

在此处输入图片说明

I would really appreciate any suggestion. Thank you!

Attempt based on @user2107843 answer. It solves my initial problem, but when I click save the second time, it runs both stored procedures instead of only the one that as changed. So if I change txtAHW and then click save, it works, it only runs the stored procedure for txtAHW. If right after that I change txtOHW as well, the stored procedure runs for both instead for running only for txtOHW. My logic here is that txtAHW has already been saved so no need to run again. Can some help me improve this:

private void textChangedEventHandler(object sender, TextChangedEventArgs args)
{        
    var control = sender as TextBox;

    if (control.Name == "txtAHW")

        if (oldAvgRate != txtAHW.Text && oldAvgRate != null)
            txtAHWHasChangedFlag = true;
        else
            txtAHWHasChangedFlag = false;
    else if (control.Name == "txtOTHW")
        if (oldOTRate != txtOTHW.Text && oldOTRate != null)
            txtOTHWHasChangedFlag = true;
        else
            txtOTHWHasChangedFlag = false;
}

You should make false in textChangedEventHandler

private void textChangedEventHandler(object sender, TextChangedEventArgs args)
        {        
            var control = sender as TextBox;

            if (control.Name == "txtAHW" )

                if(oldAvgRate != txtAHW.Text && oldAvgRate !=null)
                   txtAHWHasChangedFlag = true;
                 else
                   txtAHWHasChangedFlag = false
            else if (control.Name == "txtOTHW")
                txtOTHWHasChangedFlag = true;
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM