Solved

close db connections

Posted on 2014-09-16
2
156 Views
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 :

       dbCommand.Dispose()
        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 = ""
        Try
            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

0
Comment
Question by:metropia
2 Comments
 
LVL 12

Accepted Solution

by:
Ramkisan Jagtap earned 250 total points
Comment Utility
Add try.. catch... finally blocks
And dispose the connection in finally block
0
 
LVL 27

Assisted Solution

by:Ark
Ark earned 250 total points
Comment Utility
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

0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now