Snotra Tech Adonet Oracle Data Components Tutorial (part 5)

Go to the start page
Go to the previous page

Creating master-detail application

Usually ADO.NET programmers create master-detail relationship using DataSet component. Snotra Tech Adonet Oracle Data Components use their own mechanism to create master-detail application. You can set this kind of relationship directly in design-time using visual editor or programmatically in runtime.

Problem to solve

Suppose we have two tables in Oracle database with have master-detail relationship. We want to make .NET application that can get data from those two tables as well as edit, insert and delete data.

Creating Sample data tables

In our tutorial we will use two tables: DEPT and EMP:

CREATE TABLE DEPT (
  DEPTNO NUMBER(2) CONSTRAINT PK_DEPT PRIMARY KEY,
  DNAME VARCHAR2(14) ,
  LOC VARCHAR2(13)
)
/
CREATE TABLE EMP (
  EMPNO NUMBER(4) CONSTRAINT PK_EMP PRIMARY KEY,
  ENAME VARCHAR2(10),
  JOB VARCHAR2(9),
  HIREDATE DATE,
  DEPTNO NUMBER(2) CONSTRAINT FK_DEPTNO REFERENCES DEPT
)
/
INSERT INTO DEPT values (10,'ACCOUNTING','NEW YORK');
INSERT INTO DEPT VALUES (20,'RESEARCH','DALLAS');
INSERT INTO DEPT values (30,'SALES','CHICAGO');
INSERT INTO DEPT values (40,'OPERATIONS','BOSTON')
/

INSERT INTO EMP values (7369,'SMITH','CLERK',to_date('17-12-1980','dd-mm-yyyy'),20);
INSERT INTO EMP values (7499,'ALLEN','SALESMAN',to_date('20-2-1981','dd-mm-yyyy'),30);
INSERT INTO EMP values (7521,'WARD','SALESMAN',to_date('22-2-1981','dd-mm-yyyy'),30);
INSERT INTO EMP values (7566,'JONES','MANAGER',to_date('2-4-1981','dd-mm-yyyy'),20);
INSERT INTO EMP values (7654,'MARTIN','SALESMAN',to_date('28-9-1981','dd-mm-yyyy'),30);
INSERT INTO EMP values (7698,'BLAKE','MANAGER',to_date('1-5-1981','dd-mm-yyyy'),30);
INSERT INTO EMP values (7782,'CLARK','MANAGER',to_date('9-6-1981','dd-mm-yyyy'),10);
INSERT INTO EMP values (7788,'SCOTT','ANALYST',to_date('13-07-1987','dd-mm-yyyy'),20);
INSERT INTO EMP values (7839,'KING','PRESIDENT',to_date('17-11-1981','dd-mm-yyyy'),10);
INSERT INTO EMP values (7844,'TURNER','SALESMAN',to_date('8-9-1981','dd-mm-yyyy'),30);
INSERT INTO EMP values (7876,'ADAMS','CLERK',to_date('13-07-1987', 'dd-mm-yyyy'),20);
INSERT INTO EMP values (7900,'JAMES','CLERK',to_date('3-12-1981', 'dd-mm-yyyy'),30);
INSERT INTO EMP values (7902,'FORD','ANALYST',to_date('3-12-1981','dd-mm-yyyy'),20);
INSERT INTO EMP values (7934,'MILLER','CLERK',to_date('23-1-1982','dd-mm-yyyy'),10)
/
commit
/
To make it easy create a text file, for example master-det.txt, copy and paste the text above, save file and then execute following command:
sqlplus scott/tiger@ORCL @master-det.txt 
Then leave sqlplus by typing command "exit". Now we are ready to make application using Microsoft Visual Studio 2005.

Creating application

Create a Windows Application using Visual Studio Wizard, just like in chapter First Application.
Then drop to form STAdonetOracleConnection and rename it as stConnection and two STAdonetOraDataTable components and rename them as mTable and detTable correspondingly.
Also drop to form two DataGridViews and rename them as mGrid and detGrid. Also one can place splitter between them but it is optionally. After that you can see form as on figure below:
Master-detail form design

Then set ConnectionString property of stConnection to

 
Data source=orcl;User Id=scott;Password=tiger
Or use your own Oracle database name, username and password.

Select mTable and set following properties:

Then select detTable and set following properties: Tables DEPT and EMP have one to many relationship trough column DEPTNO. mTable is master table for detTable, so we have set Master property to mTable. Also we have set MasterFields and DetailFileds properties according their name in corresponding tables in Oracle database. If our table has complex primary key, for example DEPTNO, DNAME we can set MasterFileds to DEPTNO, DNAME and change DetailFields property correspondingly.

Pay your attention to SQL expression: where t.deptno = :deptno. :deptno is variable. It must be derlared for detTable. When user browse mTable, STAdonetOraDataTable component set appropriate :deptno value for detTable and refresh it so edit Variables property of detTable.

OracleParameter Collection Editor should be opened:
OracleParameter Collection Editor

Add a new variable using "Add" button as on figure below:
Editing variables using OracleParameter Collection Editor

Set ParameterName to deptno and OracleDBType to Int32 and push "OK". Great, now we should add a few lines on code, so open application code and add following text to form constructor after InitializeComponent();:

try
{
      stConnection.Open();
}
catch (Exception e)
{
      MessageBox.Show(e.Message, "Error");
}

mGrid.DataSource = mTable;
detGrid.DataSource = detTable;
mTable.Open();
detTable.Open();
Your application code should looks like:
Master-detail application's code

Compile and run application. The following window should appears
Master-detail application form

Editing data using master-detail application

Navigate mGrid, select another row and data in detGrid should be changed according deptno value.

Then try to add new record in detGrid, for example like here:
Master-detail application add new record

Press "Enter" to post changes to Oracle database. Following exception is raised:
Master-detail application exception

STAdonetOraDataTable catches database exceptions "on the fly" and post data automatically when you press "Enter" or change current row. If exception is raised STAdonetOraDataTable returns focus to row that accounts for exception. Type any number in EMPNO column and retry to press "Enter":
Master-detail application record is added

New row is added and posted to Oracle database. You can also modify or delete any row. If database exception is raised you will be informed at once.

Customizing columns collection of STAdonetOraDataTable

Suppose we want to show only three columns in detGrid. We can do it by editing columns collection of detTable. Edit Columns property of detTable, STDataColumn Collection editor should be open:
STDataColumn Collection Editor

Add three columns: EMPNO, ENAME, JOB:
STDataColumn Collection Editor add one columns

STDataColumn Collection Editor add three columns

Then compile and run application, you should see the following window:
Master-detail application form with special columns set

Go to the next page