Calling a class in VB

S

Schoo

I have developed several ASP.NET programs (with VB backend) recently and
have called VB classes from my ASP pages in VS.NET. Now I have my first
actual VB.NET app and I can't seem to get that to work. I have created a
class called OnCallDB/DBAccess/DBAccess, referenced it just like I do in my
ASP apps and changed my code in the calling form to look as follows:
----------------------------------------------------------------------------
-
Imports OnCallDB
Public Class Form1
Inherits System.Windows.Forms.Form

[Windows Form Designer Generated Code]
Public oDBAccess As OnCallDB.DBAccess
Private Sub Form1_Load(....
Dim ds As DataSet = oDBAccess.GetRecs("Select * from people")
Grid1.Datasource = ds.Tables(0)
Grid1.Refresh()
End Sub
End Class
----------------------------------------------------------------------------
-
The debugger stops on the "Dim ds As DataSet = oDBAccess..." line without
even trying to run it with the error message:

"An unhandled excpetion of type 'System.NullReferenceException' occurred in
OnCall.exe. ...Object reference not set to an instance of an object". It
appears that the debugger is not even getting to the data access code.

Normally I would interpret this to mean my ds object is not instantiated
properly, but if I replace that line with the following the code runs fine
(I also reference the MS Data Access App Block of course) :
----------------------------------------------------------------------------
-
Dim ds As DataSet = sqlHelper.ExecuteDataset(strConnect, CommandType.Text,
"select * from people")
----------------------------------------------------------------------------
-

The GetRecs Procedure in the oDBAccess class is Public and returns a
DataSet. Actually the code in the class was copied from a class I am using
in an ASP.NET project so I know the code in the class has been proven (it's
pretty simple anyway).

My question is: "Why is this not instantiating my object? What do I have
to do to get the declaration to recognize the data being returned from the
class as a dataset so it can assign it to the 'ds' variable?"

Scott
 
S

Scott M.

Public oDBAccess As OnCallDB.DBAccess

....should be...

Public oDBAccess As New OnCallDB.DBAccess

Since you are not making an instance of the OnCall.DBAccess class, you get
the "Object reference not set to an instance of an object" exception.
 
S

Schoo

Uggg! I knew it was something simple... THANKS! :)


Scott M. said:
Public oDBAccess As OnCallDB.DBAccess

...should be...

Public oDBAccess As New OnCallDB.DBAccess

Since you are not making an instance of the OnCall.DBAccess class, you get
the "Object reference not set to an instance of an object" exception.

Schoo said:
I have developed several ASP.NET programs (with VB backend) recently and
have called VB classes from my ASP pages in VS.NET. Now I have my first
actual VB.NET app and I can't seem to get that to work. I have created a
class called OnCallDB/DBAccess/DBAccess, referenced it just like I do in
my
ASP apps and changed my code in the calling form to look as follows:
--------------------------------------------------------------------------
--
-
Imports OnCallDB
Public Class Form1
Inherits System.Windows.Forms.Form

[Windows Form Designer Generated Code]
Public oDBAccess As OnCallDB.DBAccess
Private Sub Form1_Load(....
Dim ds As DataSet = oDBAccess.GetRecs("Select * from people")
Grid1.Datasource = ds.Tables(0)
Grid1.Refresh()
End Sub
End Class
--------------------------------------------------------------------------
--
-
The debugger stops on the "Dim ds As DataSet = oDBAccess..." line without
even trying to run it with the error message:

"An unhandled excpetion of type 'System.NullReferenceException' occurred
in
OnCall.exe. ...Object reference not set to an instance of an object". It
appears that the debugger is not even getting to the data access code.

Normally I would interpret this to mean my ds object is not instantiated
properly, but if I replace that line with the following the code runs fi ne
(I also reference the MS Data Access App Block of course) :
--------------------------------------------------------------------------
--
-
Dim ds As DataSet = sqlHelper.ExecuteDataset(strConnect,
CommandType.Text,
"select * from people")
--------------------------------------------------------------------------
--
-

The GetRecs Procedure in the oDBAccess class is Public and returns a
DataSet. Actually the code in the class was copied from a class I am
using
in an ASP.NET project so I know the code in the class has been proven
(it's
pretty simple anyway).

My question is: "Why is this not instantiating my object? What do I have
to do to get the declaration to recognize the data being returned from the
class as a dataset so it can assign it to the 'ds' variable?"

Scott
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top