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