Exforsys.com
 
Home Tutorials ODP.NET
 

ODP.NET - Retrieving Multiple Rows on to the Grid

 

ODP.NET - Retrieving Multiple Rows on to the Grid

Page 1 of 2

ODP.NET - Retrieving Multiple Rows on to the Grid

In the previous section, we tried to retrieve only one row using OracleDataReader. In this section, we will try to retrieve more than one row (or a result set) and populate a DataGridView on a WinForm.



The following code lists out the details of all employees available in the emp table:


Sample Code
  1. Imports Oracle.DataAccess.Client
  2.  
  3. Public Class Form2
  4.   Private Sub btnGetEmployees_Click(ByVal sender As 
  5.     System.ObjectByVal e As System.EventArgs) Handles 
  6.     btnGetEmployees.Click
  7.     'create connection to db
  8.     Dim cn As New OracleConnection("Data Source=xe;
  9.                           User Id=scott;Password=tiger")
  10.     Try
  11.       Dim SQL As String
  12.       'build the SELECT statement
  13.       SQL = String.Format("SELECT empno, ename, job, 
  14.             mgr, hiredate, sal, comm, deptno FROM emp")
  15.       'create command object to work with SELECT
  16.       Dim cmd As New OracleCommand(SQL, cn)
  17.       'open the connection
  18.       cmd.Connection.Open()
  19.       'get the DataReader object from command object
  20.       Dim rdr As OracleDataReader = _
  21.       cmd.ExecuteReader(CommandBehavior.CloseConnection)
  22.       'check if it has any rows
  23.       If rdr.HasRows Then
  24.         With Me.DataGridView1
  25.           'remove existing rows from grid
  26.           .Rows.Clear()
  27.           'get the number of columns
  28.           Dim ColumnCount As Integer = rdr.FieldCount
  29.           'add columns to the grid
  30.           For i As Integer = 0 To ColumnCount ­ 1
  31.             .Columns.Add(rdr.GetName(i), rdr.GetName(i))
  32.           Next
  33.           .AutoSizeColumnsMode = 
  34.            DataGridViewAutoSizeColumnsMode.ColumnHeader
  35.           'loop through every row
  36.           While rdr.Read
  37.             'get all row values into an array
  38.             Dim objCells(ColumnCount ­ 1) As Object
  39.             rdr.GetValues(objCells)
  40.             'add array as a row to grid
  41.             .Rows.Add(objCells)
  42.           End While
  43.         End With
  44.       Else
  45.         'display message if no rows found
  46.         MessageBox.Show("Not found")
  47.         Me.DataGridView1.Rows.Clear()
  48.       End If
  49.       'clear up the resources
  50.       rdr.Close()
  51.     Catch ex As Exception
  52.       'display if any error occurs
  53.       MessageBox.Show("Error: " & ex.Message)
  54.       'close the connection if it is still open
  55.       If cn.State = ConnectionState.Open Then
  56.         cn.Close()
  57.       End If
  58.     End Try
  59.   End Sub
  60. End Class
Copyright exforsys.com


Except the highlighted section, the rest of the code is already explained as part of the previous section. You can observe that the SELECT statement now tries to retrieve all rows from emp as follows:


SQL = String.Format("SELECT empno, ename, job, mgr,
hiredate, sal, comm, deptno FROM emp")


Once the OracleDataReader is ready with rows, we need to start with clearing the rows already displayed in the DataGridView with the help of the following code:


With Me.DataGridView1
'remove existing rows from grid
.Rows.Clear()


Once the rows are cleared, the first issue is the header of the grid. The moment we add columns to the grid, the header row gets automatically populated (with the column names). Before adding columns to the header, we should know the number of columns being added (just for the loop iterations) with the FieldCount property of DataGridView. The following is the code fragment that finds the number of columns and adds the columns to DataGridView:


Dim ColumnCount As Integer = rdr.FieldCount
For i As Integer = 0 To ColumnCount - 1
.Columns.Add(rdr.GetName(i), rdr.GetName(i))
Next


All the columns get auto-sized based on the column header with the following statement:


.AutoSizeColumnsMode =
DataGridViewAutoSizeColumnsMode.ColumnHeader


Once the columns are added, we need to read every successive row from the OracleDataReader and add it to the DataGridview. To add all column values at a time, we make use of the GetValues() method of OracleDataReader to push all the values in to an array and finally add the array itself as a row to the DataGridView. The following code fragment accomplishes this.


Sample Code
  1. While rdr.Read
  2.   'get all row values into an array
  3.   Dim objCells(ColumnCount ­ 1) As Object
  4.   rdr.GetValues(objCells)
  5.   'add array as a row to grid
  6.   .Rows.Add(objCells)
  7. End While
Copyright exforsys.com


The output for this code would look similar to the following figure:



Next Page: Pulling Information Using Table Name


Read Next: ODP.NET - Retrieving Typed Data



 

 

Comments



Post Your Comment:

Members Please Login
Your Name:*
e-mail ID:(required for notification)*
Image Verification: 
 
 Subscribe    

Sponsored Links

 

Subscribe via RSS


Get Daily Updates via Subscribe to Exforsys Free Training via email


Get Latest Free Training Updates delivered directly to your Inbox...

Enter your email address:


 

Subscribe to Exforsys Free Training via RSS
 

 
Partners -  Privacy and Legal Policy -  Site News -  Contact   Sitemap  

Copyright © 2000 - 2009 exforsys.com. All Rights Reserved

Page copy protected against web site content infringement by Copyscape