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