Tuesday, February 19, 2013

How to create Visual Components in PowerBuilder for Visual Studio Developers

Another of the latest videos from the SAP Database and Tools Academy (http://www.youtube.com/user/saptechnology/videos?query=PowerBuilder).








The video is pretty much self explanatory.  Just reiterate the main points here and provide some of the sample code.

1.  Create a .Net assembly target and select CustomVisual as the object type to be created.
2.  Create your visual user object in the WPF panel that is presented.
3.  Go to the Objects tab on the project and make sure you select the visual component you want to deploy.
4.  In addition to the assembly containing your visual component, PowerBuilder will place the following runtime assemblies in the output folder.  Give all of them to the Visual Studio developer.

  1.  com.sybase.iiop.net.dll  
  2. Sybase.DataWindow.Common.dll  
  3. Sybase.DataWindow.Core.dll  
  4. Sybase.DataWindow.Interop.dll  
  5. Sybase.DataWindow.Shared.dll  
  6. Sybase.DataWindow.WPF.dll  
  7. Sybase.PowerBuilder.Common.dll  
  8. Sybase.PowerBuilder.Common.xml  
  9. Sybase.PowerBuilder.Compiler.Assist.dll  
  10. Sybase.PowerBuilder.Core.dll  
  11. Sybase.PowerBuilder.Core.xml  
  12. Sybase.PowerBuilder.DataSource.Db.dll  
  13. Sybase.PowerBuilder.DataSource.dll  
  14. Sybase.PowerBuilder.Interop.dll  
  15. Sybase.PowerBuilder.WPF.Controls.dll  
  16. Sybase.PowerBuilder.WPF.Controls.Skins.dll  
  17. Sybase.PowerBuilder.WPF.Controls.xml  
  18. Sybase.PowerBuilder.WPF.dll  
  19. Sybase.PowerBuilder.WPF.xml  

5.  The visual studio developer will create a reference in their Toolbar to the new WPF control by selecting Choose Items... and then using the Browse option under WPF control to navigate to your assembly and then select the visual control within it.
6.  The Visual Studio developer can now drop the PowerBuilder visual object onto a WPF window in their application.  There may be a couple of errors the first time they attempt to compile the application:
a.  If the WPF application has been created with a Target Framework Reference of ".Net Framework 4 Client Profile" they will need to change it to ".Net Framework 4".  The assembly that PowerBuilder created indicates it has a dependency on System.Web, which is not included in the Client Profile version of the target framework.
b.  When the Visual Studio developer added your component to their WPF window, Visual Studio automatically added a reference to it to the project.  The Visual Studio developer will also need to add an explicit reference to Sybase.PowerBuilder.WPF.Controls to the project.

If you are incorporating a DataWindow control in your custom visual user object, you will need to make a few additional changes.

1.  Assuming that you are going to be using ADO.Net to connect to the database, you will need to provide these additonal runtime assemblies to the Visual Studio developer.  Note that PowerBuilder will not automatically copy them to the output folder for you.

  1. Sybase.PowerBuilder.ADO.dll  
  2. Sybase.PowerBuilder.DataSource.Db2.dll  
  3. Sybase.PowerBuilder.DataSource.Sharing.dll  
  4. Sybase.PowerBuilder.Db.dll  

Also note that the following runtime assemblies will also need to be given to the Visual Studio developer, because the DataWIndow uses them to read SRD files:

  1. antlr.runtime.dll  
  2. Antlr3.Runtime.dll  

2.  The Visual Studio developer will need to add explicit references to the antlr3.runtime.dll and Sybase.PowerBuilder.DataSource.Sharing.dll assemblies.

3.  If you want to have the Visual Studio developer pass in an established connection to the database and have the DataWindow within the PowerBuilder component use it instead of establishing it's own seperate connection to the database, you'll need to add a method to the custom visual user object that takes an argument of System.Object and then uses the SetAdoConnection method on the transaction object to assign that argument as a proxy connection.  For example:

  1. boolean  lb_rc  
  2. integer  li_rc  
  3. try  
  4.   SQLCA.DBMS = "ADO.Net"  
  5.   SQLCA.DBParm = "Namespace='Oracle.DataAccess.Client'"  
  6.   lb_rc = sqlca.SetAdoConnection(a_conn)  
  7.   if not lb_rc then  
  8.     MessageBox ( "Error", "SetAdoConnection failed" )  
  9.     return -1  
  10.   else  
  11.     connect using sqlca ;  
  12.     if sqlca.SQLCode <> 0 then  
  13.       MessageBox ( "Error", "Connect failed: " + sqlca.SQLErrText )  
  14.       return -1  
  15.     else  
  16.       li_rc = dw_1.SetTransObject ( sqlca )  
  17.       if li_rc < 0 then  
  18.         MessageBox ( "Error", "SetTransObject failed" )  
  19.         return -1  
  20.       else  
  21.         return 1  
  22.       end if  
  23.     end if  
  24.   end if  
  25. catch ( Throwable e )  
  26.   Exception exp  
  27.   exp = create Exception  
  28.   exp.SetMessage(e.GetMessage())  
  29.   throw exp  
  30. end try  

4.  The Visual Studio developer then creates a proxy object based on the IAdoConnectionProxy interface, create it based on AdoConnectionProxy, assigns the connection and transaction to it, and passes into your method on the custom visual user object.

  1. short rc;  
  2.               
  3. String connectString = "Data Source=//oracle:1521/orcl;User Id=scott;Password=tiger2";  
  4. Oracle.DataAccess.Client.OracleConnection conn = new Oracle.DataAccess.Client.OracleConnection(connectString);  
  5. conn.Open();  
  6. Oracle.DataAccess.Client.OracleTransaction trans = conn.BeginTransaction();  
  7.     
  8. Sybase.PowerBuilder.DataSource.Sharing.IAdoConnectionProxy proxy;  
  9. proxy = new Sybase.PowerBuilder.DataSource.Sharing.AdoConnectionProxy();  
  10.               
  11. proxy.Connection = conn;  
  12. proxy.Transaction = trans;  
  13.               
  14. rc = u_customvisual1.AssignConnection(proxy);  

5.  Note that when you have methods you want to expose on the custom visual user object, you select those rather than the custom visual user object in the project painter, so that PowerBuilder knows it needs to expose those methods on the control in the assembly.

No comments: