Solved

What's wrong with this block of code?

Posted on 2007-11-23
10
203 Views
Last Modified: 2010-04-01
Writing an ODBC application in C++ using the Win32 API.

The system sets the environment handles and connects to the DSN successfully.  However, the call to SQLExecDirect() is returning an error code, but the error handling block's call to SQLGetDiagRec() is not giving me any information about what the error is (A dialog with no information pops up).  I have included the relevant code block.  My main concern is the error handling block.  If I can at least figure out what the error is, I can troubleshoot the SQLExecDirect() problem myself.

Thanks!
retCode=SQLExecDirect(hStmt, (SQLCHAR*)spQuery, SQL_NTS);	//Execute discovery query
if(retCode!=SQL_SUCCESS && retCode!=SQL_SUCCESS_WITH_INFO)
{
	SQLCHAR sqlState[6]="";
	SQLINTEGER nativeError=NULL;
	SQLCHAR errMsg[SQL_MAX_MESSAGE_LENGTH]="";
	int i = 1;
	char message[512]="";
	SQLGetDiagRec(SQL_HANDLE_DBC, hDbc, i, sqlState, &nativeError, errMsg, sizeof(errMsg), NULL);
	sprintf(message, "Diag: %d, SQLSTATE: %s NativeError: %d ErrMsg: %s", i++, sqlState, nativeError, errMsg);
	MessageBox(NULL, message, "Err", MB_OK);
}

Open in new window

0
Comment
Question by:cuziyq
  • 3
  • 2
  • 2
  • +2
10 Comments
 
LVL 40

Expert Comment

by:evilrix
ID: 20338879
When you say "no information" can you be more specific please? An empty dialog or text without the error details filled in?
0
 
LVL 22

Expert Comment

by:grg99
ID: 20338915
Try initializing the error info fields with "foo", 999, and "goo".  Perhaps the get diag call is failing.  Does that function perhaps return its own error code that you could check?
0
 
LVL 14

Author Comment

by:cuziyq
ID: 20338957
No information.  In the if{} block, I have sqlState, nativeError, and errMsg all intialized to empty strings.  The call to SQLGetDiagRec() is supposed to fill those variables with the relevant information.  Then a message box comes up displaying the values of those variables.

The dialog box is coming up with nothing.  It says "Diag: 1, SQLSTATE: , NativeError: , ErrMsg: " with an OK button.  Setting a breakpoint at the MessageBox() line confirms that all the strings are still empty.

The block does work correctly in other contexts.  If I misspell the password or the DSN name, for example, the error pops up telling me exactly what is wrong.  It all runs through the same function.  This is the only case where I get nothing, and I don't know why.
0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 20

Expert Comment

by:ikework
ID: 20339234
you assume there was an error, but you didnt check the return value of SQLGetDiagRec.

adapted from http://msdn2.microsoft.com/en-us/library/ms711727.aspx:


SQLRETURN rc2;
i = 1;
while ((rc2 = SQLGetDiagRec(SQL_HANDLE_DBC, hDbc, i, sqlState, 
                            &nativeError, errMsg, sizeof(errMsg), NULL)) 
                            != SQL_NO_DATA) 
{
    sprintf(message, "Diag: %d, SQLSTATE: %s NativeError: %d ErrMsg: %s", 
            i++, sqlState, nativeError, errMsg);
    MessageBox(NULL, message, "Err", MB_OK);
    ++i;
}
 
 
// ike

Open in new window

0
 
LVL 14

Author Comment

by:cuziyq
ID: 20339371
Well, folks, I figured out the source of my error.  My Stored Procedure has an ORDER BY clause on a SELECT COUNT(*) query, which is illegal.  I fixed the stored procedure and it now works fine.  I am sure I will revisit the issue later when I have some other stored procedure that doesn't work properly.
0
 
LVL 40

Expert Comment

by:evilrix
ID: 20339451
You should be able to order by count as a named field...

select count(*) as cnt from sometable group by somefield order by cnt;
0
 
LVL 20

Expert Comment

by:ikework
ID: 20339468
>> You should be able to order by count as a named field...

some rdbms allow it .. some not .. mostly "order by count(*)"  works
0
 
LVL 14

Accepted Solution

by:
cuziyq earned 0 total points
ID: 20341525
One more thing for those who are curious . . . the error handling block does not work because the call to SQLGetDiagRec() was passing in the connection handle.  It worked before because when I deliberately misspelled the password or the DSN, it was a connection issue.  However, when I messed up the query and called SQLGetDiagRec(), it was no longer a connection issue.  SQL_HANDLE_DBC is inappropriate for diagnosing a statement error.  Passing in the statement handle (SQL_HANDLE_STMT) is necessary to get error information about a failed query.  When I chaged it, I get a full, plain English text error message in all its glory.
0
 
LVL 1

Expert Comment

by:modus_operandi
ID: 20437964
Closed, 500 points refunded.
modus_operandi
EE Moderator
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
Via a live example, show how to setup several different housekeeping processes for a SQL Server.
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question