Solved

ASP.NET global function

Posted on 2003-10-28
3
559 Views
Last Modified: 2013-11-25
I have a asp.net project that I am developing in Visual Studio .NET.  I want to connect to the database, but I do not want to have to create a db connection on every page, everytime I want to get a recordset.  I would like to create a function that can be accessed from any page within the project and will return the recordset from the DB.  Also, I would not like to do this in a include file.  Any help would be greatly appreciated.  I wrote this db connection function to do the job, just don't know what do with it so that I can access it from all pages...

Public Shared Function getDataReader(ByVal sqlqry As String) As OleDbDataReader
            Dim Connstring As String = "blah"
            Dim conn As New OleDbConnection(Connstring)
            Dim objcmd As New OleDbCommand(sqlqry, conn)
            Dim dtr As OleDbDataReader

            Try
                conn.Open()
                With objcmd
                    .CommandType = CommandType.Text
                End With
                dtr = objcmd.ExecuteReader()
                Return dtr
            Catch ex As Exception

                'exception handling code to go here
            Finally
                dtr = Nothing
                objcmd = Nothing
            End Try
        End Function
0
Comment
Question by:CodeDeamon
[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
3 Comments
 
LVL 3

Accepted Solution

by:
the-edge earned 125 total points
ID: 9637975
put it in the global.asax file and call it with this:

Global.getDataReader("SELECT * FROM blah")


the edge
0
 

Author Comment

by:CodeDeamon
ID: 9638052
It throws this error:

Name 'Global' is not declared.
0
 
LVL 3

Expert Comment

by:the-edge
ID: 9639968
it's strange...
add a module to your project and put the function there without the shared keyword:

Module Module1
    Public Function getDataReader(ByVal sqlqry As String) As OleDb.OleDbDataReader
        Dim Connstring As String = "blah"
        Dim conn As New OleDb.OleDbConnection(Connstring)
        Dim objcmd As New OleDb.OleDbCommand(sqlqry, conn)
        Dim dtr As OleDb.OleDbDataReader

        Try
            conn.Open()
            With objcmd
                .CommandType = CommandType.Text
            End With
            dtr = objcmd.ExecuteReader()
            Return dtr
        Catch ex As Exception

            'exception handling code to go here
        Finally
            dtr = Nothing
            objcmd = Nothing
        End Try
    End Function
End Module

call the function just writing it's name:

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'Put user code to initialize the page here
        getDataReader("SELECT * FROM blah")
    End Sub
0

Featured Post

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
VB.NET 2008 - Windows Protected Your PC 7 52
Object Oriented Programming, C#, referencing, scoping. 13 85
Help with opening combobox using VB.NET. 3 34
abstract class C# 1 36
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

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