Skip to main content
added 698 characters in body
Source Link
Sweeper
  • 291.8k
  • 23
  • 260
  • 438

Alternatively, don't pass the combo box at all:

 private void FillComboBox(ComboBox Box, SqlConnection con) { LogInpanel_ComboBox.Items.Clear(); using (con) { SqlCommand com = new SqlCommand("Select Id From UsersTable", con); con.Open(); using (SqlDataReader reader = com.ExecuteReader()) { while (reader.Read()) { LogInpanel_ComboBox.Items.Add(reader["Id"].ToString()); } reader.Close(); } } con.Close(); } 

Alternatively, don't pass the combo box at all:

 private void FillComboBox(ComboBox Box, SqlConnection con) { LogInpanel_ComboBox.Items.Clear(); using (con) { SqlCommand com = new SqlCommand("Select Id From UsersTable", con); con.Open(); using (SqlDataReader reader = com.ExecuteReader()) { while (reader.Read()) { LogInpanel_ComboBox.Items.Add(reader["Id"].ToString()); } reader.Close(); } } con.Close(); } 
Source Link
Sweeper
  • 291.8k
  • 23
  • 260
  • 438

In my opinion, FillComboBox should be in the form class as a private non-static method:

public partial class Form1 : Form { private void StartApp() { LobGamma.LogInPanel.FillComboBox(LogInpanel_ComboBox, LobGamma.Connection.ObtainConnection()); } private void FillComboBox(ComboBox Box, SqlConnection con) { Box.Items.Clear(); using (con) { SqlCommand com = new SqlCommand("Select Id From UsersTable", con); con.Open(); using (SqlDataReader reader = com.ExecuteReader()) { while (reader.Read()) { Box.Items.Add(reader["Id"].ToString()); } reader.Close(); } } con.Close(); } } 

This is because all FillComboBox does is figure out what contents should be in the combo box. That's related to the UI so why not put it in the form class? The form class is supposed to initialize UI components and UI related things, which is exactly what FillComboBox is doing.

Is it best to Inherit from IDisposable so that I can use the Class in a Using Statement?

You only need to implement IDisposable if there is something to be disposed. But since you already have a using statement inside FillComboBox, everything is disposed already!