2

I have a C# Windows Forms application which we originally wrote in VS2008 and have ported to VS2010. It has a binding between a object that we wrote with two date properties, bound to two datetime picker controls.

Here is the designer code, showing the bindings of one of the date time pickers; the other is identical except of course the name:

private System.Windows.Forms.DateTimePicker dtTradeDate;
this.dtTradeDate = new System.Windows.Forms.DateTimePicker();
// 
// dtTradeDate
// 
this.dtTradeDate.DataBindings.Add(new System.Windows.Forms.Binding("Value", this.bsWorkflowItem, "TradeDate", true, System.Windows.Forms.DataSourceUpdateMode.OnPropertyChanged));
this.dtTradeDate.Format = System.Windows.Forms.DateTimePickerFormat.Short;
this.dtTradeDate.Location = new System.Drawing.Point(383, 43);
this.dtTradeDate.Name = "dtTradeDate";
this.dtTradeDate.Size = new System.Drawing.Size(99, 20);
this.dtTradeDate.TabIndex = 37;

The object properties are set up as get/set properties:

public DateTime TradeDate
{
    get { return _tradeDate; }
    set { _tradeDate = value; }
}

When the form starts up, we bind the binding source of the form to the object. You can see that I've added some explicit code to avoid the situation that is occurring below, but this has no effect:

//Initializing workflow item object
_wfItem = new CamraWorkflowItem(UserSession.User);

_wfItem.TradeDate = DateTime.Today;
_wfItem.EffectiveDate = DateTime.Today;
loading.IncrementLoadingSteps(2);

dtEffDate.Value = DateTime.Today;
dtTradeDate.Value = DateTime.Today;

bsWorkflowItem.DataSource = _wfItem;

Now here is the really, really strange part. This works perfectly well in VS2008 and also on two of my colleagues' VS2010 workstations. However it doesn't work on mine. When it hits the last line, I get the following exception:

System.ArgumentOutOfRangeException crossed a native/managed boundary
Message=Value of '1/1/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'm at a loss here. If I disconnect both of the bindings, it works fine but obviously I need the binding to update the object. Also strangely if I look at the values of both of these dates in the debugger (with the exception box showing) I see a valid date; see below:

Ceci n'est pas un exception

The date values on the object are the same, so they clearly appear to be within the valid date range of the control.

This seems like a bug in Visual Studio... except it works on my colleagues' machines with the same version of .NET and Visual Studio. I've spent a day hacking at this and I'm at a loss... your help is greatly appreciated.

bluish
  • 26,356
  • 27
  • 122
  • 180
hocker
  • 688
  • 6
  • 18
  • 1
    Just a wild guess but have you checked that the regional settings on all three of your computers are the same? This might be a DateTime conversion issue – TBohnen.jnr Apr 05 '11 at 15:22
  • 1
    We are all set to English, United States so sadly that's not it. Thanks though, good thought. – hocker Apr 05 '11 at 15:52

2 Answers2

0

In your place I would:

  • Debug with a breakpoint on the CamraWorkflowItem.TradeDate property setter and getter, and ensure it has the value you're expecting.

  • Post the full stack trace.

Joe
  • 122,218
  • 32
  • 205
  • 338
  • Good idea. I put a breakpoint as you advised on both date setter/getters and the value never is set to 1/1/0001... only today's date as in my code above. The stack trace confirms that it is my code setting it only. – hocker Apr 05 '11 at 15:54
  • One other thought - the getter is never called. Why would that be? – hocker Apr 05 '11 at 16:08
  • Why would that be? - difficult to say without seeing more code and a stack trace. But perhaps at some point it's binding to something other than what you think. – Joe Apr 05 '11 at 18:14
0

I have an answer... or more precisely, a nasty workaround.

By breaking the databindings in the designer, then pasting the code that was previously generated by the designer to do the binding after where I bind the bindingsource to the object... it works:

        bsWorkflowItem.DataSource = _wfItem;
        this.dtTradeDate.DataBindings.Add(new System.Windows.Forms.Binding("Value", this.bsWorkflowItem, "TradeDate", true, System.Windows.Forms.DataSourceUpdateMode.OnPropertyChanged));
        this.dtEffDate.DataBindings.Add(new System.Windows.Forms.Binding("Value", this.bsWorkflowItem, "EffectiveDate", true, System.Windows.Forms.DataSourceUpdateMode.OnPropertyChanged));  

Getters and setters now work properly and the properties are properly bound. No exceptions occur.

But this just doesn't make any sense to me. Anyone have an idea why?

hocker
  • 688
  • 6
  • 18