Solved

ASP.NET global function

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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Suggested Solutions

Recently while returning home from work my wife (another .NET developer) was murmuring something. On further poking she said that she has been assigned a task where she has to serialize and deserialize objects and she is afraid of serialization. Wha…
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…
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…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

707 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

16 Experts available now in Live!

Get 1:1 Help Now