I know this has been asked a lot before, but in this case I think it has something to do with using JOIN
in the query, because I've run the exact same code with a simple SELECT
statement and its always returned a table, AND I've checked the JOIN
query in the sqlplus command line and there's always a result, so it's not a problem with the query itself
The method is:
public static DataView method()
{
DBConnection db = new DBConnection();
db.Connect();
db.SetSql("select column1, column2, column3 from table1 natural join table2");
DataSet result = db.RetrieveRecords();
DataView source = new DataView(result.Tables[0]);
db.Dispose();
return source;
}
I use the same method to populate a different datagrid in the application, the only difference is the query in db.SetSql
, which is select column1, column2 from table1
. That datagrid is always populated fine, and even if the query result is 0 rows, it just shows the grid empty and doesn't throw an error.
Like I said, the JOIN
query gives a result in the command line, but when I run my application, the "Cannot find table 0" error is thrown at the line DataView source = new DataView(result.Tables[0]);
Is it possibly something like when you use JOIN
the result is not considered a table?
The code for DBConnection.RetrieveRecords()
:
public DataSet RetrieveRecords()
{
OracleDataAdapter DataAdapter = null;
DataSet result = new DataSet();
try
{
command.Connection = connection; //command is OracleCommand and connection is OracleConnection
DataAdapter = new OracleDataAdapter(command);
DataAdapter.Fill(result);
}
catch (Exception )
{
autoDisconnect = true;
throw new Exception(ex.ToString());
}
//Cleaning
finally
{
if (command.Parameters.Count > 0)
{
foreach (OracleParameter pram in command.Parameters)
{
pram.Dispose();
}
}
if (command != null)
{
command.Dispose();
}
if (DataAdapter != null)
{
DataAdapter.Dispose();
}
if (autoDisconnect)
{
Dispose();
}
}
return result;
}
And DBConnection.SetSql()
(if it matters):
public void SetSql(string sql)
{
command = new OracleCommand(sql);
command.BindByName = true;
}