Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Creating object references
Message
From
09/07/2002 10:37:39
Jerry Tovar
Dana Corporation Dana It
Maumee, Ohio, United States
 
 
To
All
General information
Forum:
ASP.NET
Category:
Other
Title:
Creating object references
Miscellaneous
Thread ID:
00676644
Message ID:
00676644
Views:
57
Using C#.

I am trying to create 2 classes. In class1, I am trying to define my OleDb connection vars/fields.

In class2, I am trying to use these fields from class1 to query the datasource. However, when I run the project, I get the following error: "Object reference not set to an instance of an object"

Here is what my C# code looks like: (Pseudo Code)

///////////////////////////////////////////////////////////
public class class1
///////////////////////////////////////////////////////////
//Fields:
public string prop_strAccessConn;
public string prop_strAccessSelect;
public DataSet prop_myDataSet;
public string prop_ctable;
public string prop_myAccessConn;
public string prop_myAccessCommand;
public string prop_myDataAdapter;
public class1() //Default constructor:
prop_strAccessConn =
"Provider=VFPOLEDB.1;Data Source=C:\\APPS\\MYDBC.DBC"
prop_strAccessSelect = "SELECT * FROM mytable";

OleDbConnection prop_myAccessConn = new OleDbConnection
(prop_strAccessConn);
OleDbCommand prop_myAccessCommand = new OleDbCommand
(prop_strAccessSelect,prop_myAccessConn);
OleDbDataAdapter prop_myDataAdapter = new OleDbDataAdapter
(prop_myAccessCommand);

public static void Main ()

///Call class2 for processing.
class2.processit();


///////////////////////////////////////////////////////////
public class class2
///////////////////////////////////////////////////////////
public static void processit ()

Console.WriteLine("Write output to the console.");
class1 oclass1 = new class1(); //Create object reference.
oclass1.prop_myDataSet.Tables.Add("mytable"); //I get error here.


I can't get the above example to work. However the following example does work but I would like to separate and make the OleDb vars accessible to other classes without redefining the vars.

///Working example. Pseudo code.
//////////////////////////////
public class class1
//////////////////////////////
public static void Main ()
string strAccessConn = "Provider=VFPOLEDB.1;Data Source=C:\\APPS\\CTRAK6\\CTTRAK.DBC"

string strAccessSelect = "SELECT * FROM mytable";

// Create the dataset and add the ctghead table to it:
DataSet myDataSet = new DataSet();
myDataSet.Tables.Add("mytable");

// Create Access objects:
OleDbConnection myAccessConn = new OleDbConnection(strAccessConn);
OleDbCommand myAccessCommand = new OleDbCommand(strAccessSelect,myAccessConn);
OleDbDataAdapter myDataAdapter = new OleDbDataAdapter(myAccessCommand);

myAccessConn.Open();

etc... everything works!


What am I doing wrong with regards to defining OleDb information in class1 and accessing those objects in another class to query a datasource?

Thanks,

Jerryt
Next
Reply
Map
View

Click here to load this message in the networking platform