Quantcast
Channel: Windows Forms Data Controls and Databinding forum
Viewing all articles
Browse latest Browse all 2535

Executing ExecuteReaderAsync resets other controls back to old value

$
0
0

I have different controls and a datagridview on my form. I use EF Core to bind the controls to a SQL Server DB. I use the following code to fill the controls except the datagridview:

private List<VCurrentProject> _currentProject;
private BindingSource _currentProjectBindingSource = new BindingSource();
bool blnDisableValueChangedHandler=false;

blnDisableValueChangedHandler = true;

datetimepicker1.DataBindings.Clear();
numericUpDown1.DataBindings.Clear();
datetimepicker2.DataBindings.Clear();
_currentProjectBindingSource.DataSource = null;

_currentProject = new List<VCurrentProject>(await Operations.GetProject(projectIdentifier));

_currentProjectBindingSource.DataSource = _currentProject;

datetimepicker1.DataBindings.Add("Value", _currentProjectBindingSource, "StartMonth");
numericUpDown1.DataBindings.Add("Value", _currentProjectBindingSource, "NumMonths");
datetimepicker2.DataBindings.Add("Value", _currentProjectBindingSource, "EndMonth");

blnDisableValueChangedHandler = false;

and the following code to fill the datagridview:

public static async Task<DataTable> FillDGV(string strStoredProc)
{
	DataTable dt = new DataTable();
	Microsoft.Data.SqlClient.SqlConnection cn = GetContextConn();
	//GetContextConn=Microsoft.Data.SqlClient.SqlConnection)Context.Database.GetDbConnection()
	await Task.Run(async () =>
	{
			using (var cmd = new Microsoft.Data.SqlClient.SqlCommand() { Connection = cn, CommandType = CommandType.StoredProcedure })
			{
				cmd.CommandText = strStoredProc;
				await cn.OpenAsync();
				dt.Load(await cmd.ExecuteReaderAsync());
				await cn.CloseAsync();
			}
	});

	return dt;
}

The ValueChangedHandler of a control does this:

private async void datetimepicker1_ValueChanged(object sender, EventArgs e)
{
	if (blnDisableValueChangedHandler) return;

	DateTime dteStart = (DateTime)datetimepicker1.Value;
	Operations.Context.VCurrentProject.First().StartMonth = dteStart;
	Operations.Context.VCurrentProject.First().EndMonth = dteStart.AddMonths((int)nudNrMonths.Value);
	this.ValidateChildren();
	await Operations.Context.SaveChangesAsync();

	LoadTabPage1(); //resets datasources for controls and datagridview as shown above
}


When using datetimepicker1 the value is changed in the database but as soon as

dt.Load(await cmd.ExecuteReaderAsync());

is executed in FillDGV() the value of datetimepicker1 jumps back to the value it had before although the value in the database remains changed.

What is wrong with the code?

EDIT:

The following adjustments don't help:


Viewing all articles
Browse latest Browse all 2535

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>