cdx1

Hi, I need to get info from my database in a class (.cs) how would I reference it from that class Is there a keyword such as "This"

Thanks



Re: Visual C# Express Edition how do I reference a tableadapter from a class (.cs)

jrboddie

One way is to pass a reference to the class (or Form) that is hosting the tableAdapter to the class that needs the data through the constructor:

Class constructor

Form1 form;

public Class1(Form1 f)

{

form = f;

}

Then make the tableAdapter (and anything else you need) a public member of Form1.

Then you can create methods in your class that operate on the tableAdapter in the calling form like this:

Method in Class1

public void Fill()

{

form.table1TableAdapter.Fill(form.dataSet11.Table1);

}

You could access this method from within Form1 like this:

Using an instance of Class1 from Form1

Class1 c1 = new Class1(this);

c1.fill();





Re: Visual C# Express Edition how do I reference a tableadapter from a class (.cs)

cdx1

sorry but I'm sort of a newbie at databases. Is it possible to keep all my references to the database neatly tucked away in a class For example, in Form1 if I want to move the bindingsource I would write something like MyBindingSource.MoveNext(); Is this possible to do solely from a separate class that I created





Re: Visual C# Express Edition how do I reference a tableadapter from a class (.cs)

jrboddie

I hope this example is closer to what you are looking for.

In this example a class is created which interfaces to a database. The form interacts with the class through an exposed BindingSource property (bs) and methods.

The constructor of this class creates and hooks up a BindingSource bs to a table in the database. A method, Next(), demonstrates how the bindingSource can be manipulated in the class.

Here is the complete class (note that several 'using' statements will be required.):

Class for interfacing to a database

using System.Collections.Generic;

using System.Text;

using System.Data.Sql;

using System.Data.SqlClient;

using System.Windows.Forms;

namespace WindowsApplication1

{

public class Class1

{

//Expose BindingSource as a public member

public BindingSource bs;

//Constructor connects BindingSource to database

public Class1()

{

Database1DataSetTableAdapters.Table1TableAdapter ta;

Database1DataSet.Table1DataTable myTable;

ta = new WindowsApplication1.Database1DataSetTableAdapters.Table1TableAdapter();

myTable = ta.GetData();

bs = new BindingSource();

bs.DataSource = myTable;

}

//Example method of manipulating BindingSouce

public void Next()

{

bs.MoveNext();

}

}

}

And the following is an example of code in a form that uses this simple class. In the form constructor, an instance of the class is made and a dataGridView control is connected to the bindingSource. A buttonClickEvent handler demonstrates calling the method in the class.

Form code which interfaces to the class defined above

public partial class Form1 : Form

{

Class1 c1; //Declare a class variable

public Form1()

{

InitializeComponent();

c1 = new Class1(); //Create instance

//Connect to dataGridView on Form

dataGridView1.DataSource = c1.bs;

}

//Demonstrate use of class to manipulate BindingSource

private void button1_Click(object sender, EventArgs e)

{

c1.Next(); //Use method in the class

}

}

When the program is run, the dataGridView is filled with table data. The button, steps the selection in the dataGridView.





Re: Visual C# Express Edition how do I reference a tableadapter from a class (.cs)

cdx1

That works for me!! Thanks very much for your help!! I've spent HOURS trying to figure this out!! Thanks again!!