Click here to Skip to main content
15,796,738 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
This is making me absolutely NUTS!

Why, oh why is closing a connection hanging up my hosting server???

Quick background: I put in code to test every single step by writing out the step to a text file on the server. (All of the text-writing code works fine by the way.)

When I close the connection (as shown below) the site hangs up for about 60 seconds and eventually throws a "connection was reset" page.

Any idea what's going on here??

My code:

' start by verifying that this record is still available first
Dim conn As OleDbConnection = New OleDbConnection(sConnectionString)
Dim cmd As OleDbCommand = New OleDbCommand()
Dim sql As String = "SELECT [Type] FROM [Login] WHERE [UserID] = '" & sUsrID & "'"

	cmd = New OleDbCommand(sql, conn)
Catch ex As Exception
	If HandleError(ex, "sql: " & sql) Then Server.Transfer("~/errorhandler.aspx")
End Try

Dim sdr As OleDbDataReader = cmd.ExecuteReader(Data.CommandBehavior.SingleRow)
Dim bFound As Boolean = sdr.Read()
Dim iTyp As Integer
If bFound Then iTyp = sdr("Type")

conn.Close()   <--- THIS causes the site to hang up!

' The following code NEVER executes:
errRoutineName = "Register:btnSubmit_Click(2)"
Updated 13-Dec-13 9:37am

1 solution

Try wrapping your connection and command objects in Using blocks, see:[^]

Also, you should probably use ExecuteScalar instead of ExecuteReader, that way you don't have to use a DataReader object. If the ExecuteScalar returns null, then the user is not found, if it doesn't return null, just cast it to your type object.

When using the Using blocks, you don't have to worry about cleaning up the resources, the framework does it for you. I'm not sure why you are getting the issue, but taking out the Reader and using Using blocks will clean up your code.

Also, since I can preach in this box, do not use string concatenation with your queries. If I were to type "a'; DROP TABLE [Login]; --" in your user name box, I would delete your login table. Learn how to use parameters with your queries to make your site safe from SQL Injection attacks.
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900