Solved

close db connections

Posted on 2014-09-16
2
169 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
[X]
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
2 Comments
 
LVL 12

Accepted Solution

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

Assisted Solution

by:Ark
Ark earned 250 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

0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

733 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