Quantcast
Channel: Windows Forms Data Controls and Databinding forum
Viewing all articles
Browse latest Browse all 2535

Class fields versus local variables in a master detail form

$
0
0

I implemented a Master/Detail Form in C# starting from an example I found on Microsoft Docs (unfortunately, I cannot provide a link here).

In the example, 2 BindingSources are used to bind 2 DataGridViews to their data sources. The BindingSources are fields in the Form class, while the SqlConnection, DataSet, SqlDataAdapters are local variables in a method that retrieves data from the database.

In my example, the user can also change the data in the database tables by modifying the data in the grids and then submitting the changes to the database through the click of a button. I'm calling Update on the corresponding SqlDataAdapter in the button's Click event handler. This works if the SqlDataAdapter and the Dataset are also fields in the form class.

Is there any reason why SqlConnections, DataSets, etc., shouldn't be declared as class fields? I can't think of any and I just want to know if I'm going the right way.

Thank you.


Viewing all articles
Browse latest Browse all 2535

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>