calling a class and its methodss

G

Guest

I have a class with all the database stuff in there like path,add, delete, and
update. I also have a form that calls the class like so:
DatabaseManager manager = new DatabaseManager();
manager.path();
In the database class I made a Dataset like so:
DataSet ds = new DataSet();
da.Fill(ds);
How do I call the dataset from the database class
Here is the database code
OleDbConnection Conn = new OleDbConnection();
string sConnString = "Provider=Microsoft.Jet.OLEDB.4.0;Data
Source=F:\\\\Project Track It\\bin\\Project.mdb";

Conn.ConnectionString = sConnString;
Conn.Open();

string select = "select * from Project";
OleDbDataAdapter da = new OleDbDataAdapter (select, Conn);
DataSet ds = new DataSet();
da.Fill(ds);
Question is how do I call the dataset that I created in the database class
from the form. I s it by ref and if so can someone show me? Please
I have:
DatabaseManager manager = new DatabaseManager();
manager.path();
After that I have no clue
 
G

Guest

It looks like your dataset only has instance-level scope and isn't being
exposed outside of your class. You would need to do something something like
this:

public class DatabaseManager
{
private DataSet myData;
public DataSet MyData
{
get { return myData; }
}
// This is the method you showed code for
public void GetData()
{
// I'm omitting all of your code
// except for the line
// DataSet ds = new DataSet();
// which should be replaced with
myData = new DataSet;
}
}

Then you can access it by using something like

DatabaseManager manager = new DatabaseManager();
manager.GetData();
Console.Writeline( manager.MyData.ToString() ); // Should return
"System.Data.DataSet"

Alternatively, if you don't need that instance of DatabaseManager once you
have the DataSet, you can forego all of the stuff above and just have the
function that fills the DataSet return it.

public DataSet GetData()
{
// omitting connection-related code
DataSet ds = new DataSet();
da.Fill( ds );
return( ds );
}

Then, in your form,

DatabaseManager manager = new DatabaseManager();
DataSet ds = manager.GetData();

In either case, make sure you're not keeping your OleDbClient objects alive
longer than you need them (and you shouldn't need them after the DataSet is
filled).
 

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