0

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;
    }
North
  • 109
  • 2
  • 12
  • Have you tried "normal" join, i.e. specifying column names rather than the "fancy" natural join? – Ivan Stoev Mar 24 '16 at 19:12
  • @IvanStoev, you mean `FROM table1, table2 WHERE table1.column1 = table2.column1`? I will try, but if natural join worked when querying directly, why wouldn't it from an application? It's going to the same database – North Mar 24 '16 at 19:21
  • That's true, but when you don't know what's going on, you need to start eliminating every possible thing you can think of. Especially with Oracle - I guess you are using ODP.NET provider. – Ivan Stoev Mar 24 '16 at 19:34

0 Answers0