Solved

ASP.NET global function

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
After several hours of googling I could not gather any information on this topic. There are several ways of controlling the USB port connected to any storage device. The best example of that is by changing the registry value of "HKEY_LOCAL_MACHINE\S…
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…

739 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