我正在使用asp.net 4.0和sql server当我在应用程序中浏览时只有一些时间我看到这个错误,如果点击某些东西它解决了可以有人建议我如何克服这个
'System.InvalidOperationException:Nullable对象必须具有值.在System.ThrowHelper.ThrowInvalidOperationException(ExceptionResource资源)
您可能正在尝试访问null的可空对象的Value.
如果分配了一个值,则Value属性返回一个值,否则抛出System.InvalidOperationException.
您有多种选择来克服错误.例如:
int? a=null; // a test nullable object
//Console.WriteLine(a.Value); // this throws an InvalidOperationException
// using GetValueOrDefault()
Console.WriteLine(a.GetValueOrDefault()); //0 (default value for int)
//checking if a.HasValue
if(a.HasValue) Console.WriteLine(a.Value); // does not print anything as the if
// is false
// using the ?? operator
Console.WriteLine(a ?? -1); // prints -1
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
13656 次 |
| 最近记录: |