2012-02-22 4 views
3

" '可能System.DateTime' ' ' の間には暗黙的な変換と、" 取得しない: エラーメッセージ前の質問で

client_group_details.Add(new ClientGroupDetails(
    reader.GetString(Col2Index), 
    reader.GetString(Col3Index))); 

Getting "This method or property cannot be called on Null values" error

は、私は、次のコードに問題がありました

ここで次のエラーが発生しました:

Data is Null. This method or property cannot be called on Null values. 

この問題は、次のコードを使用して解決されました:例があるように私は今、GetDateTimeGetInt32と同様の問題を抱えている

client_group_details.Add(new ClientGroupDetails(
    reader.IsDbNull(Col2Index) ? null : reader.GetString(Col2Index), 
    reader.IsDbNull(Col3Index) ? null : reader.GetString(Col3Index))); 

を:

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を得続けます。

どうすればこの問題を解決できますか?

+1

あなたの括弧を数えます。あなたは右の敬けんよりも多くの敬虔を残しています。 –

+0

')expected'エラーの原因となった、使用しようとしたコードを投稿してください。これは、簡単に修正できる単純な構文エラーです。 – phoog

+0

ジョン・サンダースの指摘のおかげで、私はポイントを完全に逃しました。回答が削除されました。 – Almo

答えて

8

閉じ括弧がどこかにありません。

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))); 

はおそらく

client_group_details.Add(new ClientGroupDetails(
    reader.IsDbNull(Col2Index) ? null : reader.GetString(Col2Index), 
    reader.IsDbNull(Col3Index) ? null : reader.GetString(Col3Index), 
    reader.IsDbNull(Col2Index) ? (DateTime?)null : reader.GetDateTime(Col4Index))); 

または類似したものに変更する必要があります。あなたの正確なコードに応じて、誰かがどこに括弧をつけているかを教えてくれるでしょう。

7

DateTimestructであるため、値の型はnullにできません。参照型とNullable<>型のみがnullになります。 Nullable<DateTime>を使用する必要があります。これはDateTime?と書くこともできます。

DateTime? dt = null; 
dt = DateTime.Now; 
if (dt.HasValue) ... 
if (dt == null) ... 
DateTime x = dt.Value; 

dt = reader.IsDBNull(Col2Index) ? (DateTime?)null : reader.GetDateTime(Col4Index); 
+0

それは私がやってきたことですが、私は ')期待しています。 – oshirowanen

+1

'reader.IsDBNull(Col2Index)を使用しようとしていますか? (DateTime?)null:reader.GetDateTime(Col4Index) '。三項演算子は混乱しているので、「ヌル」の型を判別することはできません。 –

+0

どこかに構文エラーがあります。あなたは ''より ''( '')多いです。最後に ')'を追加してみてください。 –

関連する問題