 <p>In your code last line </p> <pre><code>returnValue = (int)sqlComm.ExecuteScalar(); </code></pre> <p>is culprit, you are trying to parser the null to int. This is why your code is throwing exception. Before parsing to is you should check is object is null. You can use try and catch to prevent unwanted error. </p>