Can you Test and trap for ADO SQL Open connection fail?

rsaritzky
Member Posts: 469
Hi,
There are numerous posts here on how to create an ADO SQL connection. The general syntax is to define a variable of type "Automation" and subtype "Microsoft ActiveX Data Objects...", then
ADOConn.Create
then
ADOConn.Open(ConnectionString); //ConnectionString is the text of the connection string.
When the OPEN works successfully, of course there's no problem. However, when the connection fails, a "modal" error message is displayed. Here's a sample error:
This message is for C/AL programmers:
The call to member Open failed. Microsoft OLE DB Provider for SQL Server returned the following message:
[DBNETLIB][ConnectionOpen (Connect()).]SQL Server does not exist or access denied.
My question is:
Is there a way to TRAP for a failed connection, so that my C/AL code can handle the error? The ADOConn.Open does not return a value directly.
DotNet-related articles on this subject refer to "try/catch" techniques (not available in C/AL) and additional return codes (adstate) AFTER the Open command. The problem is this "modal" error message that just causes the code to fail.
Has anyone figured out a way to test for a valid SQL connection in C/AL Code (doesn't have to be ADO)?
Thanks
There are numerous posts here on how to create an ADO SQL connection. The general syntax is to define a variable of type "Automation" and subtype "Microsoft ActiveX Data Objects...", then
ADOConn.Create
then
ADOConn.Open(ConnectionString); //ConnectionString is the text of the connection string.
When the OPEN works successfully, of course there's no problem. However, when the connection fails, a "modal" error message is displayed. Here's a sample error:
This message is for C/AL programmers:
The call to member Open failed. Microsoft OLE DB Provider for SQL Server returned the following message:
[DBNETLIB][ConnectionOpen (Connect()).]SQL Server does not exist or access denied.
My question is:
Is there a way to TRAP for a failed connection, so that my C/AL code can handle the error? The ADOConn.Open does not return a value directly.
DotNet-related articles on this subject refer to "try/catch" techniques (not available in C/AL) and additional return codes (adstate) AFTER the Open command. The problem is this "modal" error message that just causes the code to fail.
Has anyone figured out a way to test for a valid SQL connection in C/AL Code (doesn't have to be ADO)?
Thanks
Ron
0
Comments
-
You need to run your code in a codeunit.
Call the Codeunit like this.
If YourCodeunit.run then
Success
else
Message('failed');0 -
Hi Rashed,
This ALMOST works. It turns out that if you are updating the database, you cannot use an "IF CODEUNIT.RUN..." in the middle of a write transaction. But it DOES avoid the "modal" error message. I'll keep you posted.Ron0 -
Yes, it has to be a new transaction. you need to change the code.0
Categories
- All Categories
- 73 General
- 73 Announcements
- 66.6K Microsoft Dynamics NAV
- 18.7K NAV Three Tier
- 38.4K NAV/Navision Classic Client
- 3.6K Navision Attain
- 2.4K Navision Financials
- 116 Navision DOS
- 851 Navision e-Commerce
- 1K NAV Tips & Tricks
- 772 NAV Dutch speaking only
- 617 NAV Courses, Exams & Certification
- 2K Microsoft Dynamics-Other
- 1.5K Dynamics AX
- 320 Dynamics CRM
- 111 Dynamics GP
- 10 Dynamics SL
- 1.5K Other
- 990 SQL General
- 383 SQL Performance
- 34 SQL Tips & Tricks
- 35 Design Patterns (General & Best Practices)
- 1 Architectural Patterns
- 10 Design Patterns
- 5 Implementation Patterns
- 53 3rd Party Products, Services & Events
- 1.6K General
- 1.1K General Chat
- 1.6K Website
- 83 Testing
- 1.2K Download section
- 23 How Tos section
- 252 Feedback
- 12 NAV TechDays 2013 Sessions
- 13 NAV TechDays 2012 Sessions