Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

ASP.NET global function

Posted on 2003-10-28
3
Medium Priority
?
565 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 500 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

Nothing ever in the clear!

This technical paper will help you implement VMware’s VM encryption as well as implement Veeam encryption which together will achieve the nothing ever in the clear goal. If a bad guy steals VMs, backups or traffic they get nothing.

Question has a verified solution.

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

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:…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
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…
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…

660 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