.NET and Closing SQL Server connections
- by user307076
I am having a hard time figuring out why the following constructor will not close connnections. When I view the active connections. Here is the code that I have.
Public Sub New(ByVal UserID As Integer)
Dim oConn As New SqlConnection(ConfigurationManager.ConnectionStrings("connStr").ToString())
Dim cmd As New SqlCommand("stored proc", oConn)
Dim sdr As SqlDataReader
cmd.CommandType = CommandType.StoredProcedure
cmd.Parameters.AddWithValue("@userID", UserID)
oConn.Open()
sdr = cmd.ExecuteReader()
Try
If Not sdr.HasRows Then
sdr.Close()
If Not oConn Is Nothing Then
If oConn.State <> ConnectionState.Closed Then
oConn.Close()
End If
End If
cmd.Dispose()
Exit Sub
End If
'User has account in WATS, proceed to load account information
While sdr.Read
_firstname = Convert.ToString(sdr("First Name"))
_lastname = Convert.ToString(sdr("Last Name"))
End While
Catch ex As Exception
'Throw New Exception("User Error: " + ex.Message)
Finally
sdr.Close()
If Not oConn Is Nothing Then
If oConn.State <> ConnectionState.Closed Then
oConn.Close()
End If
End If
cmd.Dispose()
End Try
End Sub