我很难确定我在这里做错了什么。我的逻辑是只有在文本框发生变化时才运行存储过程(UpdateRate
)。如果文本框中没有更改,只需跳过该行信息的存储过程并转到下一个。
有人可以帮我解决这个问题吗?我尝试了一切。请记住,我是新手,可能无法完全理解复杂的答案。
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"/>
我在if语句的btnSave_Click里面设置了2个断点,启动了解决方案,更改了其中一个文本框,并注意到无论我做什么,这两个语句都会产生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;
}
答案 0 :(得分:0)
你应该在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;
}