Link to home
Start Free TrialLog in
Avatar of Brian Foster
Brian FosterFlag for United Kingdom of Great Britain and Northern Ireland

asked on

Sage 50 ODBC connection fails when task scheduled - VB.Net

Hi guys

My program includes a simple query on Sage 50 via ODBC, something I've programmed hundreds of times before. The Sage data tables are on a network drive. On this particular client's PC (Windows 7, 64 bit), the query ran perfectly every time when run manually, but threw the following errors when scheduled:

ERROR [08001] Cannot find all files in data path
ERROR [01000] The driver returned invalid (or failed to return) SQL_DRIVER_ODBC_VER: 23.1
ERROR [IM006] [Microsoft][ODBC Driver Manager] Driver's SQLSetConnectAttr failed
ERROR [01000] [Microsoft][ODBC Driver Manager] The driver doesn't support the version of ODBC behavior that the application requested (see SQLSetEnvAttr).

I checked all the DSNs, drivers and registry and everything seemed OK. I did a search and the only relevant post was one here by RogerMoquin:

Problem with ODBC connection when run from a scheduled task
      
His solution was to insert a two minute delay, though I think his issue was with login.  In my case the program runs long after login. Using his idea, however, I found one solution was to set the program to try to connect, then each time an error exception is raised, it waits two minutes. After 3 tries it consistently connects. (Please note that I changed to a DSN-less connection string, but it still uses the 3 tries).

' Original 
'Dim ConnectionString As String = "DSN=SageLine50v23;UID=MyID;PWD=MyPassword;"
'DSN-less
Dim ConnectionString As String = "Driver={Sage Line 50 v23};DIR=S:\ACCDATA;UID=MyID;PWD=MyPassword;"

....
        Dim Conn As OdbcConnection
        Dim Comm As OdbcCommand
        Dim DR As OdbcDataReader
        Dim SQL As String
        Dim Retry As Boolean = True
        SQL = "SELECT stock_code, qty_in_stock from stock ORDER BY stock_code"
        Conn = New OdbcConnection(ConnectionString)
        Comm = New OdbcCommand(SQL, Conn)
        Do While Retry
            Try
                Tries += 1
                Conn.Open()
                Retry = False
            Catch oe As OdbcException
                If Tries < 3 Then
                    Retry = True
                    Threading.Thread.Sleep(120000) ' wait 2 min
                Else
                    Retry = False
                    ErrCode("Sage Connection " & oe.Message.ToString() & vbCrLf & "[T" & Tries & "]")
                End If
            End Try
        Loop
        Try
            DR = Comm.ExecuteReader()
            While DR.Read()
                CWCode(RecCount) = DR("Stock_code").ToString()
                CWQty(RecCount) = DR("Qty_In_Stock").ToString()
                RecCount += 1
            End While
            DR.Close()

Open in new window


Firstly I would like to thank RogerMoquin for inspiring my solution. Finally, I would like to understand where the problem lies and if there is a better way of avoiding this in future. Any ideas?
Avatar of melmers
melmers
Flag of Germany image

Could it be that the task is not correctly configured ?

Drive S: is this the User Home drive which will mapped during a login script ?

Is the task configured to use the specific user who is logged in ? if yes then create a batch file which maps first the share path to the
correct drive and then run your software.
Avatar of Brian Foster

ASKER

Thank you for your speedy response!
1. There could be something which I missed, configuration-wise, but do you mean in the Task Scheduler? However, the task runs 100% flawlessly manually and now that I have ticked the "Run with highest privileges" box, the task itself runs to schedule OK. The problem seems to be with time lags in connecting.
2. Drive S: is the shared drive and I assume is mapped during a login script. I only have very limited knowledge of how networks are set up, but I could perhaps find out more if this is important.  
3. The task is configured in Task Scheduler to run whether user is logged on or not. I created it using his account via remote connection. I would have thought that if it was a permissions issue, the program would either run or not, rather than just being slow to connect.
My patch seems to be working for the moment, but it seems more of a fix than a full solution.
Many thanks for your help.
Hi,
you can try to create and read a file on the shared drive. if you can't read or write you have an access right problem.

can you verify the user who is running the task?is this the correct user?
I cannot try your suggestion at the moment, because I cannot connect to the client (650 km away) for maybe a week.

The task was set up using the user's login. It runs on his PC after he has logged in. So how can permissions be an issue? Also if he doesn't have the right permissions, surely it wouldn't run at all?

I very much appreciate your help.
This question needs an answer!
Become an EE member today
7 DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform.
View membership options
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.