-3

I want to display my oracle sql table on datagridview using windows form on visual studio 2017 c#. I am using oracle.data.access.client and type

    private void button1_Click(object sender, EventArgs e)
    {
        string oradb = "Data Source=127.0.0.1;User Id=practice;Password=practice;";
        OracleConnection conn = new OracleConnection(oradb);  
        conn.Open();
        OracleCommand cmd = new OracleCommand();
        cmd.Connection = conn;
        cmd.CommandText = "select * from test_dummy";
        cmd.CommandType = CommandType.Text;
        OracleDataReader dr = cmd.ExecuteReader();
        dr.Read();
        dataGridView1.DataSource = dr;
        conn.Dispose();

    }

I don't get it.. How do i make this work? . >.<

Bùi Đức Khánh
  • 3,975
  • 6
  • 27
  • 43
  • 1
    What problem you are facing with this code? Did you try using DataAdapter and Dataset to load the data in the GridView? – Chetan Jun 07 '18 at 02:11

1 Answers1

1

DataSource of DataGridView in this case should be DataTable not DataReader so I think you should change your code like this

        OracleDataReader dr = cmd.ExecuteReader();

        DataTable dataTable = new DataTable();
        dataTable.Load(dr);
        dataGridView1.DataSource = dataTable;

If you change like this and debug it has data but still nothing to display, you should check your data column binding

Bùi Đức Khánh
  • 3,975
  • 6
  • 27
  • 43
  • I keep getting this error. System.BadImageFormatException: 'Could not load file or assembly 'Oracle.DataAccess, Version=4.112.2.0, Culture=neutral, PublicKeyToken=89b483f429c47342' or one of its dependencies. An attempt was made to load a program with an incorrect format.' –  Jun 07 '18 at 02:54
  • It is another problem. Do you check it https://stackoverflow.com/questions/35271141/asp-net-core-oracle-dataaccess-system-badimageformatexception-could-not-load-fi?rq=1 – Bùi Đức Khánh Jun 07 '18 at 02:59
  • hmm.. never mind.. got it to work.. it was odac for visual studio not installed. :p –  Jun 07 '18 at 03:14
  • what's the difference between cmd.ExecuteNonQuery(); and cmd.CommandType = CommandType.Text; both seems to be working for this code. which is more efficient? –  Jun 07 '18 at 03:47