0

I am new to C# and I am using windows forms.

I have about 50 user controls in my C# project and each user control reads data from local SQL server. The SQL query is located in the user control's constructor as shown in the following code:

 public partial class BurgersUC : UserControl
{
    SqlConnection MyConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["MyConnectionString"].ConnectionString);
    SqlCommand MyCommand = new SqlCommand();
    DataTable DataTable = new DataTable();
    SqlDataAdapter Sql_Data_Adapter = new SqlDataAdapter();



 public BurgersUC()
     {

        InitializeComponent();

        try
        {


            DataTable.Rows.Clear();
            DataTable.Columns.Clear();

            MyConnection.Open();
            MyCommand.CommandText = "SELECT * FROM BurgerTable";
            MyCommand.Connection = MyConnection;
            Sql_Data_Adapter.SelectCommand = MyCommand;
            Sql_Data_Adapter.Fill(DataTable);

             button1.Text = Convert.ToString(DataTable.Rows[0]["Burger_Type"]);

             .

             .

             .


             button30.Text = Convert.ToString(DataTable.Rows[29]["Burger_Type"]);


           // DataTable = null;  

            MyCommand.Parameters.Clear();
            Sql_Data_Adapter.Dispose();
            MyConnection.Close();



        }

        catch (System.Exception excep)
        {

            MessageBox.Show(excep.Message);

        }


  }


}

My question is:

in terms of saving some memory do I have to set the DataTable to NULL after using it? Thank you

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
Kate
  • 935
  • 4
  • 14
  • 34
  • 2
    https://stackoverflow.com/questions/2785/setting-objects-to-null-nothing-after-use-in-net?rq=1 – Matteo Umili Aug 22 '16 at 14:30
  • 1
    You should actually call `Dispose()` on them – Matias Cicero Aug 22 '16 at 14:32
  • 1
    On a side note if you're interested in saving memory, do you really need to select * from BurgerTable? – Parrish Husband Aug 22 '16 at 14:35
  • @Parrish Husband yes I do. list of all burgers name is loaded from the SQL table for a sake of flexibility... – Kate Aug 22 '16 at 14:37
  • @Kate in your example you're only using Burger_Type, so I'm wondering if 'SELECT Burger_Type FROM BurgerTable' would suit your needs better. – Parrish Husband Aug 22 '16 at 14:39
  • @Parrish Husband . in your example I would still select only the "Burger_Type" column and I still would use DataTable, Am I correct? – Kate Aug 22 '16 at 14:44
  • @Kate, yes DataTable/DataSet will still work with a data adapter, even if you only retrieve a single column. The main point here is SELECT * can be expensive depending on the database design, so picking the columns you want specifically can help performance. – Parrish Husband Aug 22 '16 at 14:49
  • @Parrish Husband . yes correct I agree. Last thing: do I have to dispose the DataTable if the form will be disposed anyway? – Kate Aug 22 '16 at 14:53
  • @Kate, check out the linked answer in the first comment. I would make sure that your control finalizer properly disposes your DataAdapter and SqlConnection. – Parrish Husband Aug 22 '16 at 15:41

0 Answers0