Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

What's wrong with this block of code?

Posted on 2007-11-23
10
Medium Priority
?
210 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

 
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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

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…
In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

722 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