I have a data reader that gets empty results but I don't get why?
My connection state is open, i tested my query in my DB, it returns values but there my reader.read()
just get skipped
OdbcDataReader Reader;
string mond = "select fir_mdnt from safir JOIN safirbpv ON firbpv_fir = fir_fir where firbpv_ibnr = '" + _cb_Compte.SelectedItem + "' group by fir_mdnt;";
OdbcCommand mondant = new OdbcCommand(mond, InfoConnexion);
InfoConnexion.Open();
Reader = mondant.ExecuteReader();
while (Reader.Read())
{
MessageBox.Show(Reader.ToString());
}
InfoConnexion.Close();
Please, brush up the routine and put a break point on using (OdbcDataReader reader = mondant.ExecuteReader()) {
The code cleared
//DONE: make SQL readable
//DONE: make SQL parametrized (ODBC doesn't support named parameters, but ?)
string sql =
@"select fir_mdnt
from safir join
safirbpv on firbpv_fir = fir_fir
where firbpv_ibnr = ?
group by fir_mdnt";
//DONE: wrap IDisposable into using
//DONE: do not share the connection but create a new one
using (OdbcConnection con = new OdbcConnection(connectionStringHere)) {
con.Open();
//DONE: wrap IDisposable into using
using(OdbcCommand mondant = new OdbcCommand(sql, con)) {
//TODO: check the actual parameter type and value
//TODO: you may want to add Trim: _cb_Compte.SelectedItem.ToString().Trim()
mondant.Parameters.Add(new OdbcParameter("", OdbcType.Text)
{Value = _cb_Compte.SelectedItem});
//DONE: wrap IDisposable into using
//TODO: put a break point here
using (OdbcDataReader reader = mondant.ExecuteReader()) {
while (reader.Read()) {
MessageBox.Show(Convert.ToString(reader[0]));
}
}
}
}
Then you have to debug
sql
, parameter (its value). reader.HasRows
value.