Loko
Loko

Reputation: 6679

Oracle database table in gridview

I want to get the result from a query in my oracle database and put it in a gridview. Now my problem is, I have no idea how to output it in the gridview. I am using the gridview from the toolbox and my oracle connection is working. I also have the right SELECT query and I can output that in a listbox. I just have no idea how to do this in a gridview. I looked for it and I came across this: How to populate gridview with mysql? Although this doesn't help me.

How can I output it in a gridview so that it looks exactly the same as a normal table in the oracle database?

What should I use and how?

This is my code:

public void read()
        {
            try
            {
                var conn = new OracleConnection("")
                conn.Open();
                OracleCommand cmd = new OracleCommand("select * from t1", conn);
                OracleDataReader reader = cmd.ExecuteReader();
                DataTable dataTable = new DataTable();
            while (reader.Read())
            {
                var column1 = reader["vermogen"];
                column = (column1.ToString());
                listBox1.Items.Add(column);
            }
            conn.Close();
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }

    }

Upvotes: 4

Views: 27888

Answers (3)

m.hammad
m.hammad

Reputation: 1

First establish connection in case, you didnt establish globally by using connection string. Then use oleDbcommand for the oracle sql command you want to execute. In my case, it is 'select * from table_name' which would show all data from table to datagrid. I wrote this code in a button to display data on data grid.

{         
     OleDbConnection conn = new OleDbConnection("");
     OleDbCommand cmd = new OleDbCommand("select * from table_name", conn);

        {
            conn.Open();
            OleDbDataReader reader = cmd.ExecuteReader();
            {
                DataTable dataTable = new DataTable();
                dataTable.Load(reader);
                dataGridView1.DataSource = dataTable;
            }
            conn.Close();
        }
}

Upvotes: 0

Hamidreza
Hamidreza

Reputation: 3128

You can use DataSet too:

   public void read()
   {
       try
       {
           OracleConnection conn = new OracleConnection("");
           OracleCommand cmd = new OracleCommand("select * from t1", conn);
           conn.Open();
           cmd.CommandType = CommandType.Text;
           DataSet ds = new DataSet();
           OracleDataAdapter da = new OracleDataAdapter();
           da.SelectCommand = cmd;
           da.Fill(ds);
           dataGridView1.DataSource = ds.Tables[0];

       }
       catch (Exception ex)
       {
           MessageBox.Show(ex.Message);
       }
   }
}

Upvotes: 0

Steve
Steve

Reputation: 216273

To bind a DataTable to a DataGridView your code need simply to be changed to

    public void read()
    {
        try
        {
            using(OracleConnection conn = new OracleConnection("....."))
            using(OracleCommand cmd = new OracleCommand("select * from t1", conn))
            {
                conn.Open();
                using(OracleDataReader reader = cmd.ExecuteReader())
                {
                     DataTable dataTable = new DataTable();
                     dataTable.Load(reader);
                     dataGridView1.DataSource = dataTable;
                }
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    }
 }

The OracleDataReader could be passed to the Load method of the DataTable and then the table is ready to be bound to the DataGridView DataSource property. I have also added some using statement to ensure proper disposing of the disposable objects employed. (In particular the OracleConnection is very expensive to not close in case of exceptions)

Upvotes: 10

Related Questions