Exception of type 'System.InvalidOperationException' occurred in mscorlib.ni.dll but was not handled

断了今生、忘了曾经 提交于 2019-12-13 06:04:06

问题


I am getting this message:

"Exception of type 'System.InvalidOperationException' occurred in mscorlib.ni.dll but was not handled in user code"

This problem occurs when my TimePicker dont have any time choosen when I leave it empty.

My code for the TimePicker looks like this;

DateTime break1S = (DateTime)startBreak1.Value; 

Its on this row i am getting the message but if i set a time for the picker i dont get an message.

Any ideas?

**


回答1:


If startBreak1.Value is a string :

if (startBreak1!= null) 
    DateTime.TryParse(startBreak1.Value, out break1S);

if it's a Nullable<DateTime> (and I think it is)

DateTime break1S = startBreak1.HasValue 
                          ? startBreak1.Value
                          : new DateTime()//or anything you want;

or accept that break1S can be nullable :

var break1S = startBreak1;



回答2:


The solution look like this: 

DateTime break1S = startBreak1.Value.HasValue ? startBreak1.Value.Value : DateTime.MinValue;



回答3:


you can try this:

DateTime break1S = startBreak1.Value.HasValue ? startBreak1.Value.Value : DateTime.MinValue;


来源:https://stackoverflow.com/questions/16893855/exception-of-type-system-invalidoperationexception-occurred-in-mscorlib-ni-dll

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!