Solved

ASP.NET global function

Posted on 2003-10-28
3
557 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
  • 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

For most people, the WrapPanel seems like a magic when they switch from WinForms to WPF. Most of us will think that the code that is used to write a control like that would be difficult. However, most of the work is done by the WPF engine, and the W…
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
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…
I designed this idea while studying technology in the classroom.  This is a semester long project.  Students are asked to take photographs on a specific topic which they find meaningful, it can be a place or situation such as travel or homelessness.…

930 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