Showing posts with label ODBC. Show all posts
Showing posts with label ODBC. Show all posts

Sunday, September 20, 2009

Error: Data source name not found and no default driver specified

When working with ODBC connections in Ax, you may come across this error message:

[Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified

Or the complete error message:

ODBC operation failed.

Unable to logon to the database.

[Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified

Object 'OdbcConnection' could not be created

Then you check the control panel - Administrative tools - ODBC connections.
And you see your datasource is there. You check the name, and it's spelled ok. So what could be wrong? (Always check the most probable cause of your error message first, a typing error.)

As you might know, the Ax client is a 32 bit application. And nowadays, you might have a 64 bit operating system. Datasources created the default way in a 64 bit operating system cannot be used by Ax. So you need to create a 32 bit datasource.
There is an alternative panel applet to create a 32 bit datasource, fe in Windows Server 2008. You'll find this applet in the C:\Windows\SysWOW64 folder, called odbcad32.exe. (Now watch out, a file by the same name might be available in the C:\Windows\System32 folder. This is NOT the version needed.)

Start the executable C:\Windows\SysWOW64\odbcad32.exe and create your 32 bit datasource. Afterwards, re-execute your code. Did that help make the error go away?
(Some Microsoft info about the issue.)

Create an ODBC connection in Ax

In Ax, you are never on an island. You have lots of options to bring in data from other systems, whether it's through AIF, reading an XML file or.. directly connecting to another database. In this post, I'll show how to create an ODBC connection to a external database.
For this, we'll use the standard available ODBCConnection class, as opposed to CLR Interop and rely on the .NET functions.
Here goes, in 3 simple steps.
  1. Set the login property
  2. Create the connection
  3. Read from a sample table

static void myODBCConnection(Args _args)
{ ODBCConnection myODBC;
Statement myStatement;
LoginProperty myLoginProperty;
Resultset myResultset;

str mySQLStatement;
str myConnectionString;

str myDSN="yourdatasourcenamehere";
str myUserName="yourusername";
str myPassword="yourpassword";
;
myConnectionString=strfmt("DSN=%1;UID=%2;PWD=%3",myDSN,myUserName,myPassword);

myLoginProperty = new LoginProperty();
myLoginProperty.setOther(myConnectionString);

try
{
myODBC = new OdbcConnection(myLoginProperty);
myStatement=myODBC.createStatement();

mySQLStatement="SELECT Field1,Field2 FROM myTable";
myResultSet=myStatement.executeQuery(mySQLStatement);
while (myResultSet.next())
{
info(myResultSet.getString(1));
info(int2str(myResultSet.getInt(2)));
}
} catch
{
error('Unexpected error');
}
}


In earlier versions from Ax, the LoginProperty came with the methods to set the loginname and password. But Microsoft has cut these features (security reasons?). But as you can see in the example, we work around that by using the 'SetOther' method.

There's one thing you need to consider. The Ax client requires a 32 bit datasource. On a 64 bit system like Windows 2008, this may result in an error 'Data source name not found'. That's something for my next post.