Reputation: 101
I am new to using reflection in C#. Any help is much appreciated.
PropertyInfo.GetValue(obj, null) gives me a object value.
If the value of the column is null in the database, I get a Null exception:
System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. Microsoft.SqlServer.Dts.Pipeline.ColumnIsNullException: The column has a null value.
How to handle this situation? I should loop through all the columns and leave the columns which have a null value.
Upvotes: 7
Views: 18226
Reputation: 143
You can check for 'Null' directly as follows
if(propInfo.GetValue(this, null) != null) {
}
Upvotes: 7
Reputation: 7517
PropertyInfo.GetValue(obj, null)
is executing the property get method on the object obj
. The exception is being thrown in that get method. You need to look at the property get method you're invoking and determine when/why an exception is being thrown.
Upvotes: 2
Reputation: 141678
The getter of that property is throwing an excepting. It's trying to tell you that the property doesn't have a value.
You should be able to check PropertyName_IsNull
(Where PropertyName is the name of the property) to check if the property is null first. If it is null, handle appropriately, otherwise use the code you've already written.
From MSDN:
A <column>_IsNull property for each selected input column. This property is also read-only or read/write depending on the Usage Type specified for the column.
Upvotes: 3