Click here to Skip to main content
15,890,282 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have an oledb connection called myConnection declared inside a module. i use it in a form .i wish to know how to close the connection. should i close it in the module or form and how? please post the code.i am a beginner in vb.net and have recently learned about modules. i am open to all kinds of suggestion.thanks.

the code for the form is:


Imports System.Data<br />
Imports System.Data.OleDb<br />
<br />
<br />
Public Class Form1<br />
<br />
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click<br />
<br />
        Dim Str As String = "SELECT * FROM Login WHERE username='" & txtUsername.Text & "' AND password='" & txtPassword.Text & "'"<br />
        Dim cmd As OleDbCommand = New OleDbCommand(Str, DBconnection)<br />
        dr = cmd.ExecuteReader<br />
        If dr.HasRows Then<br />
            MsgBox("successfull")<br />
        Else : MsgBox("incorrect username or password")<br />
        End If<br />
<br />
    End Sub<br />
   <br />
End Class



the code for the module is:


Imports System.Data<br />
Imports System.Data.OleDb<br />
Module Module1<br />
<br />
<br />
    Friend conString As String<br />
    Public dr As OleDbDataReader<br />
<br />
<br />
    Public Function DBconnection() As OleDbConnection<br />
<br />
        Dim myConnection As New OleDb.OleDbConnection<br />
        conString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source = D:\vb programs\working copy for windows application 4\WindowsApplication4\student.mdb"<br />
        myConnection = New OleDb.OleDbConnection(conString)<br />
        myConnection.Open()<br />
        Return myConnection<br />
<br />
    End Function<br />
<br />
End Module<br />
Posted

Close it in a module. You can use this code:
First move this line of code out of the function you created

VB
Public myConnection As New OleDb.OleDbConnection
conString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source = D:\vb programs\working copy for windows application 4\WindowsApplication4\student.mdb"
myConnection = New OleDb.OleDbConnection(conString)


and your function will be re-coded like this:
VB
Public Function DBconnection() As OleDbConnection
myConnection.Open()
Return myConnection

End Function


VB
Public sub CloseOleDB()
 Try
   myConnection.close()
 Catch ex as Exception
   'Add you error message here
 End Try
End Sub


You can then call CloseOleDB() within the form to close your connection.

Cheers!
 
Share this answer
 
Comments
hlsc1983 30-Jun-13 8:50am    
OK i moved the lines out of the function but i get an error with this line..
myConnection = New OleDb.OleDbConnection(conString). the error is 'declaration expected'
SamFad 30-Jun-13 9:34am    
Recontruct your code like this...

Imports System.Data
Imports System.Data.OleDb

conString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source = D:\vb programs\working copy for windows application 4\WindowsApplication4\student.mdb"

Public myConnection As OleDbConnection= New OleDbConnection(conString)

Public Sub OpenOleDB()
Try
If myConnection.State = ConnectionState.Closed Then
myConnection.Open()
End If
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.Critical, "Database Connection Error!")
End Try
End Sub

Public Sub CloseOleDB()
myConnection.Close()
End Sub

Call OpenOleDB() whenever you want to open a connection and CloseOleDB() anytime you wish to close.

This should work perfectly.
hlsc1983 30-Jun-13 10:58am    
thanks...i combined the two statements into one single statement
'Dim myConnection As New OleDb.OleDbConnection' and 'myConnection = New OleDb.OleDbConnection(conString)' into

' Public myConnection As New OleDb.OleDbConnection(connString)'
i dont get any error in this case.

i do however have more doubts.
is sub a better way than function to open a connection?
Just use the connection variable in this way and it will work.

Dim connect as New Oledb.OledbConnection

connect.close()
or
connect.Dispose()

And you should place this code at the beginning if the connection was already
opened. After you closed the connection you reopen it to perform your action
after that you close it again.

Private Sub btnSubmit_Click.............btnSubmit.Click
connect.Dispose()
Try
...........
............
connect.Open()
............
..........
Catch ex As Exception
MsgBox(ex.Message)
End Try
connect.Dispose()
End Sub

Where btnSubmit is the name of the click button.
 
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