Execute Rocket MV U2 Subroutine Asynchronously using C# (async\await) and U2 Toolkit for .NET

Share on Facebook0Share on Google+0Tweet about this on Twitter0Share on LinkedIn1Email this to someoneShare on Reddit0

Overview

“Asynchronous programming involves executing operations in the background so the main thread can continue its own operations. This way the main thread can keep the user interface responsive while the background thread is processing the task at hand. .NET framework 4.5 introduced the [C# (async/await)] and [VB.NET (Async\Await)] keywords to simplify asynchronous programming (Joshi, 2014).”

For example, if you pull huge amounts of data from a database synchronously, the entire UI will block and make it impossible for the user to do anything until all database operations finish. But if done asynchronously, the user is free to work while the data is loading in the background.

U2 Toolkit for .NET v2.2.0 implements ADO.NET’s asynchronous API such as

  • ExecuteNonQueryAsync()
  • ExecuteReaderAsync()
  • ExecuteScalarAsync()

These APIs allow users to execute U2 subroutines and return multi-value data as a U2Parameter output value. Users can also return multi-columns/multi-rows of data, such as the result set (DataSet/DataTable), from U2 Subroutines (UniVerse Only).

Users can use the U2Parameter class to convert multi-value output data to Json or Objects or DataTables.

Asynchronous features can be used in the following applications:

  • ASP.NET MVC
  • Web API and oData
  • Node.js\Edge.js (Server Side) and Angular.js (Client Side)
  • WPF, WinForm
  • ASP.NET Web Form
  • Mobile and Cloud Development

This article demonstrate the following walkthroughs:

  1. Execute U2 Simple Subroutine
  2. Execute U2 subroutine, return multi-value data as output, convert multi-value data to DataTable
  3. Execute U2 subroutine, return multi-value data as output, convert multi-value data to Objects
  4. Execute U2 subroutine, return multi-value data as output, convert multi-value data to JSON
  5. Execute U2 subroutine, return result set (DataSet/DataTable) [UniVerse Only]

Requirements

Rocket U2 Version
Rocket UniVerse 10.3 or later
Rocket UniData 7.1 or later
Rocket U2 Toolkit for .NET 2.2.0 or later

 

Microsoft Version
Microsoft Visual Studio 2013 Update 4

 

Execute U2 Simple Subroutine

1. Start Visual Studio 2013

Fig1

2. Create Windows Forms Application C# Project

Fig2

3. Design Form Using Project Design

Drag fivebutton controls, a TextBox control, and a DataGridView control into the Form Designer.

Fig3

4. Add Reference to U2.Data.Client.dll (v4.5)

Fig4

 

Fig5

 

Fig6

 


5. Create C# Class : Exec_Async_Subroutine

Fig7

6. Create Async Method


public static async Task < DataTable > CallSubroutine()
{
// see below GitHub link for full source code
}

7. Call Async Method

Create an Event Handler for the “Subroutine_Async” button control.


private async void button1_Click(object sender, EventArgs e)
{
try
{
this.textBox1.AppendText("Start : Subroutine_Async" +Environment.NewLine);
string lRetVal = await Exec_Async_Subroutine.CallSubroutine();
this.textBox1.AppendText("Output : " + Environment.NewLine);
this.textBox1.AppendText(lRetVal);
this.textBox1.AppendText(Environment.NewLine);
this.textBox1.AppendText("End : Subroutine_Async" + Environment.NewLine);
}
catch (Exception e3)
{
this.textBox1.AppendText("Error : Subroutine_Async:" + e3.Message + Environment.NewLine);
}
}

8. Run App and press  the “Subroutine_Async” button control

Fig8

“”

Execute U2 Subroutine, return multi-value data as output, convert multi-value data to DataTable

1. Create Simple U2 Subroutine

This subroutine returns multi-value data using the  ARG_OUTPUT argument

SUBROUTINE SELECT_SUBROUTINE(ARG_INPUT,ARG_OUTPUT)x = ARG_INPUT
ARG_OUTPUT = “100”:@VM:”101″:@VM:”102″:@VM:”103″:@FM:”Nancy”:@VM:”Andrew”:@VM:”Janet”:@VM:”Margaret”:@FM:”01/06/1991″:@VM:”06/07/1996″:@VM:”11/08/1999″:@VM:”12/10/2001″RETURN

2. Compile Simple U2 Subroutine

>BASIC BP SELECT_SUBROUTINE
Fig9

3. Catalog Simple U2 Subroutine

>CATALOG BP SELECT_SUBROUTINE
Fig10

4. Create C# Class : Exec_Async_Subroutine_DataTable Class

Fig11

5. Create Schema of multi-value data

DataTable lRetDT = new DataTable(“EmpTable”);
lRetDT.Columns.Add(“ID”, typeof(Int32));
lRetDT.Columns.Add(“Name”, typeof(string));
lRetDT.Columns.Add(“HireDate”, typeof(DateTime));

6. Create Async Method


public static async Task < DataTable > CallSubroutine()
{
// see below GitHub link for full source code
}

 

7. Call Async Method

Create an event handler for the “Subroutine_Async_DataTable” button control.


private async void button2_Click(object sender, EventArgs e)
{
try
{
this.textBox1.AppendText("Start : Subroutine_Async_DataTable" + Environment.NewLine);
DataTable lRetValDT = await Exec_Async_Subroutine_DataTable.CallSubroutine();
this.textBox1.AppendText("Output : " + Environment.NewLine);

System.IO.StringWriter writer = new System.IO.StringWriter();
lRetValDT.WriteXml(writer, true);

this.textBox1.AppendText(writer.ToString());
this.dataGridView1.DataSource = lRetValDT;
this.textBox1.AppendText(Environment.NewLine);
this.textBox1.AppendText(“End : Subroutine_Async_DataTable” + Environment.NewLine);
}
catch (Exception e3)
{
this.textBox1.AppendText(“Error : Subroutine_Async_DataTable:” + e3.Message + Environment.NewLine);
}
}

8. Run App and press the “Subroutine_Async_DataTable” button control

Fig12

“”

Execute U2 Subroutine, return multi-value data as output, convert multi-value data to Objects

1. Create C# Class : Exec_Async_Subroutine_DataTable Class

Fig13

2. Create Schema of multi-value data

public class Employee
{
public int ID { get; set; }
public string Name { get; set; }
public DateTime HireDate { get; set; }
}

3. Create Async Method


public static async Task < Employee > CallSubroutine()
{
// see below GitHub link for full source code
}

 

4. Call Async Method

Create Event Handler for Button Control “Subroutine_Async_Objects”.


private async void button3_Click(object sender, EventArgs e)
{
try
{
this.textBox1.AppendText("Start : Subroutine_Async_Objects" + Environment.NewLine);
List lRetEmpList = await Exec_Async_Subroutine_Objects.CallSubroutine();
this.textBox1.AppendText("Output : " + Environment.NewLine);
foreach (var item in lRetEmpList)
{
this.textBox1.AppendText(item.ID+"\t"+item.Name+"\t"+item.HireDate + Environment.NewLine);
}

this.dataGridView1.DataSource = lRetEmpList;
this.textBox1.AppendText(Environment.NewLine);
this.textBox1.AppendText(“End : Subroutine_Async_Objects” + Environment.NewLine);
}
catch (Exception e3)
{
this.textBox1.AppendText(“Error : Subroutine_Async_Objects:” + e3.Message + Environment.NewLine);
}

}

5. Run App and press  the “Subroutine_Async_Objects” button control

Fig14

“”

Execute U2 Subroutine, return multi-value data as output, convert multi-value data to JSON

1. Add Reference Json.NET

Fig18

2. Create C# Class : Exec_Async_Subroutine_Json Class

Fig19

3. Create Schema of multi-value data

public class Employee
{
public int ID { get; set; }
public string Name { get; set; }
public DateTime HireDate { get; set; }
}

4. Create Async Method


public static async Task < String > CallSubroutine()
{
// see below GitHub link for full source code
}

 

5. Call Async Method

Create an event handler for the “Subroutine_Async_Json” button control.

private async void button4_Click(object sender, EventArgs e)
{

try
{
this.textBox1.AppendText(“Start : Subroutine_Async_Json” + Environment.NewLine);
string lRetVal = await Exec_Async_Subroutine_Json.CallSubroutine();
this.textBox1.AppendText(“Output : ” + Environment.NewLine);
this.textBox1.AppendText(lRetVal);
var result = Newtonsoft.Json.JsonConvert.DeserializeObject<List>(lRetVal);
this.dataGridView1.DataSource = result;
this.textBox1.AppendText( Environment.NewLine);
this.textBox1.AppendText(“End : Subroutine_Async_Json” + Environment.NewLine);
}
catch (Exception e3)
{
this.textBox1.AppendText(“Error : Subroutine_Async_Json:” + e3.Message + Environment.NewLine);
}
}

6. Run App and press the “Subroutine_Async_Json” button control

Fig20

“”

Execute U2 Subroutine, return result set (DataSet/DataTable) [UniVerse Only]

1. Create a Simple U2 Subroutine

This subroutine returns multi-columns result set ( for Universe Only)

SUBROUTINE GETCUSTOMER
$INCLUDE UNIVERSE.INCLUDE ODBC.H
SELSTMT = “SELECT * FROM CUSTOMER”
ST = SQLExecDirect(@HSTMT, SELSTMT)
RETURN

2. Compile a Simple U2 Subroutine

>BASIC BP GETCUSTOMER
Fig21

3. Catalog Simple U2 Subroutine

>CATALOG BP GETCUSTOMER
Fig22

4. Create C# Class : Exec_Async_Subroutine_ SQLExecDirect Class

Fig23

5. Create Schema of multi-value data

public class Customer
{
public int CustomerId { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
}

6. Create Async Method


public static async Task &lt; List &gt; CallSubroutine()
{
// see below GitHub link for full source code
}

 

7. Call Async Method

Create an Event Handler for the “Subroutine_Async_ SQLExecDirect” button control.


private async void button5_Click(object sender, EventArgs e)
{
try
{
this.textBox1.AppendText("Start : Subroutine_Async_SQLExecDirect" + Environment.NewLine);
List lRetEmpList = await Exec_Async_Subroutine_SQLExecDirect.CallSubroutine();
this.textBox1.AppendText("Output : " + Environment.NewLine);
foreach (var item in lRetEmpList)
{
this.textBox1.AppendText(item.CustomerId + "\t" + item.FirstName + "\t" + item.LastName + Environment.NewLine);
}

this.dataGridView1.DataSource = lRetEmpList;
this.textBox1.AppendText(Environment.NewLine);
this.textBox1.AppendText(“End : Subroutine_Async_SQLExecDirect” + Environment.NewLine);
}
catch (Exception e3)
{
this.textBox1.AppendText(“Error : Subroutine_Async_SQLExecDirect:” + e3.Message + Environment.NewLine);
}
}

8. Run App and press the “Subroutine_Async_ SQLExecDirect” button control

Fig24

Conclusion

.NET Framework 4.5, ADO.NET 4.5, Entity Framework 6.1, and Visual Studio 2013 introduced support for asynchronous operations. U2 Toolkit for .NET v2.2.0 implemented the required ADO.NET Asynchronous API specifications. Using these asynchronous methods, along with the async/await pattern, you can query the U2 Database or execute U2 subroutines asynchronously. You can also save the changes to the database in asynchronous manner. Asynchronous U2 database calls will provide so much more responsiveness to our applications.

Reference

Asynchronous Programming
Using SqlDataReader’s new async methods in .Net 4.5 (Part 1)
Using SqlDataReader’s new async methods in .Net 4.5 (Part 2)
Asynchronous Programming with Async and Await (C# and Visual Basic)

Source Code Downloads

Execute U2 Subroutine Asynchronously

Other Sample Code on U2 Toolkit for .NET

Contact us to learn more about Rocket MultiValue Solutions.
The following two tabs change content below.
Rocket Software
Rocket Software is a leading global developer of software products that help corporations, government agencies and other organizations reach their technology and business goals. 1,200 Rocketeers on five continents are focused on building and delivering solutions for more than 10,000 customers and partners.
Rocket Software

Latest posts by Rocket Software (see all)

, , , , , ,

3 Responses to Execute Rocket MV U2 Subroutine Asynchronously using C# (async\await) and U2 Toolkit for .NET

  1. Matt May 14, 2015 at 3:09 pm #

    Thanks for putting together this tutorial! I was able to follow it until the “Execute U2 Subroutine, return result set (DataSet/DataTable)” section. For some reason, I’m getting the following error:

    Cannot implicitly convert type ‘System.Data.Common.DbDataReader’ to ‘U2.Data.Client.U2DataReader’. An explicit conversion exists (are you missing a cast?)

    On the line:
    U2DataReader dr = await command.ExecuteReaderAsync();

    In file:
    Exec_Async_Subroutine_SQLExecDirect.cs

    Am I missing something obvious? I even downloaded the source code from GitHub and compared it to mine. When I open the downloaded project, I am able to build and run it just fine.

    Windows 8.1, Visual Studio 2012 (and tried VS 2013 Community), connecting to UniVerse database version 11.2.4 on a remote Windows server.

    • Matt May 15, 2015 at 8:25 am #

      In case anyone else runs into the same error, here’s the fix.

      This line:
      U2DataReader dr = await command.ExecuteReaderAsync();

      Needs an explicit conversion, like this:
      U2DataReader dr = (U2DataReader)await command.ExecuteReaderAsync();

  2. Rajan Kumar May 15, 2015 at 3:05 pm #

    Thanks Matt for reading this blog and nice comment.
    Thank you for executing all sample code related with this blog.
    I have tried again the last sample (DataSet/DataTable), I found that I do not need to do an explicit conversion, like this:

    U2DataReader dr = (U2DataReader)await command.ExecuteReaderAsync();

    Could you please do the following:

    1. Remove type cast (U2DataReader)
    2. Run the Program
    3. When it fails, send me the Log file

    How to create Log File ?
    Please read this link :
    https://github.com/RocketSoftware/multivalue-lab/tree/master/U2/Demos/U2-Toolkit/Log%20File

    How/Where to send the Log file?

    Send an email to u2askus@rocketsoftware.com

    Regards,

    Rajan

Leave a Reply