Showing posts with label shift. Show all posts
Showing posts with label shift. Show all posts

Thursday, March 22, 2012

Object reference not set to an instance of an object.

Not sure what seems to be the problems, been at this code for awhile now and I am fairly new to the whole .NET shift.

<%@dotnet.itags.org. Import Namespace="System.Data" %>
<%@dotnet.itags.org. Import NameSpace="System.Data.OleDb" %>
<%
Dim myConnection As OleDbConnection
Dim myCommand As OleDbCommand
Dim myDataReader As OleDbDataReader

myConnection.ConnectionString = "FILE Name=C:\Inetpub\DataConnection\PwrConnDev.UDL"
myConnection.Open()
myCommand = New OleDbCommand( "Select * from tMemberProfilePhoneType", myConnection )
myDataReader = myCommand.ExecuteReader()
While myDataReader.Read()
Response.Write( myDataReader.Item( "Description" ) )
End While
myDataReader.Close()
myConnection.Close()
%>

Any help would be appriciate, also the error is on line: 8myConnection = new OleDbConnection()
You are trying to use your MyConnection object before you instanciate it.

Change:

Dim myConnection As OleDbConnection

to

Dim myConnection As new OleDbConnection

When you do: Dim myConnection As OleDbConnection, all you are doing is creating a pointer that will point to a block of memory, you haven't actually set aside any memory until you call a new statement.
myConnection = new OleDbConnection()

Beat me to it... :D
=) This error is one of those that once you bang your head against it once or twice, you mark it for a slow, silent and painful execution whenever it pops up. You loathe it that much...
myConnection = new OleDbConnection()

Worked like a charm! Thank you! :D
=) This error is one of those that once you bang your head against it once or twice, you mark it for a slow, silent and painful execution whenever it pops up. You loathe it that much...

One of the products of learning to program in a C/C++ world is a very forced (and painful) understanding of pointers...even if they don't like to call them that anymore.
They've done a pretty good job of eliminating the need to understand it in sharp. Actually, Ive never really had to refer to my pointer knowledge and I am doing some very complex stuff these days.

This actually has more to do with initializing a class definition, although the side effect in C++ would have been, admittedtly, a null pointer reference. =)