DatePicker.Value.Set Error binding to datasource

2019-02-23 17:34发布

问题:

I have a bindingsource control called binding, on a form in VS2012, and a DateTimePicker control bound to it.

for the binding properties I have MinDate = 1/01/1753 and MaxDate = 31/12/9998 Value has been set by picking Today from the calender 5/04/2013 11:27 AM

I set a bindingsource up using

var dset = base.Context.ContactEvents;
var qry = dset.Where(p => p.Id > 0).OrderBy(x => x.Id);
qry.Load();
this.bindingSource.DataSource = dset.Local.ToBindingList();

The bindingsource is used in the following manner;

 public void RefreshBindingDataSourceAndPosition(BindingSource binding)
  {
    binding.DataSource = this.bindingSource.DataSource;  // error raised here
    binding.Position = this.bindingSource.Position;
  }

The error information is

System.ArgumentOutOfRangeException crossed a native/managed boundary HResult=-2146233086 Message=Value of '1/01/0001 12:00:00 AM' is not valid for 'Value'. 'Value' should be between 'MinDate' and 'MaxDate'. Parameter name: Value Source=System.Windows.Forms ParamName=Value StackTrace: at System.Windows.Forms.DateTimePicker.set_Value(DateTime value) InnerException:

I can work around the problem by not binding the Data Picker , and setting it in the EventsBindingSource_CurrentChanged event

However it seems odd to have to do this. How can I get the databinding working?

[Update] This problem is similar to the one described here I have tried to reproduce the problem in a simpler project so as to try and isolate the cause, however it works in the simpler project. Also the project works on another computer. The problem occurs on my computer with both SQL Server 2012 and 2008R2. I have tried altering the date format and country in control panel. Also I have tried different settings for the format property. I have also tried setting the date field to support null.

When I copy the error to the clipboard it shows the following ;

System.Reflection.TargetInvocationException occurred HResult=-2146232828 Message=Exception has been thrown by the target of an invocation. Source=mscorlib StackTrace: at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor) at System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(Object obj, Object[] parameters, Object[] arguments) InnerException: System.ArgumentOutOfRangeException HResult=-2146233086 Message=Value of '1/01/0001 12:00:00 AM' is not valid for 'Value'. 'Value' should be between 'MinDate' and 'MaxDate'. Parameter name: Value Source=System.Windows.Forms ParamName=Value StackTrace: at System.Windows.Forms.DateTimePicker.set_Value(DateTime value) InnerException:

My EF class is as follows

public class ContactEvent : LoggedEntity
{

    public virtual SortableBindingList<ContactEventAttendee> Attendees { get; private set; }
    public virtual ContactEventType ContactEventType { get; set; }
    public string Details { get; set; }
    public DateTime? EventTime { get; set; }
    public virtual SortableBindingList<ContactEventItem> Items { get; private set; }
    public int State { get; set; }
    public string Title { get; set; }
    public override string ToString()
    {
        return "Contact Events";
    }
}

it inherits from

public abstract class LoggedEntity
{

   public LoggedEntity()
    {
        this.RowId = Guid.NewGuid();
        this.RowVersionId = 0;
        AppDomain dm = AppDomain.CurrentDomain;  // Gets the current application domain for the current Thread.
        object s = AppDomain.CurrentDomain.GetData("SiteNumber");
        this.SourceSiteNumber = Convert.ToInt32(s);
    }

    public LoggedEntity(int SiteNumber)
    {
        // the following 3 are used to identify the version 
        this.RowId = Guid.NewGuid();
        this.RowVersionId = 0;
        this.SourceSiteNumber = SiteNumber;
    }

    public int Id { get; set; }
    public Guid RowId { get; set; }

    [ConcurrencyCheck]
    public int RowVersionId { get; set; }
    public int SourceSiteNumber { get; set; }

}

[update] A similar problem is here

[update] Another here makes me think I need to look at how keys are being processed.

[update] I noticed the following in the output window

A first chance exception of type 'System.ArgumentOutOfRangeException' occurred in System.Windows.Forms.dll

[update] This led me to here

and after turning on the debug options I found an error

Invalid object name 'dbo.__MigrationHistory'.

however that is a known bug in EF5

[Update]: I found another person with similar unsolved issues here Discovered I dont have problems when running the .EXE

[update] I can skip over the error by disabling "Break when exceptions cross App Domain or managed/native boundary in Tools->Options->Debugging->General

[update] I adding the following, so I could inspect the control properties.

    private void EventsBindingSource_BindingComplete(object sender, BindingCompleteEventArgs e)
    {
        // If the BindingComplete state is anything other than success, 
        // set the ErrorProvider to the error message.
        if (e.BindingCompleteState != BindingCompleteState.Success)
        {
            errorProvider1.SetError((Control)e.Binding.BindableComponent, e.ErrorText);
            var errdesc = e.ErrorText;
            var ctrl = (Control)e.Binding.BindableComponent;
              var info = string.Format(
               "{0} {1}",errdesc,
               ctrl.ToString());

            Debug.Print(info);
            // "Value of '1/1/0001 12:00:00 AM' is not valid for 'Value'. 
             'Value' should be between 'MinDate' and 'MaxDate'.\r\nParameter name: 
             Value System.Windows.Forms.DateTimePicker, Value: 1/1/1900 12:00:00 AM"

        }
        else
        {
            errorProvider1.SetError((Control)e.Binding.BindableComponent, "");
        }
    }

回答1:

The cause of the exception may be that the DatePicker's DataBinding "Value" property has been set to a BindingSource field. Only the DatePicker's DataBinding "Text" property needs to be set for the data binding to work correctly. Check if there is a value in the DatePicker's DataBinding "Value" property field, once removed the issue should go away.



回答2:

It seems that the key problem is the property is a Nullable DateTime. Probably the null value used to transcribe the DateTimePicker component is '1 / 01/0001 12:00:00 AM 'which creates a problem related to the configuration of MinValue and MaxValue. Using the Advanced tab of the DataBinding has an option to set the value to be used to null One way to solve this would be setting the value to null as MinDate, or we can set the MinDate with the value '01 / 01/0001 12:00 : 00 AM '. Despite my limited experience, I believe that this may be the source of your problems. the link http://msdn.microsoft.com/en-us/library/aa480734.aspx can see something else on the Advanced tab of the property DataBinding.