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

New Topic/Question
Reply



MultiQuote


|