close db connections

Posted on 2014-09-16
Medium Priority
Last Modified: 2014-10-16
I have a function that I need to add code to dispose the opened database connection.
I am unsure where in the function to add :

        db = Nothing
without breaking the code accidentally.

Could I get some help in adding these lines of code to the existent code below?

Thank you.

The code is:

    Public Shared Function SendAdminEmail() As Boolean
        Dim ipAddress As String = ""
            ipAddress = HttpContext.Current.Request.ServerVariables("REMOTE_ADDR")
        Catch ex As Exception
        End Try

        If (ipAddress > "") Then
            Dim db As Database = DatabaseFactory.CreateDatabase(Globals.AppSettings("webdb"))
            Dim dbCommand As DbCommand = db.GetStoredProcCommand("VerifySendAdminEmail")
            db.AddInParameter(dbCommand, "@IPAddress", DbType.String, ipAddress)
            Dim ds As DataSet = db.ExecuteDataSet(dbCommand)
            If (Not ds Is Nothing) Then
                If (ds.Tables.Count > 0) Then
                    If (ds.Tables(0).Rows.Count > 0) Then
                        If (ds.Tables(0).Rows(0)(0).ToString = "0") Then
                            Return False
                        End If
                    End If
                End If
            End If
        End If
        Return True
    End Function

Open in new window

Question by:metropia
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
LVL 12

Accepted Solution

Ramkisan Jagtap earned 1000 total points
ID: 40325854
Add try.. catch... finally blocks
And dispose the connection in finally block
LVL 28

Assisted Solution

Ark earned 1000 total points
ID: 40345376
Dim ds As DataSet = Nothing
Using dbCommand As DbCommand = db.GetStoredProcCommand("VerifySendAdminEmail")
    db.AddInParameter(dbCommand, "@IPAddress", DbType.String, ipAddress)
    ds = db.ExecuteDataSet(dbCommand)
End Using
If (Not ds Is Nothing) Then
End If

Open in new window


Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
Suggested Courses

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question