Solved

What's wrong with this block of code?

Posted on 2007-11-23
10
206 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
Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

 
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

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
parameter pack in c++11 2 21
Complex SQL Server WHERE CLause 9 37
SQL syntax for max(date) 3 36
SQL Server how to use a VARIABLE to link tables in a SQL Script? 3 41
Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

739 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