From: Fred Chateau on
I can't get the following line of code to work. Any idea how to do this?

int? result = subCategoryRow["RevisionNumber"].Equals(DBNull.Value) ?
null : (int?) subCategoryRow["RevisionNumber"];

I get an invalid cast exception.

Regards,

Fred Chateau



From: Peter Duniho on
Fred Chateau wrote:
> I can't get the following line of code to work. Any idea how to do this?
>
> int? result = subCategoryRow["RevisionNumber"].Equals(DBNull.Value) ?
> null : (int?) subCategoryRow["RevisionNumber"];
>
> I get an invalid cast exception.

That's because (presumably�you failed to show any declarations, so we
can't know for sure) the type returned by
subCategoryRow["RevisionNumber"] isn't Nullable<int> nor int.

Depending on what the type actually is, you may have to cast to int
(i.e. unbox a boxed int), then to int? (converting the now-unboxed int
to int?), or you may have to parse the object to an int, and then cast
to int?.

Without a more complete code example, it's not possible to say for sure.

Pete
From: Fred Chateau on
"Peter Duniho" wrote ...

> That's because (presumably�you failed to show any declarations, so we can't know for sure) the type returned by
> subCategoryRow["RevisionNumber"] isn't Nullable<int> nor int.
>
> Depending on what the type actually is, you may have to cast to int (i.e. unbox a boxed int), then to int? (converting the
> now-unboxed int to int?), or you may have to parse the object to an int, and then cast to int?.

The database column is set to int, but in certain queries, a null is returned.

Originally, the statement had no test for null, or cast, and was expecting an integer to be returned. That caused a null reference
exception.

Now, after adding the test, an integer causes the invalid cast exception.

Regards,

Fred Chateau



From: Peter Duniho on
Fred Chateau wrote:
> "Peter Duniho" wrote ...
>
>> That's because (presumably�you failed to show any declarations, so we can't know for sure) the type returned by
>> subCategoryRow["RevisionNumber"] isn't Nullable<int> nor int.
>>
>> Depending on what the type actually is, you may have to cast to int (i.e. unbox a boxed int), then to int? (converting the
>> now-unboxed int to int?), or you may have to parse the object to an int, and then cast to int?.
>
> The database column is set to int, but in certain queries, a null is returned.

Don't confuse the database format with the C# type. They may be
convertible, but that doesn't mean they are the same.

> Originally, the statement had no test for null, or cast, and was expecting an integer to be returned. That caused a null reference
> exception.

You had no cast at all? So, the type of this �
subCategoryRow["RevisionNumber"] � is actually int (i.e. System.Int32)?

> Now, after adding the test, an integer causes the invalid cast exception.

Yes, you said that in the first message. But without a proper
concise-but-complete code example, it's not possible for anyone other
than you to know for sure what the actual types are.

At the very least, you could've posted the exact message from the
exception. Even that would have been more informative than what we have
to work with at the moment.

Pete
From: Jesse Houwing on
* Fred Chateau wrote, On 13-1-2010 23:30:
> I can't get the following line of code to work. Any idea how to do this?
>
> int? result = subCategoryRow["RevisionNumber"].Equals(DBNull.Value) ?
> null : (int?) subCategoryRow["RevisionNumber"];
>
> I get an invalid cast exception.

Try:
int? result = subCategoryRow.IsNull("RevisionNumber") ?
(int?)null : (int) subCategoryRow["RevisionNumber"];

You need to cast the null to Nullable type in order to use it in the ?:
construction in order for it to work.

--
Jesse Houwing
jesse.houwing at sogeti.nl