我很難確定我在這裏做錯了什麼。我的邏輯是隻有在文本框發生變化時才運行存儲過程(UpdateRate
)。如果文本框中沒有更改,則跳過該行信息的存儲過程並移至下一個。TextChangedEvent在文本框更改與否時不會觸發
有人能幫我解決這個問題嗎?我已經嘗試了一切。請記住,我是新手,可能無法完全理解複雜的答案。
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"/>
我已經設置2個斷點內內btnSave_Click的if語句,啓動了解決方案,改變了文本框和通知的一個無論我做什麼,這兩個語句都會導致True
。即使我禁用我的文本框並單擊保存按鈕,我仍然得到True
而不是False
。當我嘗試調試我注意到下面的錯誤的TextChangedEvent,爲我改變文本框的一個:
我真的很感激任何建議。謝謝!
嘗試基於@ user2107843答案。它解決了我最初的問題,但是當我第二次單擊保存時,它將運行兩個存儲過程,而不是隻更改一個。所以,如果我更改txtAHW然後單擊保存,它將起作用,它只運行txtAHW的存儲過程。如果在此之後我也改變了txtOHW,則存儲過程爲兩者運行,而不是僅爲txtOHW運行。我的邏輯是txtAHW已經被保存了,所以不需要再次運行。可一些幫助我提高了這一點:
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;
}
的代碼將在構造函數中,這些框的Text屬性進行虛假的,所以布爾變量總是爲真。沒有任何跡象表明會回到錯誤,所以它們永遠是真的。 –
謝謝你的幫助漢斯。這聽起來像解決我的問題。你能告訴我怎樣才能讓他們回到錯誤?我會很感激一個例子。 – iCosmin
答案和解決方案不屬於該問題。如果您認爲這值得保留,則可以將其作爲答案發布。 –