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 listplantas
?– João Martins
My slide, the Connected property was only being used to value a label, forget to take it there in the example
– Gabriel Miranda
So I guess just put the
ValueMember
ofComboBox
as "Bank" (where previously had "Connected").– João Martins
I tried your tip, but the error remains the same
– Gabriel Miranda
By doing the click on the button, what value has the
SelectedItem
ofComboBox
? Is thenull
?– João Martins
I did not set any value in Selecteditem at the click of the button, that would be the problem ?
– Gabriel Miranda
Let’s go continue this discussion in chat.
– João Martins