【发布时间】:2012-03-13 00:45:48
【问题描述】:
在上一个问题中:
Getting "This method or property cannot be called on Null values" error
以下代码有问题:
client_group_details.Add(new ClientGroupDetails(
reader.GetString(Col2Index),
reader.GetString(Col3Index)));
我在哪里得到以下错误:
Data is Null. This method or property cannot be called on Null values.
使用以下代码解决了这个问题:
client_group_details.Add(new ClientGroupDetails(
reader.IsDbNull(Col2Index) ? null : reader.GetString(Col2Index),
reader.IsDbNull(Col3Index) ? null : reader.GetString(Col3Index)));
我现在对GetDateTime 和GetInt32 也有类似的问题,例如:
client_group_details.Add(new ClientGroupDetails(
reader.GetString(Col2Index),
reader.GetString(Col3Index),
reader.GetDateTime(Col4Index)));
我尝试使用以下方法解决此问题,但没有成功
client_group_details.Add(new ClientGroupDetails(
reader.IsDbNull(Col2Index) ? null : reader.GetString(Col2Index),
reader.IsDbNull(Col3Index) ? null : reader.GetString(Col3Index),
reader.IsDbNull(Col2Index) ? null : reader.GetDateTime(Col4Index)));
它给出了错误:
Compiler Error Message: CS0173: Type of conditional expression cannot be determined because there is no implicit conversion between '<null>' and 'System.DateTime'
搜索解决方案后,发现:Nullable type issue with ?: Conditional Operator。但是当我尝试使用该代码时,我不断收到) expected。
我该如何解决这个问题?
【问题讨论】:
-
计算你的括号。你的左括号比右括号多。
-
发布您尝试使用的导致
) expected错误的代码。这是一个简单的语法错误,应该很容易修复。 -
感谢 John Saunders 指出我完全没有抓住重点。答案已删除。
-
20 小时,仍然没有代码的迹象与括号的实际问题。结束也不是一个真正的问题。
标签: c# .net-3.5 c#-3.0 asmx asp.net-3.5