1 Replies - 4926 Views - Last Post: 19 February 2007 - 04:47 PM

#1 Jayman  Icon User is offline

  • Student of Life
  • member icon

Reputation: 418
  • View blog
  • Posts: 9,532
  • Joined: 26-December 05

Application settings not working in custom class

Posted 18 February 2007 - 11:23 PM

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.
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:
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



Is This A Good Question/Topic? 0
  • +

Replies To: Application settings not working in custom class

#2 Jayman  Icon User is offline

  • Student of Life
  • member icon

Reputation: 418
  • View blog
  • Posts: 9,532
  • Joined: 26-December 05

Re: Application settings not working in custom class

Posted 19 February 2007 - 04:47 PM

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.
System.Web.HttpContext.Current.Application("ConnectionString")


With that little change it is accessible from any class you create for an ASP.NET project.
Was This Post Helpful? 1

Page 1 of 1