Passing a Connection string dynamically

2

Well my problem would be to try to pass a connection string from a txt file. which is also used to bring the items from my ComboBox, the goal and bring a different bank depending on the item selected in the ComboBox. I tried to pass the string through the file and make the database connection receive as a parameter the items of my ComboBox, but the moment I click the button to search SYSTEM.NULLREFERENCEEXEEPTION appears.

     private void Form1_Load(object sender, EventArgs e)
    {
        dateInicial.Value = DateTime.Today.AddDays(-1);
        dateFinal.Value = DateTime.Today.AddDays(-1);
        textBox1.MaxLength = 20;

        comboBanco.Items.Clear();
        List<Planta> plantas = new List<Planta>();

        using (StreamReader arquivo = File.OpenText(@"C:\Conexoes\Estados.txt"))
        {
            string linha;
            while ((linha = arquivo.ReadLine()) != null)
            {
                var espaçoArquivo = linha.Split(':');

                var planta = new Planta();
                planta.Local = espaçoArquivo[0];
                planta.Banco = espaçoArquivo[1];


                plantas.Add(planta);
            }

        }

        foreach (Planta result in plantas)
        {
            comboBanco.Items.Add(result);
        }
        comboBanco.DisplayMember = "Local";

    }

    private void comboBanco_SelectedIndexChanged(object sender, EventArgs e)
    {
        comboBanco.SendToBack();
        FrmGrid formb = new FrmGrid();

        switch (((Planta)comboBanco.SelectedItem).Local)
        {
            case "CT":
                formb.lblLocal.Text = ((Planta)comboBanco.SelectedItem).Local;
                comboBanco.SelectedValue = ((Planta)comboBanco.SelectedItem).Banco;
                break;

            case "CU":
                formb.lblLocal.Text = ((Planta)comboBanco.SelectedItem).Local;
                break;

            case "AT":
                formb.lblLocal.Text = ((Planta)comboBanco.SelectedItem).Local;
                break;

            default:
                break;
        }
    }

    private void button1_Click(object sender, EventArgs e)
    {
        try
        {
            OdbcConnection conn;

            conn = new OdbcConnection(comboBanco.SelectedValue.ToString());

            MessageBox.Show(conn.State.ToString());

            conn.Open();

            MessageBox.Show(conn.State.ToString());

            DataSet ds = new DataSet();
            DataTable dt = new DataTable();
            OdbcDataAdapter ada = new OdbcDataAdapter();
            OdbcCommand cmd = new OdbcCommand();

            string sql = "   SELECT * FROM EMP WHERE ROWNUM <=50 ";

            cmd.CommandText = sql;

            cmd.Connection = conn;

            ada = new OdbcDataAdapter(cmd);
            ada.Fill(dt);

            MessageBox.Show(dt.Rows.Count.ToString());

            FrmGrid c = new FrmGrid();
            c.Show();
            c.grdRelatorio.DataSource = dt;
            c.grdRelatorio.Refresh();

        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.ToString());
        }
        class Planta
       {
         public string Local { get; set; }
         public string Banco {get; set;}
       }

    }
}
    
asked by anonymous 28.08.2018 / 13:55

1 answer

2

The problem is in SelectedValue , which is null when you click the button.

The solution is to get the instance of class Planta and get the value there.
No Form_Load can slightly optimize your code:

using (StreamReader arquivo = File.OpenText(@"C:\Conexoes\Estados.txt"))
{
    string linha;
    while ((linha = arquivo.ReadLine()) != null)
    {
        var espaçoArquivo = linha.Split(':');

        var planta = new Planta()
        {
            Local = espaçoArquivo[0],
            Banco = espaçoArquivo[1]
        };

        plantas.Add(planta);
        comboBanco.Items.Add(planta);
    }
}

comboBanco.DisplayMember = "Local";
comboBanco.ValueMember = "Banco";

Then, in event button1_Click it will fetch the instance and get the Banco property:

Planta planta = comboBanco.SelectedItem as Planta;
conn = new OdbcConnection(planta?.Banco);

I think this solves your problem!

    
28.08.2018 / 15:14