Passing a Connection string dynamically

Asked

Viewed 133 times

2

Well my problem would be trying 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 database depending on the item selected in Combobox. I tried to pass the string through the file and make the connection of the database receive as parameter the items of my Combobox, but the moment I click on the button to search appears SYSTEM.NULLREFERENCEEXEEPTION.

     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;}
       }

    }
}
  • Will not lack to fill the property Conexao when filling in the list plantas?

  • My slide, the Connected property was only being used to value a label, forget to take it there in the example

  • So I guess just put the ValueMember of ComboBox as "Bank" (where previously had "Connected").

  • I tried your tip, but the error remains the same

  • By doing the click on the button, what value has the SelectedItem of ComboBox? Is the null?

  • I did not set any value in Selecteditem at the click of the button, that would be the problem ?

Show 2 more comments

1 answer

2


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

The solution is to obtain the class instance Planta and get the value there.
In the 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 at the event button1_Click get the instance and take the property Banco:

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

I think that way it solves your problem!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.