Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Nullable types are good, but only for types that are not nullable to begin with.</p> <p>To make a type "nullable" append a question mark to the type, for example: </p> <pre><code>int? value = 5; </code></pre> <p>I would also recommend using the "<code>as</code>" keyword instead of casting. You can only use the "as" keyword on nullable types, so make sure you're casting things that are already nullable (like strings) or you use nullable types as mentioned above. The reasoning for this is </p> <ol> <li>If a type is nullable, the "<code>as</code>" keyword returns <code>null</code> if a value is <code>DBNull</code>.</li> <li>It's <a href="http://www.codeproject.com/Articles/8052/Type-casting-impact-over-execution-performance-in" rel="nofollow noreferrer">ever-so-slightly faster than casting</a> though <a href="https://stackoverflow.com/a/496167/392">only in certain cases</a>. This on its own is never a good enough reason to use <code>as</code>, but coupled with the reason above it's useful.</li> </ol> <p>I'd recommend doing something like this</p> <pre><code>DataRow row = ds.Tables[0].Rows[0]; string value = row as string; </code></pre> <p>In the case above, if <code>row</code> comes back as <code>DBNull</code>, then <code>value</code> will become <code>null</code> instead of throwing an exception. Be aware that if your DB query changes the columns/types being returned, <strong>using <code>as</code> will cause your code to silently fail</strong> and make values simple <code>null</code> instead of throwing the appropriate exception when incorrect data is returned so it is recommended that you have tests in place to validate your queries in other ways to ensure data integrity as your codebase evolves.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload