Note that there are some explanatory texts on larger screens.

plurals
  1. POClosing Database Connections in .NET
    text
    copied!<p>I've got the following bit of code (that another developer wrote) that I need to modify because the ExecuteProc procedure fails. (The reason why it fails isn't in the scope of this question. It's a long story) but in scope is that I'm not sure why a <code>NullReferenceException</code> would occur. It's in VB.NET (I'm not a VB.NET guy myself, so I'm not sure if this is something that is unique to VB.NET):</p> <pre><code>Imports System.Data Imports System.Data.SqlClient Imports System.Data.SqlTypes Imports System.Collections '(...) Public Overloads Function ExecuteProc( ByVal pvsProcName as String) As DataSet Dim oCommand As SqlCommand Dim oDataAdaptor As SqlDataAdapter = New SqlDataAdapter 'Other Stuff here Try oCommand = CommandGet(ConnectionGet) 'Do some more stuff that's irrelevant Catch oException As Exception Throw oException Finally ConnectionClose(oCommand.Connection) oCommand.Dispose() oDataAdaptor.Dispose() End Try End Function </code></pre> <p>I get the following warning:</p> <pre><code>Variable 'oCommand' is used before it has been assigned a value. A null reference exception could result at runtime.` </code></pre> <p><strong>Edit</strong>: I found the answer right before I posted the question. I posted it to keep it around (I hate putting ten minutes into something to dump it willy nilly).</p> <p>My subsequent question for VB.NET folks is this:</p> <p>What would be the difference in the following two intializations:</p> <pre><code>Dim oCommand As SqlCommand = Nothing </code></pre> <p>And:</p> <pre><code>Dim oCommand as New SqlComand </code></pre>
 

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