I need to create a method, which when the user logs into the system, when the initial menu is loaded, it loads a Static DataTable
, so that I can use DataTable
in future, to populate some ComboBox
, without have the need to make multiple connections to my database.
Then I created a static class:
public static class Paises
{
private static DataTable _tablePais;
public static DataTable Tablepais
{
get { return Paises._tablePais; }
set { Paises._tablePais = value; }
}
}
I also created a class to connect to my database:
class Consulta_pais
{
conexao_bd conexao = new conexao_bd();
public void consulta()
{
conexao.bd_string();
SqlConnection sqlconn = new SqlConnection(conexao.sqlconn);
try
{
conexao._sql = @"SELECT code,descricao FROM Paises";
SqlCommand cmd = new SqlCommand(conexao._sql, sqlconn);
sqlconn.Open();
Paises.Tablepais.Load(cmd.ExecuteReader());
}
catch
{
}
finally
{
sqlconn.Close();
}
}
}
And then in the load menu, I call my class to bring the information from the bank:
private void Menu_Inicial_Load(object sender, EventArgs e)
{
Consulta_pais load_pais = new Consulta_pais();
load_pais.consulta();
}
But when I use a combobox to populate, it goes blank, I'm populating the combobox like this:
private void carrega_cb_pais()
{
comboBox5.DataSource = Paises.Tablepais;
comboBox5.ValueMember = "code";
comboBox5.DisplayMember = "descricao";
}