Static DataTable, how to load?

0

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";
    }
    
asked by anonymous 23.07.2016 / 01:03

2 answers

0

Next place the line that fills the combo after entering the value and display member

private void carrega_cb_pais()
    {

        comboBox5.ValueMember = "code";
        comboBox5.DisplayMember = "descricao";
        comboBox5.DataSource = Paises.Tablepais;
    }
    
23.07.2016 / 13:11
0

@Thomas Eric Pimentel was looking at your code and I believe that in the class you populate the Datatable _tablePais, you do not need to pass the class name in front of the object because they are in the same context. I have refacted with some modifications and it looks like this:

public static class Paises
{
    private static DataTable _tablePais;

    public static DataTable Tablepais
    {
        get { return _tablePais; }
        private set { _tablePais = value; }
    }
}

As you are only populating your DataTable within the Countries class, I left the set as private so as not to run the risk of this DataTable receiving value outside of the class that was declared, and also to use the encapsulation concepts.

I created an example of the connection class with the database. Here's the example:

public class conexao_bd
{
    private SqlConnection sqlconn;

    public SqlConnection bd_string(out string pstrMsg)
    {
        pstrMsg = default(String);

        try
        {
            // Conexão definida no no arquvio App.Config da aplicação
            sqlconn= new SqlConnection(ConfigurationManager.ConnectionStrings["NOME_DA_CONEXAO"].ConnectionString);

            if (sqlconn.State == ConnectionState.Closed)
                sqlconn.Open();
        }
        catch (Exception ex)
        {
            pstrMsg = string.Format("Erro no método 'bd_string'\nDetalhes: {0}", ex.Message);
        }

        return sqlconn;
    }
}

The query method I set up an example, I believe that this makes the code leaner:

public class Consulta_pais
{
    conexao_bd conexao = new conexao_bd();

    public void consulta()
    {
        string strMsg = default(String);

        try
        {
            using (SqlConnection sqlconn = conexao.bd_string(out strMsg))
            {
                string strComando = @"SELECT code, descricao FROM Paises";

                using (SqlCommand cmd = new SqlCommand(strComando, sqlconn))
                {
                    Paises.Tablepais.Load(cmd.ExecuteReader());
                }
            }
        }
        catch (Exception ex)
        {
            // Tratamento da exceção
        }
    }
}

I believe that Load from the start menu will not have problems loading the combobox, I would leave it the way you did.

private void Menu_Inicial_Load(object sender, EventArgs e)
{
    Consulta_pais load_pais = new Consulta_pais();
    load_pais.consulta();
}

And in the end, to load the combobox it would look like this:

private void carrega_cb_pais()
{
    comboBox5.ValueMember = "code";
    comboBox5.DisplayMember = "descricao";
    comboBox5.DataSource = Paises.Tablepais;
    comboBox5.Update();
    comboBox5.Refresh();
}
    
25.07.2016 / 01:56