Welcome to Dream.In.Code
Become an Expert!

Join 149,810 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 2,745 people online right now. Registration is fast and FREE... Join Now!




Application settings not working in custom class

 
Reply to this topicStart new topic

Application settings not working in custom class, ASP.NET 2.0

Jayman
18 Feb, 2007 - 10:23 PM
Post #1

Student of Life
Group Icon

Joined: 26 Dec, 2005
Posts: 7,306



Thanked: 66 times
Dream Kudos: 500
Expert In: Everything

My Contributions
Ok so here is the deal. I have defined an application connection string in my Global.asax file for this project I'm working on.
CODE

Application("ConnectionString") = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\PizzaAndAMovie.mdb"


Now I don't have any problem accessing the application setting from any of the Web forms. I can connect to the database and retrieve data from within each *.aspx.vb pages.

I created a class that is stored in the App_Code folder so that I could create some Pizza objects. I have that class set to Inherits System.Web.UI.Page so that the application settings are available to that class.

But the problem is that when I try to create an Connection object inside that class I get a Object reference not set to an instance of an object. error message.

It works on all the code-behind pages, any ideas why it doesn't work in a custom class??

Error Message:
QUOTE
Object reference not set to an instance of an object.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

Source Error:

Line 127:
Line 128: 'declare variables
Line 129: Dim DBConnection As New OleDbConnection(Application("ConnectionString"))
Line 130: Dim SQL As New OleDbCommand
Line 131: 'Add the new record to the Pizza table using an INSERT query


Source File: C:\Documents and Settings\jason fairow\Desktop\School Assignments\Intro to VB.NET SE320\Final Project\PizzaMovie\App_Code\Pizza.vb Line: 129


Pizza class:
CODE

Imports Microsoft.VisualBasic
Imports System.Data.OleDb


Public Class Pizza
    Inherits System.Web.UI.Page
    'declare public fields
    Private m_OrderID As Integer
    Private m_PizzaSize As String
    Private m_Pepperoni As Boolean
    Private m_Sausage As Boolean
    Private m_Ham As Boolean
    Private m_BlackOlives As Boolean
    Private m_GreenPeppers As Boolean
    Private m_Onions As Boolean
    Private m_SpecialInstructions As String
    Private m_Price As Double

    Public Sub New(ByVal id As Integer, ByVal size As String, ByVal pep As Boolean, ByVal sau As Boolean, _
        ByVal ham As Boolean, ByVal bla As Boolean, ByVal gre As Boolean, ByVal oni As Boolean, _
        ByVal spe As String, ByVal pri As Double)

        'assign values to variables
        Me.OrderID = id
        Me.PizzaSize = size
        Me.Pepperoni = pep
        Me.Sausage = sau
        Me.Ham = ham
        Me.BlackOlives = bla
        Me.GreenPeppers = gre
        Me.Onions = oni
        Me.SpecialInstructions = spe
        Me.Price = pri
    End Sub


    Public Sub InsertPizza()

        'declare variables
        Dim DBConnection As New OleDbConnection(Application("ConnectionString"))
        Dim SQL As New OleDbCommand
        'Add the new record to the Pizza table using an INSERT query
        DBConnection.Open()
        SQL.Connection = DBConnection
        SQL.CommandText = "INSERT INTO Pizzas " & _
            "(OrderId, PizzaSize, Pepperoni, Sausage, Ham, BlackOlives, GreenPeppers, " & _
            " Onions, SpecialInstructions, Price) " & _
            " Values (" & m_OrderID & ", '" & m_PizzaSize & "', " & _
            m_Pepperoni & ", " & m_Sausage & ", " & _
            m_Ham & ", " & m_BlackOlives & ", " & _
            m_GreenPeppers & ", " & m_Onions & ", '" & _
            m_SpecialInstructions & "', " & m_Price & ")"
        SQL.ExecuteNonQuery()
        DBConnection.Close()

    End Sub
End Class

User is online!Profile CardPM
+Quote Post

Jayman
RE: Application Settings Not Working In Custom Class
19 Feb, 2007 - 03:47 PM
Post #2

Student of Life
Group Icon

Joined: 26 Dec, 2005
Posts: 7,306



Thanked: 66 times
Dream Kudos: 500
Expert In: Everything

My Contributions
For those that are interested.

After a couple of hours and a large headache I finally figured out how to access the Application and Session settings from inside a Custom class.

Instead of trying to access it using only the Application("ConnectionString")

You need to reference it using the following code to gain access to the current settings.
CODE

System.Web.HttpContext.Current.Application("ConnectionString")


With that little change it is accessible from any class you create for an ASP.NET project.
User is online!Profile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/8/09 08:19AM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Help!

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month