posh studio EXE gets an ODBC connection error

Ask questions about creating Graphical User Interfaces (GUI) in PowerShell and using WinForms controls.
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
This topic is 2 years and 3 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
User avatar
fvenezia65
Posts: 8
Last visit: Wed Feb 14, 2024 9:44 am

posh studio EXE gets an ODBC connection error

Post by fvenezia65 »

good afternoon!

Looking for some help with a posh studio EXE that gets an ODBC connection error.

Product: PowerShell Studio 2019 (64 Bit)
Build: v5.6.160
OS: Windows 7 Professional (64 Bit)
Build: v6.1.7601.65536

I’m creating a “powershell v5 host (windows application)” EXE (64bit) as a wrapper to run a standalone powershell-script that connects to an AS400 using ODBC.

The script runs successfully, when run from a posh-console-window.

When I call the same script from my EXE, it fails to connect via ODBC with this error:

“Exception calling "Fill" with "1" argument(s): "ERROR [08S01] [IBM][System i Access ODBC Driver]Communication link failure. comm rc=11003 - CWBCO1004 - Remote address could not be resolved"
At \\nasdata201\sharedata\NSUH-IS01\Data\OC Tools\PowerShell-scripts\NwHS-Get-JournalReceiverAttributes.ps1:107 char:1
+ [void]$adapter.Fill($dataset)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : OdbcException”

anything special i need to do to be able to use ODBC from within the EXE?
if i run the same exact script from the posh-console (32 and 64-bit), it can use ODBC properly.

thanks for any help!
-frank
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: posh studio EXE gets an ODBC connection error

Post by jvierra »

It looks like you have no connection. Why are you creating a GUI app with Windows Application Host? YOu should be using the Windows Forms host for a GUI app.

Note your error: "- Remote address could not be resolved"". This means you have no name resolution on your system. Likely your AS400 configuration is not set up correctly.
User avatar
fvenezia65
Posts: 8
Last visit: Wed Feb 14, 2024 9:44 am

Re: posh studio EXE gets an ODBC connection error

Post by fvenezia65 »

hi! thanks for answering.
i use the windows application host, so i can redirect the script's output to a console, instead of a place on a form - works like a charm. (i've got psm1's that get loaded and they had trouble writing back to a place on the form.)
in this case, i know the name resolution is working - if i run the same EXACT script (.ps1) from the posh-console, it works properly - connects to the as400, runs commands, and receives output.
it's only when the same EXACT script is called with the same parameters from the posh-studio-generated-EXE that it fails.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: posh studio EXE gets an ODBC connection error

Post by jvierra »

The most common reason for this is that your code is written wrong and without the connection code there is no way to know how you have done this. If you use "Fill" wrong then you can get this error. Remember that an EXE is not a console and it does not load the environment in the same way. This can happen with ODBC drivers. Again, without code there is no way for us to know what you are trying to do.

Also note that using a DataAdapter is only used to bind to forms or to manage relational datasets. If you are loading one table or view then we would use a DataTable object and the data tables "Load" method.
-
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: posh studio EXE gets an ODBC connection error

Post by jvierra »

Here is a test step that will clear up the differences between using a forms mode and a console.

Build with "Sapien PowerShell V5 Host (Commandline)" and be sure that STA remains checked. Run the EXE and see if you get the same error. This will be closer to PS Console" than the Application host.
User avatar
Alexander Riedel
Posts: 8473
Last visit: Tue Mar 19, 2024 1:15 am
Answers: 19
Been upvoted: 37 times

Re: posh studio EXE gets an ODBC connection error

Post by Alexander Riedel »

Most commonly when things work in the console but not in a packaged exe, these are the culprits (in no particular order):
- Console/ISE is running elevated, executable is not.
- Not checking STA when packaging, but is required
- Console/ISE (powershell.exe) has a firewall exception, packaged exe does not
- Console/ISE initializes values / modules / code in a profile, which is NOT executed by a packaged exe
- Location of the packaged executable is not trusted, for example on a network share
- Requirement that executables have to be signed is not filled
Alexander Riedel
SAPIEN Technologies, Inc.
User avatar
fvenezia65
Posts: 8
Last visit: Wed Feb 14, 2024 9:44 am

Re: posh studio EXE gets an ODBC connection error

Post by fvenezia65 »

thanks so much for your help!
definitely a snafu in the packaged EXE interacting with ODBC for this odd-ball as400-connection (that uses SQL to run commands on the as400!).
appreciate the efforts here.
i'm going to let our team run this from a posh-console and give up on a pretty EXE.
have a great day.
-frank
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: posh studio EXE gets an ODBC connection error

Post by jvierra »

Don't give up just drop the need for a console and output to a textbox on the form.

Using a co-console with a form always finds conflicts in third-party tools often due to threading issues. The AS400 drivers have always had critical requirements.

Take the simple approach. Place connection code in new form on a button click and use the standard Sapien Forms engine and test. If it works then skip the console Window and use that engine.
User avatar
fvenezia65
Posts: 8
Last visit: Wed Feb 14, 2024 9:44 am

Re: posh studio EXE gets an ODBC connection error

Post by fvenezia65 »

thanks, jvierra!
i do have another wrapper-EXE that runs a similar as400-connection script successfully.
the only difference is that in my new script, the as400-connection stuff was moved into a function, since i needed to make a bunch of similar calls.
appreciate your help!
-frank
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: posh studio EXE gets an ODBC connection error

Post by jvierra »

If you have the connection object in a function then it will be out-of-scope when the function ends.

Create all data object for the connection in a global scope and this won't happen.

Again, without your code there is no perfect way to understand your issue.
This topic is 2 years and 3 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked