locked
Executing ExecuteReaderAsync resets other controls back to old value RRS feed

  • Question

  • 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:

    Tuesday, June 30, 2020 10:24 AM

All replies

  • Hi QuestionBot,
    Based on your description, I suggest you can check the value change of datetimepicker by making a breakpoint in ValueChanged.
    Best Regards,
    Daniel Zhang


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, July 1, 2020 9:37 AM
  • Hi QuestionBot,
    Based on your description, I suggest you can check the value change of datetimepicker by making a breakpoint in ValueChanged.
    Best Regards,
    Daniel Zhang


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    This is what I would like to do but there is a bug in VS 2019 causing VS to get in a state of unresponsiveness whenever I set a breakpoint at certain points in my code. Problem has been already reported: here and here.



    • Edited by QuestionBot Wednesday, July 1, 2020 10:47 AM
    Wednesday, July 1, 2020 10:47 AM
  • Hi QuestionBot,
    Maybe it is a problem of the Visual Studio, you can update your Visual Studio or try to use other versions of the Visual Studio.
    Best Regards,
    Daniel Zhang


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Thursday, July 2, 2020 7:32 AM
  • .NET Core seems to be pretty buggy still. I rebuilt my project with .NET Framework and everything works fine although the code is the same.
    Sunday, July 5, 2020 11:18 AM
  • Hi QuestionBot,
    Data breakpoints in .NET Core won't work for:
    1.Properties that are not expandable in the tooltip, Locals, Autos, or Watch window
    2.Static variables
    3.Classes with the DebuggerTypeProxy Attribute
    4.Fields inside of structs
    More details you can refer to this document.
    Best Regards,
    Daniel Zhang


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, July 6, 2020 6:18 AM