none
Save date to local database from other class than the Form class (where I have the databinding) RRS feed

  • Question

  • I am new to databases why this question may seem obvious.

    Simply I am creating a test application, where I link a local database to a form and add a data source  (and link the data source to the form). All works fine!

    But lets say that I need to ADD new data from a different Class. How to I link to the database? 

    I have tried to call the "adapter" through this code (from the other class)...

    [Serializable()]
        public partial class Class1 : Form1
        {
            public void TestAdd()
            {
                this.firstTestTableAdapter.Fill(this.dBtestDataSet1.FirstTest);
            }
        }
    But it will not recognize the :
    firstTestTableAdapter
    What are I'm missing?

    this is my Form code. 

    namespace DBtest3
    {
        public partial class Form1 : Form
        {
            private Class1 _class1;
    
            public Form1()
            {
                InitializeComponent();
    
                _class1 = new Class1();
            }
    
            private void firstTestBindingNavigatorSaveItem_Click(object sender, EventArgs e)
            {
                this.Validate();
                this.firstTestBindingSource.EndEdit();
                this.tableAdapterManager.UpdateAll(this.dBtestDataSet1);
    
            }
    
            private void Form1_Load(object sender, EventArgs e)
            {
                // TODO: This line of code loads data into the 'dBtestDataSet1.FirstTest' table. You can move, or remove it, as needed.
                this.firstTestTableAdapter.Fill(this.dBtestDataSet1.FirstTest);
    
            }
        }
    }

    • Moved by CoolDadTx Wednesday, September 23, 2015 5:33 PM ADO related
    Wednesday, September 23, 2015 1:34 PM

Answers

  • In the original code (from which you copied) you had used the designer to drag and drop a Dataset onto your form. That triggered the generation of a table adapter that eventually would become a field on your form.  Hence you could reference the field directly in your form.

    For a new form you'll need to basically create an instance of the table adapter yourself. You can store that in a field if you want, depending upon usage.  You can refer to the InitializeComponent method of your original form to see how to create and initialize the table adapter.  Note however that replicating this code in multiple forms isn't the best solution so you should consider moving the logic to a separate data class that both forms can use as needed.  That is beyond the scope of the forum discussions though.

    Also note that you shouldn't mark the form as serializable because it isn't.

    Michael Taylor
    http://blogs.msmvps.com/p3net

    Wednesday, September 23, 2015 5:32 PM

All replies

  • But lets say that I need to ADD new data from a different Class. How to I link to the database? 

    I have tried to call the "adapter" through this code (from the other class)...

    http://www.codeproject.com/Articles/24625/Coding-an-N-Tier-Application-in-C-without-any-Wiza

    Wednesday, September 23, 2015 5:22 PM
  • In the original code (from which you copied) you had used the designer to drag and drop a Dataset onto your form. That triggered the generation of a table adapter that eventually would become a field on your form.  Hence you could reference the field directly in your form.

    For a new form you'll need to basically create an instance of the table adapter yourself. You can store that in a field if you want, depending upon usage.  You can refer to the InitializeComponent method of your original form to see how to create and initialize the table adapter.  Note however that replicating this code in multiple forms isn't the best solution so you should consider moving the logic to a separate data class that both forms can use as needed.  That is beyond the scope of the forum discussions though.

    Also note that you shouldn't mark the form as serializable because it isn't.

    Michael Taylor
    http://blogs.msmvps.com/p3net

    Wednesday, September 23, 2015 5:32 PM