d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > How Do I Call This Method And Its Property?
12Next
Add Reply New Topic New Poll
Member
Posts: 7,427
Joined: Aug 22 2008
Gold: 2,334.50
Oct 16 2012 01:58am
So I have this class (Add > New Item > Class)

Code
   class DataSetConfiguration
   {
       private const string CONNECTION_STRING = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=\\xtreme.mdb";
       private const string QUERY_STRING = "SELECT * FROM CUSTOMER";
       private const string DATATABLE_NAME = "Customer";

       public static DataSet CustomerDataSet
       {
           get
           {
               CustomerDataSetSchema dataSet = new CustomerDataSetSchema();

               OleDbConnection oleDbConnection = new OleDbConnection(CONNECTION_STRING);
               OleDbDataAdapter oleDbDataAdapter = new OleDbDataAdapter(QUERY_STRING, oleDbConnection);
               oleDbDataAdapter.Fill(dataSet, DATATABLE_NAME);

               return dataSet;
           }
       }
   }


Now I would like to go to my Form1.cs and call this class and use its property and show the All Records in a dataGridView
How would I do this?
Can't find any example online that somebody did this way.
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Oct 16 2012 04:30pm
since it's not an object, just reference it like so:

DataSetConfiguration.CustomerDataSet

depending on namespace, you might have to make this a public class
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Oct 16 2012 05:04pm
looking at this again, why is this even a property? properties should only maintain state, not perform database queries. this belongs in a method, not a property.

This post was edited by carteblanche on Oct 16 2012 05:04pm
Member
Posts: 7,427
Joined: Aug 22 2008
Gold: 2,334.50
Oct 16 2012 05:05pm
Quote (carteblanche @ Oct 16 2012 06:30pm)
since it's not an object, just reference it like so:

DataSetConfiguration.CustomerDataSet

depending on namespace, you might have to make this a public class


I placed DataSetConfiguration.CustomerDataSet in Form1.cs and this is not allowed.

I even made my class public "public class DataSetConfiguration"

This post was edited by JuicyFruitSweeT on Oct 16 2012 05:05pm
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Oct 16 2012 05:06pm
Quote (JuicyFruitSweeT @ Oct 16 2012 07:05pm)
I placed DataSetConfiguration.CustomerDataSet in Form1.cs and this is not allowed.

I even made my class public "public class DataSetConfiguration"


what do you mean "not allowed"? what is the compile time error?

what namespaces are they in?

what method are you invoking the property in?

This post was edited by carteblanche on Oct 16 2012 05:08pm
Member
Posts: 7,427
Joined: Aug 22 2008
Gold: 2,334.50
Oct 16 2012 06:03pm
Quote (carteblanche @ Oct 16 2012 07:06pm)
what do you mean "not allowed"? what is the compile time error?

what namespaces are they in?

what method are you invoking the property in?


Error said: "myprojectname.DataSetConfiguration.CustomerDataSet' is a property but is used like a type"
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Oct 16 2012 06:09pm
Quote (JuicyFruitSweeT @ Oct 16 2012 08:03pm)
Error said: "myprojectname.DataSetConfiguration.CustomerDataSet' is a property but is used like a type"


that's your problem.

1. put it inside a method/constructor
2. assign it to a variable

would help if you turned it into a method instead of a property. 1) so you dont confuse yourself and 2) so you dont accidentally call it multiple times
Member
Posts: 11,637
Joined: Feb 2 2004
Gold: 434.84
Oct 16 2012 06:18pm
Holy abuse of properties, batman.
Member
Posts: 7,427
Joined: Aug 22 2008
Gold: 2,334.50
Oct 16 2012 06:22pm
Quote (carteblanche @ Oct 16 2012 08:09pm)
that's your problem.

1. put it inside a method/constructor
2. assign it to a variable

would help if you turned it into a method instead of a property. 1) so you dont confuse yourself and 2) so you dont accidentally call it multiple times


Should I get a dataGridView and use this property I made to display Records?
Is that even possible?
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Oct 16 2012 06:31pm
Quote (JuicyFruitSweeT @ Oct 16 2012 08:22pm)
Should I get a dataGridView and use this property I made to display Records?
Is that even possible?


read a tutorial

http://www.dotnetperls.com/datagridview-tutorial
Go Back To Programming & Development Topic List
12Next
Add Reply New Topic New Poll