title: What is the best practice for handling null int/char from a SQL Database?
body: <p>I have a database that hold's a user's optional profile. In the profile I have strings, char (for M or F) and ints.</p>
  
  <p>I ran into an issue where I try to put the sex of the user into the property of my Profile object, and the application crashes because it doesn't know how to handle a returned null value.</p>
  
  <p>I've tried casting the data to the appropriate type </p>
  
  <pre><code>char sex = (char)dt.Rows[0]["Sex"];
  </code></pre>
  
  <p>Which didn't fix my problem. I then tried changing the types to Nullable and Nullable and get conversion issues all the same. My current solution that I was able to find is the following:</p>
  
  <pre><code>object.sex = null;  
  if(dt.Rows[0]["Sex"] != DBNull.Value)
        object.sex = (char)dt.Rows[0]["Sex"];
  object.WorkExt = null;
  if(dt.Rows[0]["WorkExt"] != DBNull.Value)
        object.WorkExt = (int)dt.Rows[0]["WorkExt"];
  </code></pre>
  
  <p>Is there a simpler or better way to do this? Or am I pretty much on the right track?</p>