1

I have a problem with my C# application and I don't know how to deal with that. It's an app for my studies and it's first time I am using a C# and Visual Studio.

So - I have main form Form1 with a MenuStrip where I can go to another forms (which are datatables from my dataset displayed in a DataGridView). I can work in connectionless mode (writing data to .xml file) and in connection mode (sync directly to my database). In main form I have a Switch_btn button where I can change a mode I am working in. It's using a class:

public static class Connection_mode
    {
        public static int connection = 0;
    }

It's just changing the value of one variable like that:

Connection_mode.connection = 1;

In another form (class) Function I have a method which updates a database:

public void MethodToExec()
        {
            this.ds.WriteXml("D:\\sth.xml");
            this.dataSet_baza.Tables["funkcja"].Merge(funkcjaTable);
            this.funkcjaTableAdapter.Update(dataSet_baza.funkcja);
        }

When I am in connectionless mode I have a local DataTable on which I am making changes. And in this form it's working and send all data from local table to the database - it's nice.

And it's my main question: I want to sync data with a database when I click a Switch_btn in main Form1. I can have all other forms closed but when I click Switch I want to exec the MethodToExec() from Function form.

I've tried sth like that in Switch_btn code:

var funkcja = new Function();
funkcja.MethodToExec();

But it's not updating the database.

Maybe there is better way to do that but I don't know how.

BenMorel
  • 34,448
  • 50
  • 182
  • 322
Bartush
  • 17
  • 5
  • You do not want to create a **new and empty** `Function` object and call its *MethodToExec* method (which you do with `var funkcja = new Function();`) - you want to use the existing `Function` object which contains all the data that should be updated. –  May 19 '14 at 22:14
  • have you tried doing it this way? http://stackoverflow.com/questions/1665533/communicate-between-two-windows-forms-in-c-sharp –  May 19 '14 at 22:52

1 Answers1

0

Do away with the Function form altogether. You never use forms in this way. Instead, create a service class that you can instantiate from your form (note that the dependencies are arbitrary as I am not familiar with your model):

public class Service()
{
  private DataSet ds;
  private DataSet dataSet_baza;
  private TableAdapter funkjaTableAdapter;

  public Service()
  {
    ds = new DataSet();
    dataSet_baza = new DataSet();
    funkjaTableAdapter = new TableAdapter();
  }

  public void WriteXML(DataTable funkcjaTable)
  {
    this.ds.WriteXml("D:\\sth.xml");
    this.dataSet_baza.Tables["funkcja"].Merge(funkcjaTable);
    this.funkcjaTableAdapter.Update(dataSet_baza.funkcja);
  }
}

Then within your Form1 Switch_btn method:

var svc = new Service();

try
{
   svc.WriteXML();
}
catch(Exception ex)
{
   //do something with the exception
}

Now, you can share this functionality across all of your classes, which can instantiate their own Service object. If you need the Service.WriteXML method to be available to all classes without needing to instantiate one, make the WriteXML method static:

public static void WriteXML() {...}

And call it directly without creating a new Service object.

field_b
  • 688
  • 5
  • 21
  • `Function` is actually one of my dataSet DataTables - wrong name, I know. I don't like when Visual Studio generate so much code for me and I don't know whats going on. So I should create Service class in every class which is my dataSet table? The only problem is that I cannot create TableAdapter in a way You provided. Even that I would work on it and I am very grateful for your help. – Bartush May 19 '14 at 23:20
  • I'm confused. You state: "In another form (class) Function I have a method which updates a database". In any case, you should move business functionality and persistence logic out of your presentation layer (forms), especially if your forms have to call each others methods. That's a 'code smell'. Good luck! – field_b May 19 '14 at 23:31
  • Ok, I have to precise my problem. I have a dataSet_baza connected to my project - it contain 5 tables. Function (funkcja) is one of them. In the main form I have a MenuStrip to open other forms. In form I can provide new data to my tables and save it to xml or update the database. If I close the form data is stored in xml. If I change the connection mode it should update the database - In every form I have a method looks like `MethodToExec` from 1st post, connected with a Sync button. When I close the form and click the Switch button I want to run the code of that method. – Bartush May 19 '14 at 23:43
  • OK, your problem is more clear. Still, encapsulation is one of the pillars of object oriented programming. At the same time, duplicated code is a code smell. You should try to maximize encapsulation and reuse, and minimize code smells. You should also NEVER mix persistence code with presentation code. – field_b May 20 '14 at 00:49
  • BUT, I am going to update my answer to include how you could pass in the Function table and persist it to XML. Please feel free to at least up vote :) – field_b May 20 '14 at 00:50