Reputation: 141
I need to return a NULL or Blank value for Apvl_Lvl_Id if the SQL result returns NULL as the integration this passes the Apvl_Lvl_Id value to can't handle a value if it doesn't match an existing value for that instance.
I can't figure out what I'm missing, hoping someone can offer some pointers. Hope the below code is enough for an example.
while (dr.Read())
{
if(dr.IsDBNull(Apvl_Lvl_Id))
{
int? Apvl_Lvl_IdNULL;
Apvl_Lvl_Id = Apvl_Lvl_IdNULL;
}
else
{
Apvl_Lvl_Id = dr.GetInt32(0);
}
}
Upvotes: 0
Views: 63
Reputation: 49280
Sorry, but that code makes little sense. Apparently, Apvl_Lvl_Id
is supposed to be the column ordinal - effectively the zero-based count of the column in a row - that you check for being DBNull. Then you are trying to assign the actual column's value to that ordinal? Apart from that, there is just no way you can assign null
to an int
. Introduce a separate variable, say, Apvl_Lvl_Value
, make it an Nullable<int>
(or int?
) and assign the the column's value as required (actual value or null
).
In "short", the code should more like this:
while (dr.Read())
{
int? Apvl_Lvl_Value;
// Check if the value of the column with the ordinal Apvl_Lvl_Id is DBNull
if(dr.IsDBNull(Apvl_Lvl_Id))
{
// If so, use .NET "null" for the rest of the logic.
Apvl_Lvl_Value = null;
}
else
{
// Use the actual columns (non-NULL) value.
Apvl_Lvl_Value = dr.GetInt32(Apvl_Lvl_Id);
}
// Do something with the column's value, stored in Apvl_Lvl_Value
}
Now, some things here are guesswork which are not really clear otherwise from your question, but generally, the above is a "pattern" by which one would use IsDBNull()
and
a Get*()
method together.
Upvotes: 2