0

I have a student table that references a parent table, as a FK. But a student can be older than 18, in which case he's responsable for himself and the parent_id is set to null. I have to check if the id is null, but:

if (dtreader_resp.Read())
            {
                if(dtreader_resp.GetInt16("resp_id") != null)
                {
                    resp.Resp_id = dtreader_resp.GetInt16("resp_id");
                }
            }

Always returns true. Is there a way to check if that field is null?

1 Answer 1

1

You could use IsDBNull or use a nullable int:

var data = sqlReader["resp_id"] as int?;
if (data.HasValue)
{
    var actualValue = data.Value
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.