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

The line of code where the error occurs:

oclass1.prop_myDataSet.Tables.Add("mytable");

trys to reference the object prop_myDataSet but that object has never been created in Class1. You only define it to be a DataSet object but never create one. Before you can add a Table to the DataSet, you will need to create it with the "new" clause.

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

Cathi Gero, CPA
Prenia Software & Consulting Services
Microsoft C# / .NET MVP
Mere Mortals for .NET MVP
cgero@prenia.com
www.prenia.com
Weblog: blogs.prenia.com/cathi
Previous
Reply
Map
View

Click here to load this message in the networking platform