Link to home
Start Free TrialLog in
Avatar of vdiaz761
vdiaz761

asked on

Function To Connect To Database ASP.NET VB

Hello Experts,

I have the following code:
Function connectToDB(ByVal strQuery As String) As OleDbDataReader
    Dim connDb As OleDbConnection
    Dim cmdParamQuery As OleDbCommand
    Dim dtrDataReader As OleDbDataReader
    Dim datasource As String = (Server.MapPath(System.Configuration.ConfigurationManager.AppSettings("conString"))).ToLower()
    connDb = New OleDbConnection("PROVIDER=Microsoft.Jet.OLEDB.4.0;DATA SOURCE=" + datasource)
    connDb.Open()
    cmdParamQuery = New OleDbCommand(strQuery, connDb) 'strQuery PASSED IN FUNCTION CALL
    dtrDataReader = cmdParamQuery.ExecuteReader()
    Return dtrDataReader
  End Function

  Sub displayData()
    Dim dtrNewsArticles As OleDbDataReader
    Dim strQuery As String = "SELECT * FROM tableName"
    dtrNewsArticles = connectToDB(strQuery)
    While dtrNewsArticles.Read()
      'Do Code
    End While
    'Close Database
  End Sub

My question is, how would I close the database after ther WHile Loop?

Thanks

Vic
Avatar of Elvio Lujan
Elvio Lujan
Flag of Argentina image

vdiaz761...
first i suggest you use an dataset in thi case...

and for your problem you'll need to pass the connection to the funciton too:

Function connectToDB(ByVal strQuery As String, ByVal connDb As OleDbConnection) As OleDbDataReader
    Dim cmdParamQuery As OleDbCommand
    Dim dtrDataReader As OleDbDataReader
    Dim datasource As String = (Server.MapPath(System.Configuration.ConfigurationManager.AppSettings("conString"))).ToLower()
    connDb = New OleDbConnection("PROVIDER=Microsoft.Jet.OLEDB.4.0;DATA SOURCE=" + datasource)
    connDb.Open()
    cmdParamQuery = New OleDbCommand(strQuery, connDb) 'strQuery PASSED IN FUNCTION CALL
    dtrDataReader = cmdParamQuery.ExecuteReader()
    Return dtrDataReader
  End Function

  Sub displayData()
    Dim connDb As OleDbConnection
    Dim dtrNewsArticles As OleDbDataReader
    Dim strQuery As String = "SELECT * FROM tableName"
    dtrNewsArticles = connectToDB(strQuery, connDb)
    While dtrNewsArticles.Read()
      'Do Code
    End While
    connDb.Close()
  End Sub
Avatar of vdiaz761
vdiaz761

ASKER

Lem,

Thanks for the quick reply. Two things however:
I was trying to avoid having to Dim the OleDbConnection and I get the following error with your suggestion:
Object reference not set to an instance of an object.

This error is in reference to connDb.Close()

Thanks

Vic
Lem,

Also, your suggestion to use a dataset... can you explain?

Vic
   Public Function DevolverDataset(ByVal myExecuteQuery As String) As DataSet
        Dim MyConnectionString As String = System.Configuration.ConfigurationSettings.AppSettings("conn_peic.ConnectionString")
        Dim myConnection As New System.Data.OleDb.OleDbConnection(MyConnectionString)
        myConnection.Open()
        Dim dataadapter_PBQ As New OleDb.OleDbDataAdapter(myExecuteQuery, myConnection)
        Dim dataset_PBQ As New DataSet
        dataadapter_PBQ.Fill(dataset_PBQ)
        myConnection.Close()
        Return dataset_PBQ
    End Function


This Function returns Dummy Dataset, that bounds with any VB.NET controls.
ASKER CERTIFIED SOLUTION
Avatar of Elvio Lujan
Elvio Lujan
Flag of Argentina image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
what do I need to import for DataSet (i.e Imports System.Data.OleDb)
System.Data
Great... Almost there. How do I now access the columns return from the query in my displayData subRoutine as I would have I used a datareader:
ie dataReader("columnName")

Thanks
   for each dr As DataRow in ds.Tables(0).Rows
        dr("columnName") 'with this you'll get the column value in the actual row
    next
Perfect! You da man! Thanks...
Lem...

Are you ready?
Isaiah Chapter 13
sure man... but still i have to live in this planet :)