I have a constructor
that I feed through a query to BBDD through a method that, when called from different points of the application, will cause the result obtained from SELECT
no have the same columns in all cases.
To give you an idea, this is the method that returns the object I want to create:
private static SMOrder CreateSMOrderObjectFromDataRecord(IDataRecord record)
{
return new SMOrder(){
Society = (int)record["SOCIETY_ID"], //no siempre llega
Division = (int)record["DIVISION_ID"], //no siempre llega
Order = (string)record["ORDER"],
Lang = (string)record["LANG_ID"],
Description = (string)record["DESCRIPTION"] //no siempre llega
};
}
With the following code I can check if the value that arrives from BBDD is empty or not before assigning it to my variable, but it only seems to work with strings
:
string description = !String.IsNullOrEmpty((string)record["DESCRIPTION"].ToString()) ? (string)record["DESCRIPTION"] : null
I need to do the same for a whole value int
and thus take advantage of the call to the same method.
I have tried with the following possibilities:
Society = (int)record["SOCIETY_ID"] == null ? 0 : (int)record["SOCIETY_ID"],
Society = (int)record["SOCIETY_ID"] ? 0 : (int)record["SOCIETY_ID"],
Society = !String.IsNullOrEmpty(record["SOCIETY_ID"].ToString()) ? (int)record["SOCIETY_ID"] : 0,
But I always get false
since never an integer = null
.
How can I solve this without having to modify the SELECT
?
EDIT
The problem that I face is that record["SOCIETY_ID"]
(or other columns) does not exist within record
and when doing the current check, or the ToString()
peta and gives the following error:
< strong> Exception details : System.IndexOutOfRangeException: SOCIETY_ID.
I've tried the examples of this question and their answers but still giving the same error.